summaryrefslogtreecommitdiffstats
path: root/Lib/argparse.py
diff options
context:
space:
mode:
authorVinay Sajip <vinay_sajip@yahoo.co.uk>2016-08-18 20:23:48 (GMT)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>2016-08-18 20:23:48 (GMT)
commitef948cd058180a2fb38d792c912c9b14d190d6c7 (patch)
tree58a73f353df03109da2d307e3ba4d972c1999a6e /Lib/argparse.py
parent97c1adf3935234da716d3289b85f72dcd67e90c2 (diff)
downloadcpython-ef948cd058180a2fb38d792c912c9b14d190d6c7.zip
cpython-ef948cd058180a2fb38d792c912c9b14d190d6c7.tar.gz
cpython-ef948cd058180a2fb38d792c912c9b14d190d6c7.tar.bz2
Closes #12713: Allowed abbreviation of subcommands in argparse.
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r--Lib/argparse.py23
1 files changed, 18 insertions, 5 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 209b4e9..e0edad8 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