[ANN] argparse 0.4 - Command-line parsing library

Steven Bethard steven.bethard at gmail.com
Fri Jan 5 01:45:36 CET 2007

Announcing argparse 0.4

argparse home:

argparse single module download:

argparse bundled downloads at PyPI:

New in this release

* The add_argument method now allows a positional argument with
nargs='*' to specify a default value::

    >>> parser = argparse.ArgumentParser()
    >>> parser.add_argument('foo', nargs='*', default=['def'])
    >>> parser.parse_args('1 2 3'.split())
    Namespace(foo=['1', '2', '3'])
    >>> parser.parse_args([])

About argparse

The argparse module is an optparse-inspired command line parser that
improves on optparse by:

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

as well as including a number of other more minor improvements on the
optparse API. To whet your appetite, here's a simple program that sums
its command-line arguments and writes them to a file::

   parser = argparse.ArgumentParser()
   parser.add_argument('integers', type=int, nargs='+')
   parser.add_argument('--log', type='outfile', default=sys.stdout)
   args = parser.parse_args()
   args.log.write('%s\n' % sum(args.integers))

More information about the Python-announce-list mailing list