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 /Lib/argparse.py | |
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 'Lib/argparse.py')
-rw-r--r-- | Lib/argparse.py | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index 557cc00..57eaaad 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1023,9 +1023,13 @@ class _SubParsersAction(Action): class _ChoicesPseudoAction(Action): - def __init__(self, name, help): + def __init__(self, name, aliases, help): + metavar = dest = name + if aliases: + metavar += ' (%s)' % ', '.join(aliases) sup = super(_SubParsersAction._ChoicesPseudoAction, self) - sup.__init__(option_strings=[], dest=name, help=help) + sup.__init__(option_strings=[], dest=dest, help=help, + metavar=metavar) def __init__(self, option_strings, @@ -1053,15 +1057,22 @@ class _SubParsersAction(Action): if kwargs.get('prog') is None: kwargs['prog'] = '%s %s' % (self._prog_prefix, name) + aliases = kwargs.pop('aliases', ()) + # create a pseudo-action to hold the choice help if 'help' in kwargs: help = kwargs.pop('help') - choice_action = self._ChoicesPseudoAction(name, help) + choice_action = self._ChoicesPseudoAction(name, aliases, help) self._choices_actions.append(choice_action) # create the parser and add it to the map parser = self._parser_class(**kwargs) self._name_parser_map[name] = parser + + # make parser available under aliases also + for alias in aliases: + self._name_parser_map[alias] = parser + return parser def _get_subactions(self): |