optparse option prefix
Steven Bethard
steven.bethard at gmail.com
Wed Apr 4 11:41:38 EDT 2007
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
More information about the Python-list
mailing list