Skip to content

Commit aa32a7e

Browse files
isidenticalmiss-islington
authored andcommitted
bpo-23378: Add an extend action to argparse (GH-13305)
Add an extend action to argparse https://bugs.python.org/issue23378
1 parent d5c120f commit aa32a7e

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

Doc/library/argparse.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,15 @@ how the command-line arguments should be handled. The supplied actions are:
797797
>>> parser.parse_args(['--version'])
798798
PROG 2.0
799799

800+
* ``'extend'`` - This stores a list, and extends each argument value to the
801+
list.
802+
Example usage::
803+
804+
>>> parser = argparse.ArgumentParser()
805+
>>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
806+
>>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
807+
Namespace(foo=['f1', 'f2', 'f3', 'f4'])
808+
800809
You may also specify an arbitrary action by passing an Action subclass or
801810
other object that implements the same interface. The recommended way to do
802811
this is to extend :class:`Action`, overriding the ``__call__`` method

Lib/argparse.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,12 @@ def __call__(self, parser, namespace, values, option_string=None):
11541154
vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, [])
11551155
getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings)
11561156

1157+
class _ExtendAction(_AppendAction):
1158+
def __call__(self, parser, namespace, values, option_string=None):
1159+
items = getattr(namespace, self.dest, None)
1160+
items = _copy_items(items)
1161+
items.extend(values)
1162+
setattr(namespace, self.dest, items)
11571163

11581164
# ==============
11591165
# Type classes
@@ -1262,6 +1268,7 @@ def __init__(self,
12621268
self.register('action', 'help', _HelpAction)
12631269
self.register('action', 'version', _VersionAction)
12641270
self.register('action', 'parsers', _SubParsersAction)
1271+
self.register('action', 'extend', _ExtendAction)
12651272

12661273
# raise an exception if the conflict handler is invalid
12671274
self._get_handler()

Lib/test/test_argparse.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,15 @@ def test(self):
17861786
self.assertEqual(parser.parse_args(['42']), NS(badger='foo[42]'))
17871787

17881788

1789+
class TestActionExtend(ParserTestCase):
1790+
argument_signatures = [
1791+
Sig('--foo', action="extend", nargs="+", type=str),
1792+
]
1793+
failures = ()
1794+
successes = [
1795+
('--foo f1 --foo f2 f3 f4', NS(foo=['f1', 'f2', 'f3', 'f4'])),
1796+
]
1797+
17891798
# ================
17901799
# Subparsers tests
17911800
# ================
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add an extend action to argparser.

0 commit comments

Comments
 (0)