467,920 Members | 1,192 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,920 developers. It's quick & easy.

optparse option prefix

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
Apr 4 '07 #1
  • viewed: 1422
Share:
4 Replies
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

Apr 4 '07 #2
>
(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
Apr 4 '07 #3
Diez B. Roggisch wrote:
>(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.
Sounds nice, I'll do so. Thanks!

Mathias
Apr 4 '07 #4
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?
If your arguments come in a particular order, you could use argparse
(http://argparse.python-hosting.com/) to parse them as positional
arguments::
>>parser = argparse.ArgumentParser()
parser.add_argument('foo')
parser.add_argument('bar', nargs='+')
parser.add_argument('baz')
parser.parse_args('FOO BAR1 BAR2 BAR3 BAZ'.split())
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
Apr 4 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Hans-Joachim Widmaier | last post: by
5 posts views Thread by GMTaglia | last post: by
7 posts views Thread by Henry Ludemann | last post: by
1 post views Thread by sector119 | last post: by
7 posts views Thread by R. Bernstein | last post: by
3 posts views Thread by Dan | last post: by
reply views Thread by Robert Kern | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.