473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.OptionParser()

parser.add_option("-f", action="store", type="string", dest="file_name")
(options, args) = parser.parse_args(sys.argv[1:])
print(options.file_name)

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

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

optparse.OptionParser().add_option( . . .)
(options, args) = optparse.OptionParser().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 3146
washu wrote:
optparse.OptionParser().add_option( . . .) Here you create an OptionParser instance and add one options
(options, args) = optparse.OptionParser().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.OptionParser() substitution, I spell everything out:

optparse.OptionParser().add_option( . . .)
(options, args) = optparse.OptionParser().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.OptionParser() 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
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...
7
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...
1
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...
3
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...
1
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...
3
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...
2
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...
0
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 "--"...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.