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.

classification
Title: argparse: subcommand name and arity
Type: enhancement Stage:
Components: Library (Lib) Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Thibault.Kruse, chris.jerdonek
Priority: normal Keywords:

Created on 2013-02-19 16:51 by Thibault.Kruse, last changed 2022-04-11 14:57 by admin.

Messages (2)
msg182395 - (view) Author: Thibault Kruse (Thibault.Kruse) Date: 2013-02-19 16:51
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.
msg182406 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2013-02-19 18:45
> This is not satisfactory. I would prefer:
> import argparse
> argparser = argparse.ArgumentParser()
> subparsers = argparser.add_subparsers('cmd1') % name here

Have you tried passing by keyword?

subparsers = argparser.add_subparsers(dest='cmd1')

It seems to work.  I observed something similar for the metavar parameter on issue 14039.

> 'A...'
> Does anyone know what that notation implies?

I could be wrong, but I think this is just an arbitrary string for internal use.
History
Date User Action Args
2022-04-11 14:57:42adminsetgithub: 61442
2013-02-19 18:45:20chris.jerdoneksetnosy: + chris.jerdonek
messages: + msg182406
2013-02-19 16:51:08Thibault.Krusecreate