GH-139067: Add example for argparse's append action (#131389)

This commit is contained in:
Moshe Kaplan 2025-09-17 12:24:20 -04:00 committed by GitHub
parent c72ffe71f1
commit 101fd33065
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -774,16 +774,16 @@ how the command-line arguments should be handled. The supplied actions are:
>>> parser.parse_args('--foo --bar'.split()) >>> parser.parse_args('--foo --bar'.split())
Namespace(foo=True, bar=False, baz=True) Namespace(foo=True, bar=False, baz=True)
* ``'append'`` - This stores a list, and appends each argument value to the * ``'append'`` - This appends each argument value to a list.
list. It is useful to allow an option to be specified multiple times. It is useful for allowing an option to be specified multiple times.
If the default value is non-empty, the default elements will be present If the default value is a non-empty list, the parsed value will start
in the parsed value for the option, with any values from the with the default list's elements and any values from the command line
command line appended after those default values. Example usage:: will be appended after those default values. Example usage::
>>> parser = argparse.ArgumentParser() >>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action='append') >>> parser.add_argument('--foo', action='append', default=['0'])
>>> parser.parse_args('--foo 1 --foo 2'.split()) >>> parser.parse_args('--foo 1 --foo 2'.split())
Namespace(foo=['1', '2']) Namespace(foo=['0', '1', '2'])
* ``'append_const'`` - This stores a list, and appends the value specified by * ``'append_const'`` - This stores a list, and appends the value specified by
the const_ keyword argument to the list; note that the const_ keyword the const_ keyword argument to the list; note that the const_ keyword