@@ -774,16 +774,16 @@ how the command-line arguments should be handled. The supplied actions are:
774
774
>>> parser.parse_args('--foo --bar'.split())
775
775
Namespace(foo=True, bar=False, baz=True)
776
776
777
- * ``'append' `` - This stores a list, and appends each argument value to the
778
- list. It is useful to allow an option to be specified multiple times.
779
- If the default value is non-empty, the default elements will be present
780
- in the parsed value for the option, with any values from the
781
- command line appended after those default values. Example usage::
777
+ * ``'append' `` - This appends each argument value to a list.
778
+ It is useful for allowing an option to be specified multiple times.
779
+ If the default value is a non-empty list , the parsed value will start
780
+ with the default list's elements and any values from the command line
781
+ will be appended after those default values. Example usage::
782
782
783
783
>>> parser = argparse.ArgumentParser()
784
- >>> parser.add_argument('--foo', action='append')
784
+ >>> parser.add_argument('--foo', action='append', default=['0'] )
785
785
>>> parser.parse_args('--foo 1 --foo 2'.split())
786
- Namespace(foo=['1', '2'])
786
+ Namespace(foo=['0', ' 1', '2'])
787
787
788
788
* ``'append_const' `` - This stores a list, and appends the value specified by
789
789
the const _ keyword argument to the list; note that the const _ keyword
@@ -981,8 +981,8 @@ the various :class:`ArgumentParser` actions. The two most common uses of it are
981
981
(like ``-f `` or ``--foo ``) and ``nargs='?' ``. This creates an optional
982
982
argument that can be followed by zero or one command-line arguments.
983
983
When parsing the command line, if the option string is encountered with no
984
- command-line argument following it, the value of ``const `` will be assumed to
985
- be `` None `` instead. See the nargs _ description for examples.
984
+ command-line argument following it, the value from ``const `` will be used.
985
+ See the nargs _ description for examples.
986
986
987
987
.. versionchanged :: 3.11
988
988
``const=None `` by default, including when ``action='append_const' `` or
@@ -1142,16 +1142,21 @@ if the argument was not one of the acceptable values::
1142
1142
game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
1143
1143
'paper', 'scissors')
1144
1144
1145
- Note that inclusion in the *choices * sequence is checked after any type _
1146
- conversions have been performed, so the type of the objects in the *choices *
1147
- sequence should match the type _ specified.
1148
-
1149
1145
Any sequence can be passed as the *choices * value, so :class: `list ` objects,
1150
1146
:class: `tuple ` objects, and custom sequences are all supported.
1151
1147
1152
1148
Use of :class: `enum.Enum ` is not recommended because it is difficult to
1153
1149
control its appearance in usage, help, and error messages.
1154
1150
1151
+ Note that *choices * are checked after any type _
1152
+ conversions have been performed, so objects in *choices *
1153
+ should match the type _ specified. This can make *choices *
1154
+ appear unfamiliar in usage, help, or error messages.
1155
+
1156
+ To keep *choices * user-friendly, consider a custom type wrapper that
1157
+ converts and formats values, or omit type _ and handle conversion in
1158
+ your application code.
1159
+
1155
1160
Formatted choices override the default *metavar * which is normally derived
1156
1161
from *dest *. This is usually what you want because the user never sees the
1157
1162
*dest * parameter. If this display isn't desirable (perhaps because there are
0 commit comments