[ANN] The argparse module

Steven Bethard steven.bethard at gmail.com
Thu Aug 3 07:41:32 CEST 2006


I'm pleased to announce initial availability of the argparse module,
an optparse-inspired command line parser:

    http://argparse.python-hosting.com/

The argparse module can be downloaded as a single file, argparse.py,
through the Browse Source link on the website.

Argparse improves on optparse by:

* handling both optional and positional arguments
* supporting parsers that dispatch to sub-parsers
* producing more informative usage messages
* supporting arguments that consume any number of string args
* allowing types and actions to be specified with simple callables instead
  of hacking class attributes like STORE_ACTIONS or CHECK_METHODS

as well a number of other improvements on the optparse API.

Just to whet your appetite:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> _ = parser.add_argument('--foo', type=int, choices=[1, 2, 3],
...                         help='optional with integer choices')
>>> _ = parser.add_argument('--bar', nargs='?', const='C', default='D',
...                         help='optional with or without arg')
>>> _ = parser.add_argument('baz', nargs='+', type=float,
...                         help='positional with one or more args')
>>> parser.parse_args('--bar --foo 2 0.5 1.5'.split())
Namespace(bar='C', baz=[0.5, 1.5], foo=2)

This is the first public presentation of this module, so the APIs are
still subject to change. If you find it of interest, and you'd like to
help improve it, I'd be glad to open the project up to more
developers.

Bug reports can be made through Trac at:
    http://argparse.python-hosting.com/newticket


Steven Bethard


More information about the Python-announce-list mailing list