From ea233967a17d6a8bbe55ce9f4f635e0107d24577 Mon Sep 17 00:00:00 2001 From: Dmitry Voropaev <144015565+v0ropaev@users.noreply.github.com> Date: Mon, 28 Sep 2026 19:03:14 +0300 Subject: [PATCH] gh-110131: Document how a non-empty default works with append and extend (GH-158155) (cherry picked from commit 70bc048e5cc5ae28663381bdc906292b5c5bf334) Co-authored-by: Dmitry Voropaev <144015565+v0ropaev@users.noreply.github.com> --- Doc/library/argparse.rst | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index 790d7564aafa64..ba333e497bde16 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -690,9 +690,9 @@ how the command-line arguments should be handled. The supplied actions are: * ``'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:: + If the default value is a non-empty list, the parsed value for the option + 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', default=['0']) @@ -717,12 +717,16 @@ how the command-line arguments should be handled. The supplied actions are: value ``'+'`` or ``'*'``. Note that when nargs_ is ``None`` (the default) or ``'?'``, each character of the argument string will be appended to the list. + If the default value is a non-empty list, the parsed value for the option + 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="extend", nargs="+", type=str) + >>> parser.add_argument("--foo", action="extend", nargs="+", type=str, + ... default=["d1"]) >>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"]) - Namespace(foo=['f1', 'f2', 'f3', 'f4']) + Namespace(foo=['d1', 'f1', 'f2', 'f3', 'f4']) .. versionadded:: 3.8