summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/argparse.py7
-rw-r--r--Lib/test/test_argparse.py9
2 files changed, 16 insertions, 0 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 798766f..ef888f0 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1154,6 +1154,12 @@ class _SubParsersAction(Action):
vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, [])
getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings)
+class _ExtendAction(_AppendAction):
+ def __call__(self, parser, namespace, values, option_string=None):
+ items = getattr(namespace, self.dest, None)
+ items = _copy_items(items)
+ items.extend(values)
+ setattr(namespace, self.dest, items)
# ==============
# Type classes
@@ -1262,6 +1268,7 @@ class _ActionsContainer(object):
self.register('action', 'help', _HelpAction)
self.register('action', 'version', _VersionAction)
self.register('action', 'parsers', _SubParsersAction)
+ self.register('action', 'extend', _ExtendAction)
# raise an exception if the conflict handler is invalid
self._get_handler()
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index e849c7b..9d68f40 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -1786,6 +1786,15 @@ class TestActionRegistration(TestCase):
self.assertEqual(parser.parse_args(['42']), NS(badger='foo[42]'))
+class TestActionExtend(ParserTestCase):
+ argument_signatures = [
+ Sig('--foo', action="extend", nargs="+", type=str),
+ ]
+ failures = ()
+ successes = [
+ ('--foo f1 --foo f2 f3 f4', NS(foo=['f1', 'f2', 'f3', 'f4'])),
+ ]
+
# ================
# Subparsers tests
# ================