[New-bugs-announce] [issue17240] argparse: subcommand name and arity

Thibault Kruse report at bugs.python.org
Tue Feb 19 17:51:08 CET 2013


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 <report at bugs.python.org>
<http://bugs.python.org/issue17240>
_______________________________________


More information about the New-bugs-announce mailing list