Author elsdoerfer
Recipients bethard, elsdoerfer, nvie, r.david.murray
Date 2010-08-10.21:06:19
SpamBayes Score 2.68047e-12
Marked as misclassified No
Message-id <1281474382.32.0.408868822574.issue9253@psf.upfronthosting.co.za>
In-reply-to
Content
To expand on my case from issue9540, I have a bunch of commands, each of which should enable a specific subset of options only available the individual command, but all of the commands share the same behavior in taking nargs='*' positional arguments:

./script.py --global-option command --command-option arg1 arg2 arg3

For example:

./backups.py -c /etc/tarsnap.conf make --no-expire job1 job2

If no positional arguments are given, all jobs defined in the config file are run. Or, in the above example, only "job1" and "job2" are run.

The positional arguments are the same for *all* commands. Now I can define them separately for each subparser, which is what I'm currently doing, but I kind of like having the global usage instructions (script.py -h) indicating the fact that positional arguments can be passed after the command. 

In fact, right now I'm able to sort of achieve this by defining the positional nargs arguments both globally (to have them show in usage) and in each subparser (to have them parsed). This wouldn't be possible anymore if argparse where to throw an error after adding arguments after a subparser, although probably a more correct behavior.

Anyway, while the two issues are clearly related, I don't think that the two are necessarily mutually exclusive. argparse could allow both optional subparsers (if no subparser matches), as well as pass control back to the parent parser once an already matched subparser is no longer able to handle further command line input. Or optionally, support defining subparsers as "options only", so that positional arguments would always be handled by the parent parser.

Now, I can see how this could potentially become messy if we start talking about these positional arguments handled by the parent then being followed by more flags, which would then presumably also be handled by the parent etc. On the other hand, my use case doesn't seem that strange to me.
History
Date User Action Args
2010-08-10 21:06:22elsdoerfersetrecipients: + elsdoerfer, bethard, r.david.murray, nvie
2010-08-10 21:06:22elsdoerfersetmessageid: <1281474382.32.0.408868822574.issue9253@psf.upfronthosting.co.za>
2010-08-10 21:06:20elsdoerferlinkissue9253 messages
2010-08-10 21:06:19elsdoerfercreate