Welcome, guest | Sign In | My Account | Store | Cart
1

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".

Python, 5 lines
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[0] optparser = OptionParser() ... optparser.disable_interspersed_args() (opts, sys.argv) = optparser.parse_args() sys.argv[:0] = argv_start

Add a comment

Sign in to comment

Created by Rocky Bernstein on Thu, 26 Jan 2006 (PSF)
Python recipes (4258)
Rocky Bernstein's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks