473,395 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
4 1633
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sam Smith | last post by:
I am using optparse for the commandline parsing for my programs. I was wondering if it is possible to detect if an option or option-arg has been specified on the commandline by the user or not....
8
by: Hans-Joachim Widmaier | last post by:
I was really pleased when the Optik module found its way into the standard Python battery compartment, as it matched all my option parsing requirements. But, as always, there's really nothing that...
5
by: GMTaglia | last post by:
Hi guys, I was wondering why optparse accept an option if in the command line is not *exactly* the one present in the source code....may be an example will explain better.... #!/usr/bin/env...
7
by: Henry Ludemann | last post by:
I've been writing an optparse alternative (using getopt) that is at a stage where I'd be interested in people's opinions. It allows you to easily creating command line interfaces to existing...
1
by: sector119 | last post by:
Hi I use optparse with callback action, my callback function return some value, but optparse does not store this value, options.callback_dest always is None. How can I store callback function...
7
by: R. Bernstein | last post by:
optparse is way cool, far superior and cleaner than other options processing libraries I've used. In the next release of the Python debugger revision, I'd like to add debugger options: --help...
1
by: Pupeno | last post by:
Hello, I am doing some extreme use of optparse, that is, extending it as explained on http://docs.python.org/lib/optparse-other-reasons-to-extend-optparse.html I have subclassed OptionParser and...
3
by: Dan | last post by:
I've been using optparse for a while, and I have an option with a number of sub-actions I want to describe in the help section: parser.add_option("-a", "--action", help=\ """Current supported...
0
by: Robert Kern | last post by:
Jeff Keasler wrote: If you code it up with unit tests and documentation, it has a good chance. But in the meantime, you can tell optparse to stop processing options using the standard "--"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.