optparse question, passing unknown flags to subprocess

Joseph Garvin joseph.h.garvin at gmail.com
Wed May 20 17:50:32 EDT 2009


I'm working on a python script that takes several command line flags,
currently parsed by hand. I'd like to change the script to parse them
with OptionParser from the optparse module. However, currently the
script invokes a subprocess, and any flags the script doesn't
understand it assumes are meant to be passed to the subprocess. But if
I switch to using OptionParser, any options I haven't added to my
parser will cause an error, instead of it ignoring those and letting
me pass them onto the subprocess.

What's the best/most-pythonic way to handle this? I could subclass
OptionParser and override its exit() and error() methods as suggested
by the docs (http://www.python.org/doc/2.4/lib/optparse-how-optik-handles-errors.html)
and have them do nothing, but there are some errors I probably don't
want to ignore (like if the user tries to pass a string to a known
flag that takes an int).



More information about the Python-list mailing list