Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

After several years of Front-End Development I recently started development with python. I have created some basic webapps with Django and wrote some simple scripts. After using VIM as a Python IDE I really fell I love with "Command-line programs" (is there an official term for this?). Right now I am capable of doing newbie things like asking someones age and printing it to the screen. However this comes down to running a .py script and after this script is done the normal bash return. I would like create a program that I can run from the command line and that would allow the same user experience as VIM (one that you open and close). For example I created a simple script to import RSS feeds. It would be cool if I could open my terminal type the name of my program -> program would open -> Then I would like to use commands like :findsomething. Basically have real interaction with my program.

To conclude:

  • How would I got about creating such a program?
  • What kinds of modules, books or site would you recommend for a noob programmer?
share|improve this question
3  
What you're describing is a terminal application, not a command line program –  Eric Jun 27 '13 at 20:49
add comment

4 Answers

up vote 7 down vote accepted

A true command-line program is something in the vein of ls or grep; it is started from the command-line, but it's non-interactive and can be used in pipelines and combined with other programs. A typical command-line program has no interactive user experience, instead relying on shell's history and init file for customization.

What you want to create is a curses application, that uses the full capabilities of the TTY as an interactive platform, for better or worse. To do that, look up curses.

share|improve this answer
    
Thanks I started learning the curses module. Seems be exactly what I was looking for! –  Niels B Jun 28 '13 at 0:25
add comment

You should take a look at the cmd module.

See the Python Cookbook for examples of its use.

share|improve this answer
add comment

If you want to create an standalone binary for a UNIX system, use freeze. If you want one for a Windows system, look into py2exe. To control locations of output on your screen, use the curses module.

share|improve this answer
add comment

THe simplest way to do an interactive console application would be:

keep_going = True
while keep_going:
    command = raw_input('command? ').strip()
    if command == 'say_hello':
        print('Hello')
    elif command == 'other_thing':
        print('Doing something else')
    elif command == 'quit':
        keep_going = False
    else:
        print('Invalid Command.')

That's the basic structure. If you want something more vim-like, you'll probably need to use the curses library.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.