optparse option prefix

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Wed Apr 4 05:25:56 EDT 2007


On Wed, 04 Apr 2007 08:52:48 +0000, Mathias Waack wrote:

> We've integrated python into a legacy application. Everything works fine (of
> course because its python;). There's only one small problem: the
> application reads the commandline and consumes all arguments prefixed with
> a '-' sign. Thus its not possible to call a python module from the
> commandline with a parameter list containing options prefixed by '-'
> or '--' signs. Thats not a major problem, but it prevents us from using th
> optparse module. Is there a way to change to prefix, so one could use a '+'
> (for instance) to mark command line options for optparse?

You have the source code. Does it look like the "-" is hard-coded in the
module?

Some solutions:

(1) Fork the code. It's open source, you should be able to copy the code
into a new module while still obeying the licence. (Note: open source does
not mean "I can do anything I want". There is still a licence, but it is a
very user-friendly licence. Read it and obey it.) Duplicate the file and
change all the relevant "-" signs to "+" signs.


(2) Are you sure you need to be using optparse? It sounds like an very
non-standard use of the module to me. Perhaps there is a simpler
alternative.


(3) Create a filter module that reads sys.argv, replaces leading "+" signs
with "-" signs, and then stuffs it back into sys.argv before optparse gets
to see it.


-- 
Steven D'Aprano 




More information about the Python-list mailing list