Re: Partial option parsing
Greg Ward <[email protected]> Wed, 31 Mar 2004 20:57:58 -0500
| Newsgroups | gmane.comp.python.optik.user |
|---|---|
| Message-ID | <[email protected]> |
On 18 March 2004, Hans-Joachim Widmaier said:
> I have a program which has several subcommands, some generic options
> and options specific to subcommands. I figured the easiest way to
> implement this scheme would be to enhance optparse so that
> OptionParser.parse_args() takes another (optional, of course) argument
> that allows it to treat unknown options as arguments.
> This turned out to be rather easy to implement, and so I did.
The usual answer to this problem is multiple instances of OptionParser.
Each one has all the standard options plus its own options. Probably
the cleanest way to do this is with your own OptionParser subclass, and
instantiate it once for each sub-command:
from optparse import OptionParser
class MyOptionParser(OptionParser):
standard_option_list = [
make_option(...),
...,
]
foo_parser = MyOptionParser()
foo_parser.add_option(...)
bar_parser = MyOptionParser()
bar_parser.add_option(...)
(assuming "foo" and "bar" are your sub-commands).
At least, that should work, and I'm pretty sure there have been people
who have had success doing something like that. I've never done it
myself, though.
> While I was at it, I also added gettext support to the strings
> (i.e. surrounded them with _()) that will be seen by a end user
> (messages to exceptions raised by programmer's fault can stay in english).
Argh, this has been a patch in SF for ages -- I just haven't gotten
around to it. Please take a look at
https://sourceforge.net/tracker/?func=detail&aid=736940&group_id=38019&atid=421097
and see if your patch is better than that one, or if they can be merged,
or if it doesn't matter, or what. The more people pester me about this,
the more likely it is to get done someday. ;-)
Greg
--
Greg Ward <[email protected]> http://www.gerg.ca/
I am deeply CONCERNED and I want something GOOD for BREAKFAST!
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click