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.

Author Thibault.Kruse
Recipients Thibault.Kruse
Date 2013-02-19.16:51:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1361292668.26.0.464460633083.issue17240@psf.upfronthosting.co.za>
In-reply-to
Content
I realize there have been several suggestions around argparse subcommands. Mine is related to this isse:
http://bugs.python.org/issue9253

In short, I suggest that the add_subparsers() function take an argument like nargs that determines how many times user may or have to use a subcommand. E.g. ?, 1, +, *. The multiples are useful for things like "setup.py build bist upload" or "make build test doc". I notice currently the subparsers object created by add_subparsers() has this:
>>> subparsers.nargs
'A...'
Does anyone know what that notation implies?

The default for nargs can be whatever it currently is, though issue9253 makes me wonder whether there currently is any default behavior over different argparse versions.

Also, I believe subcommands should have a name by which the choice of the subcommands ends up in the arg namespace.

E.g.:
import argparse
argparser = argparse.ArgumentParser()
subparsers = argparser.add_subparsers()
subparser1 = subparsers.add_parser('foo')
subparser1.add_argument('--fooopt')
subparser2 = subparsers.add_parser('bar')
subparser2.add_argument('--baropt')
argparser.parse_args(['foo'])
Namespace(fooopt=None)

This is not satisfactory. I would prefer:
import argparse
argparser = argparse.ArgumentParser()
subparsers = argparser.add_subparsers('cmd1') % name here

subparser1 = subparsers.add_parser('foo')
subparser1.add_argument('--fooopt')
subparser2 = subparsers.add_parser('bar')
subparser2.add_argument('--baropt')
argparser.parse_args(['foo'])
Namespace(fooopt=None, cmd1='foo') % value here


The dest member of subparsers already seems to work as intended:
subparsers.dest='cmd1'; but users should not have to set it like that, to improve early error checking.
History
Date User Action Args
2013-02-19 16:51:08Thibault.Krusesetrecipients: + Thibault.Kruse
2013-02-19 16:51:08Thibault.Krusesetmessageid: <1361292668.26.0.464460633083.issue17240@psf.upfronthosting.co.za>
2013-02-19 16:51:08Thibault.Kruselinkissue17240 messages
2013-02-19 16:51:07Thibault.Krusecreate