diff options
author | Steven Bethard <steven.bethard@gmail.com> | 2010-12-18 11:19:23 (GMT) |
---|---|---|
committer | Steven Bethard <steven.bethard@gmail.com> | 2010-12-18 11:19:23 (GMT) |
commit | fd311a712d5876c3a3efff265978452eea759f85 (patch) | |
tree | 6222b825a28cbe2e94f167eb4f1ae519c6dada52 /Doc/library | |
parent | 04129748ae4f794dffbcf07bf53be27dc79c2d09 (diff) | |
download | cpython-fd311a712d5876c3a3efff265978452eea759f85.zip cpython-fd311a712d5876c3a3efff265978452eea759f85.tar.gz cpython-fd311a712d5876c3a3efff265978452eea759f85.tar.bz2 |
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.rst | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 0ca8aa0..72878c5 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 |