Add subparser aliases for argparse. Resolves issue 9324. Approved by Georg for beta2 on the tracker.

This commit is contained in:
Steven Bethard 2010-12-18 11:19:23 +00:00
parent 04129748ae
commit fd311a712d
5 changed files with 77 additions and 6 deletions

View file

@ -1428,6 +1428,16 @@ Sub-commands
{foo,bar} additional help
Furthermore, ``add_parser`` supports an additional ``aliases`` argument,
which allows multiple strings to refer to the same subparser. This example,
like ``svn``, aliases ``co`` as a shorthand for ``checkout``::
>>> parser = argparse.ArgumentParser()
>>> subparsers = parser.add_subparsers()
>>> checkout = subparsers.add_parser('checkout', aliases=['co'])
>>> checkout.add_argument('foo')
>>> parser.parse_args(['co', 'bar'])
Namespace(foo='bar')
One particularly effective way of handling sub-commands is to combine the use
of the :meth:`add_subparsers` method with calls to :meth:`set_defaults` so
@ -1466,7 +1476,7 @@ Sub-commands
>>> args.func(args)
((XYZYX))
This way, you can let :meth:`parse_args` does the job of calling the
This way, you can let :meth:`parse_args` do the job of calling the
appropriate function after argument parsing is complete. Associating
functions with actions like this is typically the easiest way to handle the
different actions for each of your subparsers. However, if it is necessary