Commit aa32a7e1 authored by Batuhan Taşkaya's avatar Batuhan Taşkaya Committed by Miss Islington (bot)

bpo-23378: Add an extend action to argparse (GH-13305)



Add an extend action to argparse


https://bugs.python.org/issue23378
parent d5c120f7
...@@ -797,6 +797,15 @@ how the command-line arguments should be handled. The supplied actions are: ...@@ -797,6 +797,15 @@ how the command-line arguments should be handled. The supplied actions are:
>>> parser.parse_args(['--version']) >>> parser.parse_args(['--version'])
PROG 2.0 PROG 2.0
* ``'extend'`` - This stores a list, and extends each argument value to the
list.
Example usage::
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
>>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
Namespace(foo=['f1', 'f2', 'f3', 'f4'])
You may also specify an arbitrary action by passing an Action subclass or You may also specify an arbitrary action by passing an Action subclass or
other object that implements the same interface. The recommended way to do other object that implements the same interface. The recommended way to do
this is to extend :class:`Action`, overriding the ``__call__`` method this is to extend :class:`Action`, overriding the ``__call__`` method
......
...@@ -1154,6 +1154,12 @@ class _SubParsersAction(Action): ...@@ -1154,6 +1154,12 @@ class _SubParsersAction(Action):
vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, []) vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, [])
getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings) 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 # Type classes
...@@ -1262,6 +1268,7 @@ class _ActionsContainer(object): ...@@ -1262,6 +1268,7 @@ class _ActionsContainer(object):
self.register('action', 'help', _HelpAction) self.register('action', 'help', _HelpAction)
self.register('action', 'version', _VersionAction) self.register('action', 'version', _VersionAction)
self.register('action', 'parsers', _SubParsersAction) self.register('action', 'parsers', _SubParsersAction)
self.register('action', 'extend', _ExtendAction)
# raise an exception if the conflict handler is invalid # raise an exception if the conflict handler is invalid
self._get_handler() self._get_handler()
......
...@@ -1786,6 +1786,15 @@ class TestActionRegistration(TestCase): ...@@ -1786,6 +1786,15 @@ class TestActionRegistration(TestCase):
self.assertEqual(parser.parse_args(['42']), NS(badger='foo[42]')) 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 # Subparsers tests
# ================ # ================
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment