New submission from Thibault Kruse:
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.
----------
components: Library (Lib)
messages: 182395
nosy: Thibault.Kruse
priority: normal
severity: normal
status: open
title: argparse: subcommand name and arity
type: enhancement
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue17240>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com