467,075 Members | 1,006 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,075 developers. It's quick & easy.

Can optparse do dependencies?

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]

Feb 25 '06 #1
  • viewed: 2536
Share:
3 Replies
[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

Feb 25 '06 #2
Raymond Hettinger wrote:
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).
--
Giovanni Bajo
Feb 26 '06 #3
* Bob (bo********@yahoo.com) wrote:
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

Feb 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Hans-Joachim Widmaier | last post: by
7 posts views Thread by Henry Ludemann | last post: by
3 posts views Thread by Karlo Lozovina | last post: by
reply views Thread by Steven Bethard | last post: by
2 posts views Thread by mbeachy@gmail.com | last post: by
3 posts views Thread by Dan | last post: by
reply views Thread by Robert Kern | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.