#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    How do I program a command line interface?


    How would I program a command line interface so that my program constantly asks for user input and I can type strings and the program responds depending on the string input?

    I know I can do it with raw_input and then use an if then statement to go through the list of arguments but is there a more efficient or easier way?

    I saw argparse and optparse but I can't seem to get those to work without running the script with arguments from the command line...

    Zach
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    You can give argparse fake arguments, as shown here.

    Had you considered using the cmd module?

    Following program supports help, ls, and cd commands. If you explain why exit does not work I'll explain why I used exec.
    Code:
    import os, sys, pprint
    
    DEBUG = True
    
    if sys.version[0] != '3':
        print('This program requires python version 3')
        exit(1)
    
    commands = dict(
        ls=(os.listdir, os.curdir,),
        cd = (os.chdir, None,),
        exit=(sys.exit, 0,),
        )
    
    commands['help'] = (sys.stdout.write, str(set(commands))+'\n',)
    
    def obey(command, argument):
        (f, a,) = command
        if a is None:
            a = argument
        try:
            result = f(a)
        except:
            if DEBUG:
                print(f,a)
            result = 'failed'
        return result
    
    exec('''while True:
        sys.stdout.write("Hell's gate! ")
        sys.stdout.flush()
        request = sys.stdin.readline()
        fields = request.split()
        if not fields:
            break
        (command, *arguments) = fields
        command = command.lower()
        if command in commands:
            result = obey(commands[command], ' '.join(arguments))
            if result is not None:
                pprint.pprint(result)''')
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Originally Posted by b49P23TIvg
    Following program supports help, ls, and cd commands. If you explain why exit does not work I'll explain why I used exec.
    You're catching the SystemExit exception that sys.exit raises. Use "except Exception:" instead of bare "except:".
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481

    Thank you!


    Code:
    (command, *arguments) = fields(command, *arguments) = fields
    I wrote code to fail gracefully if not using python 3. The program would fail with syntax error in python 2, you'd never execute the graceful failure. I solved the problem by packing the syntax error into a string.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo