Re: Gathering unknown option data without quitting
Greg Ward <[email protected]> Fri, 05 May 2006 20:42:10 -0400
| Newsgroups | gmane.comp.python.optik.user |
|---|---|
| Message-ID | <[email protected]> |
On 03 May 2006, Allan Crooks said:
> I would like to do something like this:
>
> def setup(func):
> configure parser
> options, args, unknown_options = parser.parse_args()
> environment.handle(options)
> func(environment, unknown_options)
>
> At the moment, you can't pass any options intended for the func
> function, since it is rejected as an unknown option for the parser
> created by the setup function.
>
> Has anyone written something to extend optik to do this?
Not me! I added a smart-ass remark to the docs years ago to the effect
that "if you don't like the fact that Optik terminates your process on
error, you'll need to subclass OptionParser and override error()". I
think you're the first person to actually take me up on this.
> I think I could
> probably write something to get what I want, but I'm just wondering
> whether I could reuse existing code rather than writing something myself.
On reflection, and looking at the code again, I think I would start by
overriding _process_args() rather than error(). You want to call the
real _process_args(), but catch BadOptionError. (You probably want to
let OptionValueError pass up and be handled by the "except" clause in
parse_args().) Then accumulate the bad options in a list somewhere. Or
put them in the parser's 'largs' variable, if you don't need to
distinguish between leftover positional args and bad options.
I thikn what's going to make this difficult is dealing with "-ffoo" or
"--file=foo" where "foo" is the argument to -f/--file. Good luck, and
don't forget: Use The Source, Luke.
Greg
--
Greg Ward <[email protected]> http://www.gerg.ca/
I brought my BOWLING BALL -- and some DRUGS!!
-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642