classification
Title: argparse: Allow the use of -- to break out of nargs and into subparser
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.2
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: bethard, elsdoerfer, eric.araujo, r.david.murray
Priority: normal Keywords:

Created on 2010-08-11 19:21 by elsdoerfer, last changed 2010-09-20 21:33 by eric.araujo.

Messages (3)
msg113616 - (view) Author: Michael.Elsdörfer (elsdoerfer) Date: 2010-08-11 19:21
argparse already seems to support -- to indicate that what follows are positional arguments. However, I would like to parse something like:

./script.py --ignore one two -- COMMAND

I.e., --ignore is an nargs='+' argument, and I need a way to break out of --ignore and have argparse consider what follows on it's own merits. If COMMAND in the above example refers to a subparser, this won't work:

error: invalid choice: '--' (choose from 'command1', 'command2', 'command3')

I'm not sure what's the best solution here. Allowing -- here would change the semantics of forcing everything that follows to be positional arguments, since the subparser might have flags. I'm not sure if that is what is required by Unix conventions, but if not, then I think it makes sense to allow -- to be followed by a subparser.
msg113647 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-08-12 01:45
It looks like, if accepted, this would be a feature request,so I'm marking it as such and setting versions to 3.2.  You'd have to provide a patch pretty soon to get it in to 3.2, though.

However, I'm guessing that this is something better off implemented via subclassing in your code, since it sounds like a fairly unusual command pattern.  (I've only ever seen options after a -- in a unix comand when those options were being passed unparsed to some *other* command that the first command was a wrapper for.)

I've added Steven as nosy, we'll see what he thinks.
msg113668 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2010-08-12 08:43
This is closely related to issue 9338. The parser should know that your command line requires at least the COMMAND argument, so it should stop parsing in time for that. However, in the case of subcommands, even if we solved issue 9338, you would still get the behavior that

./script.py --ignore one two COMMAND arg1 arg2

would get parsed as "arg2" being the command. So I guess there still ought to be a way to tell argparse to stop parsing nargs='+' optionals.

Seems like there's also a bug in the current behavior - you should get an error saying that no command was given, not an error saying you issued the command "--".
History
Date User Action Args
2010-09-20 21:33:29eric.araujosetnosy: + eric.araujo
2010-08-12 08:43:07bethardsetmessages: + msg113668
2010-08-12 01:45:02r.david.murraysetversions: - Python 3.1, Python 2.7, Python 3.3
nosy: + r.david.murray, bethard

messages: + msg113647

type: behavior -> enhancement
2010-08-11 19:21:25elsdoerfercreate