473,699 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perceived optparse shortcomings

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 does
all you ever want, especially when it comes to option parsing -
there's just too many schemes to handle them all comfortably.

With this program I'm working on, I started to recognize that not only
the functionality should be delegated to modules, but that most
command line options are private to these modules (call 'em plug-ins)
as well. This calls for a command line like CVS or subverion employ:

<cmd> <generic options> <subcmd> <subcmd options> <args>

There are several ways to do it:
- Let one option parser recognize all options. No drop-in plug-ins,
no possibility for the same options meaning different things in
different plug-ins (no, this isn't neccessarily a bad design anyway).
Obviously horrible.
- Don't allow interspersed arguments. This is something I dislike. I
want to be able to type a forgotten generic option when I'm at the end
of a possibly long line. Even with modern shells its a nuisance to
have to go to the line's beginning, skip the command and insert the
option.
- Augment optparse to just ignore unknown options and treat them as
arguments. Doesn't allow clustering of generic and subcommand-specific
options, doesn't allow for generic options to be arguments to
subcommand-specific options, but these seem as minor points to me. So
I did that. And I'd like to ask if this
might have a chance to get into the standard optparse module (It's
just a few lines).

The bigger problem, though, is internationaliz ation. I don't care for
all the exceptions raised by programmer errors, but the usage, help
and user-visible error messages should really be inernationalize d.
Subclassing optparse doesn't help much here, as the strings are
sprinkled all over. Is it even thinkable that optparse (or, in the
end, the whole standard library) be prepared for internationaliz ation?
Couldn't it be handled like that:

At the module's top:

import __builtin__
try:
__builtin__._
except AttributeError:
def _(s):
return s

and surrounding all relevant strings with _()?

Having all programs only talk english or having to rewrite every
module certainly can't be the answer.

Deutsch labernd der Eure,
Hans-Joachim Widmaier
Jul 18 '05 #1
8 2305
On 16 Mar 2004 23:20:10 -0800,
hj********@web. de (Hans-Joachim Widmaier) wrote:
With this program I'm working on, I started to recognize that
not only the functionality should be delegated to modules, but
that most command line options are private to these modules
(call 'em plug-ins) as well. This calls for a command line like
CVS or subverion employ: <cmd> <generic options> <subcmd> <subcmd options> <args> There are several ways to do it:


[ snip ]

How about having your plugins "register" with your main program
*before* you call optparse? Part of the registration would be
something like "I (the plugin) accespt these options with/without
these arguments, and here are some callbacks to handle them."
During registration, build up the argument strings/arrays/etc. you
need for optparse from that information.

I've not used optparse, but I've used that scheme with getopt.

If you allow interspersed options, you (your main program) will
have to handle conflicts gracefully (i.e., plugin1 and plugin2
both want an "-a" option).

Regards,
Heather

--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli
Jul 18 '05 #2
>> <cmd> <generic options> <subcmd> <subcmd options> <args>

Heather> How about having your plugins "register" with your main program
Heather> *before* you call optparse? ....

Heather> I've not used optparse, but I've used that scheme with getopt.

I was thinking that with getopt I would call it once to process the generic
options. That would leave the args as the list equivalent of

<subcmd> <subcmd options> <args>

The main program plucks <subcmd> from that list and then passes the
remaining args to the appropriate subcommand, which calls getopt again with
the list equivalent of

<subcmd options> <args>

I don't know if that's possible with optparse, but if so, that's the route
I'd try first.

Skip

Jul 18 '05 #3
Hans-Joachim Widmaier wrote:
This calls for a command line like CVS or subverion employ:

<cmd> <generic options> <subcmd> <subcmd options> <args>
I suggest the following:

1. Build an OptionParser for your <cmd> that accepts only the generic
options. Make sure to call
OptionParser.di sable_intersper sed_args() before calling
.parse_args(). The first argument in the returned args list should
be your <subcmd> (be sure to check!).

2. Once you know your <subcmd>, build a new OptionParser that handles
its options. Pass the argument list returned from step 1 (less the
subcmd) to .parse_args().

If you really want the subcmd's option parser to handle the <cmd>'s
generic options too, you could augment the OptionParser built in
step 1:

parser = OptionParser()
parser.add_opti on(...)
...
parser.disable_ interspersed_ar gs()
values, args = parser.parse_ar gs()

subcmd = args[0]
parser.add_opti on(...) # depending on subcmd
...
parser.enable_i nterspersed_arg s() # if you like
values, args = parser.parse_ar gs(args=args[1:], values=values)

You'll have to be careful about conflicts.
- Don't allow interspersed arguments. This is something I
dislike. I want to be able to type a forgotten generic option when
I'm at the end of a possibly long line.
You won't have any arguments *before* the subcmd, so the above should
work for you.
- Augment optparse to just ignore unknown options and treat them as
arguments.
There goes all error-checking.
Doesn't allow clustering of generic and subcommand-specific options,
You'd have to avoid conflicts completely.
doesn't allow for generic options to be arguments to
subcommand-specific options,
What does that mean?
but these seem as minor points to me. So I did that. And I'd like to
ask if this might have a chance to get into the standard optparse
module (It's just a few lines).
Your "minor points" will be major to someone else, so I doubt a
simplistic patch will be accepted. But by all means, show the code
(not here though; see below).
The bigger problem, though, is internationaliz ation. ... Is it even
thinkable that optparse (or, in the end, the whole standard library)
be prepared for internationaliz ation?


I imagine that optparse would be open to internationaliz ation. I'd
like to see some of optparse's strings be parameterized instead of
hard-coded. These changes just need someone to implement (and write
tests for, and document) them.

In any case, discussions here are unlikely to be seen, and almost
certainly won't be acted upon. I suggest posting to the optik-users
(@lists.sf.net) list or filing a feature request. A working code,
test, & doc patch would be helpful too.

--
David Goodger http://python.net/~goodger
For hire: http://python.net/~goodger/cv
Jul 18 '05 #4
> Hans-Joachim Widmaier wrote:
doesn't allow for generic options to be arguments to
subcommand-specific options,

On Wed, Mar 17, 2004 at 10:03:51AM -0500, David Goodger wrote: What does that mean?


Using the example of CVS again, the "-z#" argument specifies that
compression should be used for network connections. I think that these
have the same effect:
cvs -z3 co foo
cvs co -z3 foo
"-z#" can appear before or after subcommand in the commandline and has
the same meaning in either location.

Jeff

Jul 18 '05 #5
At some point, Jeff Epler <je****@unpytho nic.net> wrote:
Hans-Joachim Widmaier wrote:
> doesn't allow for generic options to be arguments to
> subcommand-specific options,

On Wed, Mar 17, 2004 at 10:03:51AM -0500, David Goodger wrote:
What does that mean?


Using the example of CVS again, the "-z#" argument specifies that
compression should be used for network connections. I think that these
have the same effect:
cvs -z3 co foo
cvs co -z3 foo
"-z#" can appear before or after subcommand in the commandline and has
the same meaning in either location.


You didn't try it, did you :-)? It doesn't work -- generic options have to
come before the subcommand w/ cvs.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)phy sics(dot)mcmast er(dot)ca
Jul 18 '05 #6
David Goodger <go*****@python .org> wrote in message news:<ma******* *************** *************@p ython.org>...
Hans-Joachim Widmaier wrote:
> This calls for a command line like CVS or subverion employ:
>
> <cmd> <generic options> <subcmd> <subcmd options> <args>
I suggest the following:

1. Build an OptionParser for your <cmd> that accepts only the generic
options. Make sure to call
OptionParser.di sable_intersper sed_args() before calling
.parse_args(). The first argument in the returned args list should
be your <subcmd> (be sure to check!).


As I wrote somewhat later: I dislike
OptionParser.di sable_intersper sed_args().
<subcmd> is in reality just another option (as it's not really a
command; it selects a plugin that handles the special requirements of
a device).
2. Once you know your <subcmd>, build a new OptionParser that handles
its options. Pass the argument list returned from step 1 (less the
subcmd) to .parse_args().
I have a generic interface that handles the generic options, scans its
directory for plug-ins and imports them. If one is selected, the
not-consumed arguments (and options) are passed to it. The plug-in
then does standard option parsing.
Works like a charm. (Bigger picture: The whole thing has a GUI also,
where you can select the plug-in interactively; the whole commandline
thing is to allow for a batch mode, too.)
If you really want the subcmd's option parser to handle the <cmd>'s
generic options too, you could augment the OptionParser built in
step 1:
There's just one option that needs to be handled, too (so it can be
called itself). Trivial to just define it with SUPPRESS_HELP if not
called standalone.
> - Augment optparse to just ignore unknown options and treat them as
> arguments.


There goes all error-checking.


Nah, it doesn't. Of course this is optional, and after the plug-in
parses the remains, there's nothing left to worry about.
> doesn't allow for generic options to be arguments to
> subcommand-specific options,


What does that mean?


E.g. if you have a generic option --bar and a subcmd-specific option
--foo that takes a string argument. Now if you call

<cmd> <subcmd> --foo --bar

--bar will be seen as a generic option and not as an argument to
--foo. But that's not really a problem, as it _is_ ambigous anyway
(might be a forgotten argument after all).
Your "minor points" will be major to someone else, so I doubt a
simplistic patch will be accepted. But by all means, show the code
(not here though; see below).
The patch is just a few lines (and took much less time to write than
this article), and it only implements an optional argument to
OptionParser.pa rse_args() that allows it to treat unknown options as
arguments.
In any case, discussions here are unlikely to be seen, and almost
certainly won't be acted upon. I suggest posting to the optik-users
(@lists.sf.net) list or filing a feature request. A working code,
test, & doc patch would be helpful too.


Thanks for your suggestion.
Hans-Joachim
Jul 18 '05 #7
On Thu, Mar 18, 2004 at 12:49:15AM -0500, David M. Cooke wrote:
You didn't try it, did you :-)? It doesn't work -- generic options have to
come before the subcommand w/ cvs.


Well, I tried this and got the error I expected:
$ cvs co -z3
cvs checkout: No CVSROOT specified! Please use the `-d' option
cvs [checkout aborted]: or set the CVSROOT environment variable.
... but now I see that it doesn't actually work that way:
$ cvs diff -z3
diff: invalid option -- z

Jeff

Jul 18 '05 #8
Am Wed, 17 Mar 2004 09:36:24 -0600 schrieb Skip Montanaro:
The main program plucks <subcmd> from that list and then passes the
remaining args to the appropriate subcommand, which calls getopt again with
the list equivalent of

<subcmd options> <args>

I don't know if that's possible with optparse, but if so, that's the route
I'd try first.


After some small changes to optparse, it is. It's the route I went, and
I'm satisfied with the result.

Hans-J.
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3161
by: washu | last post by:
Hi, I'm was going through the module help located on http://docs.python.org/lib/optparse-store-action.html and I tried modifying it a tiny bit and things don't work. If someone could tell me what I'm doing wrong, I'd appreciate it. The script that works (based on the code on the webpage) is: #!/usr/bin/env python
7
2788
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 functions, using flags (which are optional) and arguments. It will automatically print a nicely formatted usage (eg: -h or --help), and easily & automatically validates parameter existence and type. You can download it, and read a bit more about it,...
3
2454
by: Karlo Lozovina | last post by:
If I create a file with only one line: --- from optparse import OptionParser --- I get this when I try to run it from "DOS" prompt: Traceback (most recent call last): File "optparse.py", line 1, in ?
3
2803
by: Bob | last post by:
I'd like to setup command line switches that are dependent on other switches, similar to what rpm does listed below. From the grammar below we see that the "query-options" are dependent on the query switch, {-q|--query}. Can "optparse" do this or do I have to code my own "thing"? Thanks. QUERYING AND VERIFYING PACKAGES: rpm {-q|--query} .... query-options
0
1136
by: Steven Bethard | last post by:
I feel like I must be reinventing the wheel here, so I figured I'd post to see what other people have been doing for this. In general, I love the optparse interface, but it doesn't do any checks on the arguments. I've coded something along the following lines a number of times: class OptionArgParser(optparse.OptionParser): def __init__(self, *args, **kwargs): self.min_args = kwargs.pop('min_args', None) self.max_args =...
1
1770
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 Option. MyOptionParser uses MyOption as option_class and in Python 2.4 it works. But I have to target Python 2.3. In Python 2.3 the help and version options seem to be created before even a parser is created and they are created using a hardcoded...
2
1701
by: mbeachy | last post by:
Some rather unexpected behavior in the set_default/set_defaults methods for OptionParser that I noticed recently: <Option at 0x-483b3414: -r/--restart> {'restart': None} {'retart': False, 'restart': None} Why does set_default not raise an exception when passed a key that it doesn't recognize?
1
1165
by: Jeff Keasler | last post by:
Hi, optparse doesn't seem to have a pass-through capability for command line parameters/options that were not registered with add_option. I'm not the first person to complain about this. On Wed Mar 17 08:20:10 CET 2004, there's a thread titled "Perceived optparse shortcomings" where someone complains of the same problem. In a scripting environment, I often want to strip some of the command line
0
921
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 "--" marker. For example: $ cat mycommand.py import optparse parser = optparse.OptionParser()
0
8689
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9178
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8885
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7752
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.