Can optparse do dependencies?

B

Bob

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} [select-options] [query-options]
....
query-options
[--changelog] [-c,--configfiles] [-d,--docfiles] [--dump]
[--filesbypkg] [-i,--info] [--last] [-l,--list]
[--provides] [--qf,--queryformat QUERYFMT]
[-R,--requires] [--scripts] [-s,--state]
[--triggers,--triggerscripts]
 
R

Raymond Hettinger

[Bob]
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} [select-options] [query-options]
...
query-options
[--changelog] [-c,--configfiles] [-d,--docfiles] [--dump]
[--filesbypkg] [-i,--info] [--last] [-l,--list]
[--provides] [--qf,--queryformat QUERYFMT]
[-R,--requires] [--scripts] [-s,--state]
[--triggers,--triggerscripts]

The optparse module doesn't have native support for switch
dependencies; however, it could likely be done with multiple passes.
The parse_args() takes an args list as an argument. Make first pass
that captures your main query switches, then run another parser on a
slice of the args list.

For example, capture the main switches on a first pass over the full
argument list (catching all possible main switches and treating
everything else as a catchall). Given, args=['-ql', '--changelog',
'-d', '-c'], parse out the --changelog and then call another parser
with args=['-d', '-c'].

This functionality seems useful enough to build into the tool directly,
so do consider putting a feature request on SourceForge (and assign to
Greg Ward).


Raymond
 
G

Giovanni Bajo

Raymond said:
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} [select-options] [query-options]
...
query-options
[--changelog] [-c,--configfiles] [-d,--docfiles] [--dump]
[--filesbypkg] [-i,--info] [--last] [-l,--list]
[--provides] [--qf,--queryformat QUERYFMT]
[-R,--requires] [--scripts] [-s,--state]
[--triggers,--triggerscripts]

The optparse module doesn't have native support for switch
dependencies; however, it could likely be done with multiple passes.
The parse_args() takes an args list as an argument. Make first pass
that captures your main query switches, then run another parser on a
slice of the args list.

For example, capture the main switches on a first pass over the full
argument list (catching all possible main switches and treating
everything else as a catchall). Given, args=['-ql', '--changelog',
'-d', '-c'], parse out the --changelog and then call another parser
with args=['-d', '-c'].

This functionality seems useful enough to build into the tool
directly, so do consider putting a feature request on SourceForge
(and assign to Greg Ward).

In fact, if we were to request an addition to optparse in this direction, I
think it should add standardized support for the "subcommand pattern", that is
the same command line arrangement that CVS, SVN and other programs uses. rpm
doesn't use it and I consider this an error in UI design (it should really have
been "rpm query --changelog" and similar).
 
M

Mike Erickson

* Bob ([email protected]) said:
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.

After you pull your options and arguments from optparse, just check for
them, eg:

parser = optparse.OptionParser()
....
(options, args) = parser.parse_args()

if (option.q and not option.p):
parser.error("you must use p to use q")

The meaning of -b doesn't change when it follows -a, but if you want
that, it is doable by extending optparse. There is even an example in
the very good "extending optik"[1] documentation available off the
sf.net site:

http://optik.sourceforge.net/

regards,

mike

[1]: optparse is also known as optik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top