summaryrefslogtreecommitdiff
path: root/Doc/library
diff options
context:
space:
mode:
authorSteven Bethard <steven.bethard@gmail.com>2010-12-18 11:19:23 +0000
committerSteven Bethard <steven.bethard@gmail.com>2010-12-18 11:19:23 +0000
commitfd311a712d5876c3a3efff265978452eea759f85 (patch)
tree6222b825a28cbe2e94f167eb4f1ae519c6dada52 /Doc/library
parent04129748ae4f794dffbcf07bf53be27dc79c2d09 (diff)
downloadcpython-git-fd311a712d5876c3a3efff265978452eea759f85.tar.gz
Add subparser aliases for argparse. Resolves issue 9324. Approved by Georg for beta2 on the tracker.
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/argparse.rst12
1 files changed, 11 insertions, 1 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
index 0ca8aa0661..72878c5474 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -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