Python usage notes/Command line argument parsing

From Helpful
Jump to: navigation, search
Various things have their own pages, see Category:Python. Some of the pages that collect various practical notes include:


This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

Note that these are mostly for command line options that adhere to POSIX syntax or syntax which is similar enough, not necessarily your own creative definitions.


optparse

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

Takes more study than getopt, and more code, but is more flexible.

These days, you may wish to look at argparse instead (≥2.7).


Some example:

from optparse import OptionParser
 
p = OptionParser() # Has some options, but we're ignoring them here
 
# storing an also-specified value is the default action
p.add_option("-o", "--output",  dest="outputfile", help="write output to file")
 
# toggling things can be solved with default-unless-present (no value), e.g.
p.add_option("-s", "--show",  dest="show", default=False, action="store_true"
             help="show output in window")
 
options, args = p.parse_args()  # defaults to parsing sys.argv[1:].  May exit the program.
print 'all options:  %r'%options
print 'args:         %r',args


Notes:

  • you mostly just need to specify how each option should be handled
  • you can specify a short and/or a long form
  • -h and --help are registered by default
  • the things you name in dest will sit in such-named object attributes on the options object, e.g. options.outputfile, options.show
  • on errors:
    • you can manually trigger option-parsing-related errors (e.g. while doing your own sanitizing) by calling p.error().
    • You may want to also play with p.print_help()
    • optparse's response to errors consists mostly of printing an error message and exiting. There is no exception to catch, or ability to ignore errors.
If you want that, you'll have to subclass OptionParser and override its exit() and/or error().


  • on add_option():
    • the default default value is None
    • dest will create an attribute on the options object that is returned.
    • you can specify a default value for each attribute to take when no storage action is taken. If you don't mention a default value, it will be None.
    • action (default is store) can be one of the following: (the first three are probably the most commonly used)
      • "store": the next part in the user arguments is the value, converted and stored as that value.
      • "store_const": store a pre-set value. No value is taken from the user arguments
      • "store_true", store_false: specific cases of store_const for True and False
      • "append": like store, but if a value was already present, we append instead of overwrite
      • "append_const": append, but with a supplied valie instead of a user value
      • "count": count the amount of times something is mentioned. You can use it to handle something like -v, -vv, -vvv, etc. as different levels of verbosity.
      • "callback": Call a named function. Mostly useful for hacking your own functionality on top, such as additional checks (e.g. checking whether one option was already set, used after another, reaction to ' -- ' meaning 'no more processing', etc.)
      • "help": react to use of this argument by printing help (You'll probably rarely do this yourself, because it's registered under -h and --help by default)
      • "version": react to use of this argument by printing a value handed along. You'll rarely do this yourself.
    • you can specify that the value should be converted to another type. Built-in conversions are "string", "int", "long", "choice", "float" and "complex"; you can make your own.


# you can share the same variable can get confusing. I suggest you avoid this, and handle the behaviour
# in some of your own logic.
p.add_option("-v", "--verbose", dest="verbose", default=None, action="store_true",
             help="print detailed status")
p.add_option("-q", "--quiet",   dest="verbose", default=None, action="store_false",
             help="don't print status (default)")
# the above is sort of a three-valued thing now -- False for quiet, None for medium, True for verbose.
#   in the script this came from that's a feature, but it can also be a bug. 
#   using store_const would be clearer here
# None is the default default, I add it here for clarity -- and to point out that different default values would be confusing
 
 
# so does this default to True or None, and why? Would you bet on it without diving into the docs?
p.add_option("-t", "--text",  dest="textonly", default=True, action="store_true",
             help="only handle text files (default)")
p.add_option("-b", "--binary",dest="textonly",               action="store_false",
             help="also handle binary files (see also -t/--text)")

See also


getopt

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

Less powerful than optparse, also less code to write.

Separates/extracts options without values, options with values, and things not part of the options (e.g. filenames meant to be passed in) - though it seems not to deal with options after arguments(verify)

See http://docs.python.org/library/getopt.html


Example (mostly from python docs): Getopt is generally applied to sys.args[1:] (getopt.getopt(sys.args[1:], ...).

For code-example's sake, an array is literally supplied here:

import getopt
 
#In the real world you would use sys.argv[1:]
example = ['-a', '-b', '-cfoo', '-d', 'bar', 'file1', 'file2'] 
 
optlist, args = getopt.getopt(example, 'abc:d:')
Now optlist is
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
, and args is
['file1', 'file2']


Can also take long options. Example (mostly from python docs):

try:
    opts, args = getopt.getopt(sys.argv[1:], 
                               "ho:v",
                               ["help", "output="])
except getopt.GetoptError, err:
    print str(err) # will print something like "option -a not recognized"
    usage()
    sys.exit(2)
 
# default values, can be overwritten by the actual options
output = None
verbose = False
 
#iterate over the things we got
for o, a in opts:
    if o == "-v":
        verbose = True
    elif o in ("-h", "--help"):
        usage()
        sys.exit()
    elif o in ("-o", "--output"):
        output = a
    # ...
    else:
        raise RuntimeError("unhandled command line option")