473,594 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

optparse: Why does this syntax work and not another?

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

import optparse, sys

parser = optparse.Option Parser()

parser.add_opti on("-f", action="store", type="string", dest="file_name ")
(options, args) = parser.parse_ar gs(sys.argv[1:])
print(options.f ile_name)

I call the script (optparse_test. py) in this fashion:
../optparse_test.p y -f foo.txt

And everything works. It will print foo.txt. Now, instead of doing the
parser = optparse.Option Parser() substitution, I spell everything out:

optparse.Option Parser().add_op tion( . . .)
(options, args) = optparse.Option Parser().parse_ args( . . . )

And the script stops with an error message of: "no such option: -f"

Anyone know why one syntax works and why the other doesn't?
Jul 18 '05 #1
3 3154
washu wrote:
optparse.Option Parser().add_op tion( . . .) Here you create an OptionParser instance and add one options
(options, args) = optparse.Option Parser().parse_ args( . . . )


Here you create a NEW OptionParser instance with no options and feed the
arguments to it.

Peter
Jul 18 '05 #2
On Mon, 05 Jul 2004 06:36:46 GMT, washu <me@privacy.net > wrote:
And everything works. It will print foo.txt. Now, instead of doing the
parser = optparse.Option Parser() substitution, I spell everything out:

optparse.Option Parser().add_op tion( . . .)
(options, args) = optparse.Option Parser().parse_ args( . . . )
And the script stops with an error message of: "no such option: -f"


Because your code is incorrect. The first of these two lines
creates and OptionParser() instance, and calls 'add_option()'
on it. It then throws away the newly created instance, as you
didn't assign it to anything.

Jul 18 '05 #3
Thanks Anthony and Peter,

I didn't realize that optparse.Option Parser() was a class. It makes sense
now. Now that I think about it, it's obvious that it has to be a class.
Luckily, it gave me an excuse to down a couple of Guinness's.
Jul 18 '05 #4

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

Similar topics

8
2299
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...
7
2781
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,...
1
2856
by: sector119 | last post by:
Hi I use optparse with callback action, my callback function return some value, but optparse does not store this value, options.callback_dest always is None. How can I store callback function return value or callback option value like store action do? I modify optparse.py Option::take_action def and add there:
3
2799
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
1
1763
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...
3
2082
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
2
1027
by: Tim Arnold | last post by:
Hi, I'm writing a command-line interface using optparse. The cli takes several options with a single action and several parameters to be used in the resulting worker classes. I've been passing parameters from optparse to the workers in two ways: (1) creating a Globals.py module, set parameters once in the cli code and read it when needed in the worker class methods. Something like this: import Globals
0
916
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
1269
by: John O'Hagan | last post by:
Here's a strange one for you: I have a generator function which produces lists of numbers and takes options which influence the output. The generator contains a loop, and to enable the options to have a different value on each iteration, the options may themselves be instances of the same generator, in the form of attributes of an optparse object. In the body of the loop, the next() method is called on each generator to get the new value...
0
7941
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
7874
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
8368
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...
1
8000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6652
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
5738
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
5404
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
2383
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
0
1205
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.