[Python-ideas] Collection type argument for argparse where nargs != None
Brett Cannon
brett at python.org
Fri Aug 4 12:33:49 EDT 2017
I'm not a heavy argparse user so take my opinion with a grain of salt (and
I do appreciate the time you put into proposing this), but I'm not seeing
the usefulness to classify this as so pragmatic as to outweigh adding one
more thing to explain about argparse. Since you're proposing just having a
callable to use after constructing the list couldn't you just do e.g.
`args.stuff = frozenset(args.stuff)` instead and just be explicit about it?
On Fri, Aug 4, 2017, 06:01 David Mayo, <pobocks at gmail.com> wrote:
> A friend of mine (@bcjbcjbcj on twitter) came up with an idea for an
> argparse improvement that I'd like to propose for inclusion.
>
> 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.
>
> I've worked up a set of examples in this gist:
> https://gist.github.com/pobocks/bff0bea494f2b7ec7eba1e8ae281b888
>
> And a rough implementation here:
> https://github.com/python/cpython/compare/master...pobocks:argparse_colltype
>
> 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.
>
> Thank you all for your time in considering this,
>
> - Dave Mayo
> @pobocks on twitter, github, various others
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170804/9e29397c/attachment.html>
More information about the Python-ideas
mailing list