<div dir="ltr"><div><div>A friend of mine (@bcjbcjbcj
on twitter) came up with an idea for an argparse improvement that I'd
like to propose for inclusion.
<br><br>Currently, argparse with nargs=<anything but None> collects
arguments into a list (or a list of lists in the case of
action="append"). I would like to propose adding a "collection type"
argument to the store and append actions and to add_argument,
consisting of a callable that would be applied to the list of
type-converted args before adding them to the Namespace. This would
allow for alternate constructors (e.g. set), for modifying the list
(e.g. with sorted), or to do checking of properties expected across all
components of the argument at parse time.
<br><br>I've worked up a set of examples in this gist: <a href="https://gist.github.com/pobocks/bff0bea494f2b7ec7eba1e8ae281b888" target="_blank">https://gist.github.com/<wbr>pobocks/<wbr>bff0bea494f2b7ec7eba1e8ae281b8<wbr>88</a><br><br>And a rough implementation here: <a href="https://github.com/python/cpython/compare/master...pobocks:argparse_colltype" target="_blank">https://github.com/python/<wbr>cpython/compare/master...<wbr>pobocks:argparse_colltype</a><br><br>I think this would be genuinely useful, and would require very little
change to argparse, which should be backwards compatible provided that
the default for the collection type is list, or None with list specified
if None.
<br><br>Thank you all for your time in considering this,
<br><br>- Dave Mayo<br></div>@pobocks on twitter, github, various others<br></div></div>