a better getopt than getopt.py?

Sean 'Shaleh' Perry shaleh at valinux.com
Mon Feb 19 20:21:47 EST 2001


Writing my fisrt real python app that people will run from the command line
(most of my previous coding has been modules for other software to web stuff)
I decided to use getopt.  My setup looks like this:

LONG_OPTIONS = ['help', 'setup-lab', 'lab=']
SHORT_OPTIONS = 'hS'
    
try:
    optlist, args = getopt.getopt(sys.argv[1:], SHORT_OPTIONS,
                                  LONG_OPTIONS)
except getopt.error, e:
    print e
    print USAGE
    sys.exit(1)

for option, argument in optlist:
    if option in ('-h', '--help'):
        print USAGE
        sys.exit(0)
    elif option in ('-S', '--setup-lab'):
        print 'Want to set up the lab ...'
    elif option == 'lab':
        print 'Put lab in: ' + argument

this means I have my options defined twice and then once again in the USAGE
output.  Is there not a better way to handle arguments?




More information about the Python-list mailing list