473,804 Members | 3,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
3 2808
[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********@yah oo.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.Option Parser()
....
(options, args) = parser.parse_ar gs()

if (option.q and not option.p):
parser.error("y ou 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2311
by: Hans-Joachim Widmaier | last post by:
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...
3
3168
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
2793
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
2461
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 ?
0
1140
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
1780
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
1707
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?
3
2089
by: Dan | last post by:
I've been using optparse for a while, and I have an option with a number of sub-actions I want to describe in the help section: parser.add_option("-a", "--action", help=\ """Current supported actions: create, build, import, exp_cmd and interact. create -- Vaguely depreciated, should create a new project, but it is
0
928
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
9708
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
9588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
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
10340
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
10085
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
9161
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
7625
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
6857
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();...
3
2999
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.