| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d5c120f commit aa32a7e
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -797,6 +797,15 @@ how the command-line arguments should be handled. The supplied actions are: | |||
| 797 | 797 | >>> parser.parse_args(['--version']) | |
| 798 | 798 | PROG 2.0 | |
| 799 | 799 | ||
| 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 | + | ||
| 800 | 809 | You may also specify an arbitrary action by passing an Action subclass or | |
| 801 | 810 | other object that implements the same interface. The recommended way to do | |
| 802 | 811 | this is to extend :class:`Action`, overriding the ``__call__`` method | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1154,6 +1154,12 @@ def __call__(self, parser, namespace, values, option_string=None): | |||
| 1154 | 1154 | vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, []) | |
| 1155 | 1155 | getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings) | |
| 1156 | 1156 | ||
| 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) | ||
| 1157 | 1163 | ||
| 1158 | 1164 | # ============== | |
| 1159 | 1165 | # Type classes | |
@@ -1262,6 +1268,7 @@ def __init__(self, | |||
| 1262 | 1268 | self.register('action', 'help', _HelpAction) | |
| 1263 | 1269 | self.register('action', 'version', _VersionAction) | |
| 1264 | 1270 | self.register('action', 'parsers', _SubParsersAction) | |
| 1271 | + self.register('action', 'extend', _ExtendAction) | ||
| 1265 | 1272 | ||
| 1266 | 1273 | # raise an exception if the conflict handler is invalid | |
| 1267 | 1274 | self._get_handler() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1786,6 +1786,15 @@ def test(self): | |||
| 1786 | 1786 | self.assertEqual(parser.parse_args(['42']), NS(badger='foo[42]')) | |
| 1787 | 1787 | ||
| 1788 | 1788 | ||
| 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 | + | ||
| 1789 | 1798 | # ================ | |
| 1790 | 1799 | # Subparsers tests | |
| 1791 | 1800 | # ================ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + Add an extend action to argparser. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments