From 6e0a2fafd55ba1c611e14bc6b2a967a6c5d763e4 Mon Sep 17 00:00:00 2001 From: Moshe Kaplan Date: Mon, 17 Mar 2025 22:08:02 -0400 Subject: [PATCH 1/2] Add example for `argparse`'s `append` action Add an example for `argparse`'s `append` action when a default value is provided. --- Doc/library/argparse.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 79e15994491eff..472b6fa3c713f1 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -784,6 +784,10 @@ how the command-line arguments should be handled. The supplied actions are: >>> parser.add_argument('--foo', action='append') >>> parser.parse_args('--foo 1 --foo 2'.split()) Namespace(foo=['1', '2']) + >>> parser = argparse.ArgumentParser() + >>> parser.add_argument('--foo', action='append', default=['0']) + >>> parser.parse_args('--foo 1 --foo 2'.split()) + Namespace(foo=['0', '1', '2']) * ``'append_const'`` - This stores a list, and appends the value specified by the const_ keyword argument to the list; note that the const_ keyword From 1866873ccdeae8233d6cb69907d009f789df85c8 Mon Sep 17 00:00:00 2001 From: Moshe Kaplan Date: Sun, 31 Aug 2025 21:14:50 -0400 Subject: [PATCH 2/2] Update argparse.rst Reduces to a single example (with default arg) and clarifies phrasing. --- Doc/library/argparse.rst | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 472b6fa3c713f1..125b7627ba1e6e 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -774,16 +774,12 @@ how the command-line arguments should be handled. The supplied actions are: >>> parser.parse_args('--foo --bar'.split()) Namespace(foo=True, bar=False, baz=True) -* ``'append'`` - This stores a list, and appends each argument value to the - list. It is useful to allow an option to be specified multiple times. - If the default value is non-empty, the default elements will be present - in the parsed value for the option, with any values from the - command line appended after those default values. Example usage:: +* ``'append'`` - This appends each argument value to a list. + It is useful for allowing an option to be specified multiple times. + If the default value is a non-empty list, the parsed value will start + with the default list's elements and any values from the command line + will be appended after those default values. Example usage:: - >>> parser = argparse.ArgumentParser() - >>> parser.add_argument('--foo', action='append') - >>> parser.parse_args('--foo 1 --foo 2'.split()) - Namespace(foo=['1', '2']) >>> parser = argparse.ArgumentParser() >>> parser.add_argument('--foo', action='append', default=['0']) >>> parser.parse_args('--foo 1 --foo 2'.split())