This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: argparse.add_argument action parameter should allow value extend
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, gvanrossum, miss-islington, paul.j3, the.mulhern, wolma
Priority: normal Keywords: patch

Created on 2015-02-02 14:23 by the.mulhern, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 13305 merged BTaskaya, 2019-05-14 02:41
Messages (5)
msg235260 - (view) Author: the mulhern (the.mulhern) Date: 2015-02-02 14:23
As well as the append action it would be convenient for there to be an extend action.

This is kind of useful to allow something like:

parser.add_argument("--foo", action="extend", nargs="+", type=str)


parser.parse_args("--foo f1 --foo f2 f3 f4".split())

to result in

["f1", "f2", "f3", "f4"].

The action "append" results in

[["f1"], ["f2", "f3", "f4"]]

And action store in

["f2", "f3", "f4"].

It is easy to write a custom action, but it feels like a fairly common requirement.

Probably it would make sense to extend the default, similarly to how append behaves.
msg238936 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-03-22 20:21
My opinion is that this is an unnecessary addition.


- it is easy to add as a custom action class

- I haven't seen other requests for it

- the append retains information about the argument strings that extend looses

- it is easy flatten the appended attribute, e.g with a simple loop or a flattener like 'list(itertools.chain(*ll))'
msg238942 - (view) Author: Wolfgang Maier (wolma) * Date: 2015-03-22 21:19
> - I haven't seen other requests for it

For the record, an Extend custom action class is one of very few such classes I have ever written for argparse for exactly the OP's usecase, i.e., it is useful for any parser that should accept the same option multiple times, while accepting multiple arguments each time.
It *is* very simple to implement (and that's why I never asked for it as a feature), but only once you've understood custom action classes, which are not exactly for beginners.
All in all, I'm +1 on the request.
msg342952 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-05-20 20:29
I've felt the need for this myself.
msg343076 - (view) Author: miss-islington (miss-islington) Date: 2019-05-21 17:47
New changeset aa32a7e1116f7aaaef9fec453db910e90ab7b101 by Miss Islington (bot) (Batuhan Taşkaya) in branch 'master':
bpo-23378: Add an extend action to argparse (GH-13305)
Date User Action Args
2022-04-11 14:58:12adminsetgithub: 67567
2019-05-21 17:53:34gvanrossumsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-05-21 17:47:45miss-islingtonsetnosy: + miss-islington
messages: + msg343076
2019-05-20 20:29:04gvanrossumsetnosy: + gvanrossum
messages: + msg342952
2019-05-14 10:36:02matrixisesetversions: + Python 3.8, - Python 3.5
2019-05-14 02:41:32BTaskayasetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request13215
2015-03-23 06:15:46berker.peksagsetnosy: + berker.peksag
stage: needs patch

versions: - Python 3.4, Python 3.6
2015-03-22 21:19:46wolmasetnosy: + wolma
messages: + msg238942
2015-03-22 20:21:12paul.j3setnosy: + paul.j3
messages: + msg238936
2015-02-02 14:23:10the.mulherncreate