I have a program, say a debugger, that runs another program which has options of its own. I want people to be able to give options to either program and don't want them to get confused. For example, perhaps both have "--help" options.
So to be a little more concrete, let's say I'd like to run say program (pdb) and have it pass "--help" to a program I want it to run (specified as a command argument) called "debugged-script". I'd like to issue the command like this:
pdb --trace debugged-script --help
and have "--help" go to "debugged-script" and not "pdb".
1 2 3 4 5
optparser = OptionParser() ... optparser.disable_interspersed_args() (opts, argv) = optparser.parse_args() ## argv now has the options to pass to the second program
Another convention that has been used such as in the X11 "startx" command is to use "--" to separate the two sets of options. However this isn't as desirable as the simple syntax described above.
In the above you may want to modify sys.argv after you've done the first round of processing. Here the code would be:
import sys argv_start = sys.argv optparser = OptionParser() ... optparser.disable_interspersed_args() (opts, sys.argv) = optparser.parse_args() sys.argv[:0] = argv_start