From cd5483c03b713df469c2228af9122cc2f2396451 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Thu, 18 Aug 2016 21:23:48 +0100 Subject: Closes #12713: Allowed abbreviation of subcommands in argparse. --- Lib/argparse.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'Lib/argparse.py') diff --git a/Lib/argparse.py b/Lib/argparse.py index 209b4e99c1..e0edad8e42 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1110,6 +1110,12 @@ class _SubParsersAction(Action): parser_name = values[0] arg_strings = values[1:] + # get full parser_name from (optional) abbreviated one + for p in self._name_parser_map: + if p.startswith(parser_name): + parser_name = p + break + # set the parser name if requested if self.dest is not SUPPRESS: setattr(namespace, self.dest, parser_name) @@ -2307,11 +2313,18 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): def _check_value(self, action, value): # converted value must be one of the choices (if specified) - if action.choices is not None and value not in action.choices: - args = {'value': value, - 'choices': ', '.join(map(repr, action.choices))} - msg = _('invalid choice: %(value)r (choose from %(choices)s)') - raise ArgumentError(action, msg % args) + if action.choices is not None: + ac = [ax for ax in action.choices if str(ax).startswith(str(value))] + if len(ac) == 0: + args = {'value': value, + 'choices': ', '.join(map(repr, action.choices))} + msg = _('invalid choice: %(value)r (choose from %(choices)s)') + raise ArgumentError(action, msg % args) + elif len(ac) > 1: + args = {'value': value, + 'choices': ', '.join(ac)} + msg = _('ambiguous choice: %(value)r could match %(choices)s') + raise ArgumentError(action, msg % args) # ======================= # Help-formatting methods -- cgit v1.2.1 From 53b09e11d5c12b544f3ac12c9166b3954c86e414 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Tue, 23 Aug 2016 08:43:16 +0100 Subject: Issue #12713: reverted fix pending further discussion. --- Lib/argparse.py | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) (limited to 'Lib/argparse.py') diff --git a/Lib/argparse.py b/Lib/argparse.py index e0edad8e42..209b4e99c1 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1110,12 +1110,6 @@ class _SubParsersAction(Action): parser_name = values[0] arg_strings = values[1:] - # get full parser_name from (optional) abbreviated one - for p in self._name_parser_map: - if p.startswith(parser_name): - parser_name = p - break - # set the parser name if requested if self.dest is not SUPPRESS: setattr(namespace, self.dest, parser_name) @@ -2313,18 +2307,11 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): def _check_value(self, action, value): # converted value must be one of the choices (if specified) - if action.choices is not None: - ac = [ax for ax in action.choices if str(ax).startswith(str(value))] - if len(ac) == 0: - args = {'value': value, - 'choices': ', '.join(map(repr, action.choices))} - msg = _('invalid choice: %(value)r (choose from %(choices)s)') - raise ArgumentError(action, msg % args) - elif len(ac) > 1: - args = {'value': value, - 'choices': ', '.join(ac)} - msg = _('ambiguous choice: %(value)r could match %(choices)s') - raise ArgumentError(action, msg % args) + if action.choices is not None and value not in action.choices: + args = {'value': value, + 'choices': ', '.join(map(repr, action.choices))} + msg = _('invalid choice: %(value)r (choose from %(choices)s)') + raise ArgumentError(action, msg % args) # ======================= # Help-formatting methods -- cgit v1.2.1