@@ -690,6 +690,21 @@ how the command-line arguments should be handled. The supplied actions are:
690
690
>>> parser.parse_args('--str --int'.split())
691
691
Namespace(types=[<class 'str'>, <class 'int'>])
692
692
693
+ * ``'extend' `` - This stores a list and appends each item from the multi-value
694
+ argument list to it.
695
+ The ``'extend' `` action is typically used with the nargs _ keyword argument
696
+ value ``'+' `` or ``'*' ``.
697
+ Note that when nargs _ is ``None `` (the default) or ``'?' ``, each
698
+ character of the argument string will be appended to the list.
699
+ Example usage::
700
+
701
+ >>> parser = argparse.ArgumentParser()
702
+ >>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
703
+ >>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
704
+ Namespace(foo=['f1', 'f2', 'f3', 'f4'])
705
+
706
+ .. versionadded :: 3.8
707
+
693
708
* ``'count' `` - This counts the number of times a keyword argument occurs. For
694
709
example, this is useful for increasing verbosity levels::
695
710
@@ -715,17 +730,6 @@ how the command-line arguments should be handled. The supplied actions are:
715
730
>>> parser.parse_args(['--version'])
716
731
PROG 2.0
717
732
718
- * ``'extend' `` - This stores a list, and extends each argument value to the
719
- list.
720
- Example usage::
721
-
722
- >>> parser = argparse.ArgumentParser()
723
- >>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
724
- >>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
725
- Namespace(foo=['f1', 'f2', 'f3', 'f4'])
726
-
727
- .. versionadded :: 3.8
728
-
729
733
Only actions that consume command-line arguments (e.g. ``'store' ``,
730
734
``'append' `` or ``'extend' ``) can be used with positional arguments.
731
735
0 commit comments