argparse missing optparse capabilities?

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Thu Jan 5 04:19:22 EST 2012


Am 05.01.2012 09:05, schrieb rurpy at yahoo.com:
> I have optparse code that parses a command line containing
> intermixed positional and optional arguments, where the optional
> arguments set the context for the following positional arguments.
> For example,
>
>    myprogram.py arg1 -c33 arg2 arg3 -c44 arg4
>
> 'arg1' is processed in a default context, 'args2' and 'arg3' in
> context '33', and 'arg4' in context '44'.

Question: How would you e.g. pass the string "-c33" as first argument, 
i.e. to be parsed in the default context?

The point is that you separate the parameters in a way that makes it 
possible to parse them in a way that works 100%, not just a way that 
works in 99% of all cases. For that reason, many commandline tools 
accept "--" as separator, so that "cp -- -r -x" will copy the file "-r" 
to the folder "-x". In that light, I would consider restructuring your 
commandline.

> I am trying to do the same using argparse but it appears to be
> not doable in a documented way.

As already hinted at, I don't think this is possible and that that is so 
by design.

Sorry..

Uli



More information about the Python-list mailing list