Re: parsing partial options
Greg Ward <[email protected]> Sat, 13 Nov 2004 16:37:13 -0500
| Newsgroups | gmane.comp.python.optik.user |
|---|---|
| Message-ID | <[email protected]> |
On 12 November 2004, Thomas Heller said:
> I have a script which takes an awful lot of options, some for the script
> itself, the others should be collected together and passed to an
> external command that the script calls.
Depends on exactly what syntax you want. One fairly well-established
Unix convention is this:
myprog -a -b --option -- -x -y --other-option
where "-a", "-b", and "--option" are for myprog, and the stuff after
"--" is passed on as-is to the sub-program. This is how xinit allows
you to pass options to the X server it launches, for example. It's fine
if your users are quite aware that myprog is launching some other
program. If that's an implementation detail that you'd rather hide,
then you probably want this syntax:
myprog -a -x -b -y --option --other-option
where -a, -b, and --option are processed by myprog, and the rest are
passed on to your sub-program.
Optik doesn't really support this directly. Obviously, you could define
-x, -y, and --other-option in myprog's OptionParser, but that will get
tedious if you have a lot of them. Something like this *should* work:
class MyOptionParser(optik.OptionParser):
def _process_long_opt(self, rargs, values):
next_arg = rargs[0]
try:
optik.OptionParser._process_long_opt(self, rargs, values)
except BadOptionError:
self.largs.append(next_arg)
def _process_short_opts(self, rargs, values):
try:
optik.OptionParser._process_short_opts(self, rargs, values)
except BadOptionError:
self.largs.append(?)
but there are a couple of problems here:
* _process_short_opts() *should* raise BadOptionError when it sees
a bad option, but it doesn't -- it just calls self.error()
(this is inconsistent with _process_long_opts())
* BadOptionError doesn't explicitly include the offending option
(you would have to parse it out of the str() of the exception option
-- ugh)
* if the offending argument is "--option=value", I'm not entirely
sure what will happen to the "=value" bit here
* if the offending option is "-b" in an argument like "-ab", I'm
not entirely sure if this will work (it would have a fighting chance
if _process_short_opts() and BadOptionError were better designed
and implemented)
I'll throw together a test program and see if I can't use it to correct
some of these design errors.
Greg
--
Greg Ward <[email protected]> http://www.gerg.ca/
Question authority!
-------------------------------------------------------
This SF.Net email is sponsored by: InterSystems CACHE
FREE OODBMS DOWNLOAD - A multidimensional database that combines
robust object and relational technologies, making it a perfect match
for Java, C++,COM, XML, ODBC and JDBC. www.intersystems.com/match8