optparse option prefix

M

Mathias Waack

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?

Regards
Mathias
 
S

Steven D'Aprano

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

Diez B. Roggisch

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

That's not even necessary, the optparser will work on a passed argument
list. No need to alter sys.argv.

Diez
 
M

Mathias Waack

Diez said:
That's not even necessary, the optparser will work on a passed argument
list. No need to alter sys.argv.

Sounds nice, I'll do so. Thanks!

Mathias
 
S

Steven Bethard

Mathias said:
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?

If your arguments come in a particular order, you could use argparse
(http://argparse.python-hosting.com/) to parse them as positional
arguments::
Namespace(bar=['BAR1', 'BAR2', 'BAR3'], baz='BAZ', foo='FOO')

I've also filed a feature request for you asking for options with '+' as
a prefix:

http://argparse.python-hosting.com/ticket/30

I'll see if I can make some time to implement it, but if you'd like to
take a look yourself, I can see '-' being hard-coded in at least:

add_argument()
_get_optional_kwargs()
consume_optional() within _parse_args()
_parse_optional()
_get_option_prefix_tuples()

I guess there ought to be an easy way to customize the prefix
characters... Maybe something like::

parser = argparse.ArgumentParser(prefix_chars='+')

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top