473,385 Members | 1,256 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,385 software developers and data experts.

unexpected optparse set_default/set_defaults behavior

Some rather unexpected behavior in the set_default/set_defaults
methods for OptionParser that I noticed recently:
>>import optparse
parser = optparse.OptionParser()
parser.add_option("-r", "--restart", dest="restart", action="store_true")
<Option at 0x-483b3414: -r/--restart>
>>parser.defaults
{'restart': None}
>>parser.set_default("retart", False)
parser.defaults
{'retart': False, 'restart': None}

Why does set_default not raise an exception when passed a key that it
doesn't recognize?

Bad typysts bewaer.

The only reason I can think not to raise an exception is so that
defaults can be defined before the options are added. Is there some
use case that I'm not thinking of here? I realize that changing this
could break some existing scripts, but I'm still tempted to file this
as a bug.

Mike

Aug 17 '07 #1
2 1677
mb*****@gmail.com wrote:
Some rather unexpected behavior in the set_default/set_defaults
methods for OptionParser that I noticed recently:
>>>import optparse
parser = optparse.OptionParser()
parser.add_option("-r", "--restart", dest="restart", action="store_true")
<Option at 0x-483b3414: -r/--restart>
>>>parser.defaults
{'restart': None}
>>>parser.set_default("retart", False)
parser.defaults
{'retart': False, 'restart': None}

Why does set_default not raise an exception when passed a key that it
doesn't recognize?

Bad typysts bewaer.

The only reason I can think not to raise an exception is so that
defaults can be defined before the options are added. Is there some
use case that I'm not thinking of here?
I'm not really sure what other use case there is with optparse, but
argparse has the same behavior because sometimes it's useful to store
values that can't be changed by anything on the command line. This is
particularly useful when you're dealing with sub-commands::
>>import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
# set up the foo parser, adding a static "func" default
>>foo_parser = subparsers.add_parser('foo')
foo_parser.set_defaults(func=lambda: 'do something for foo')
foo_parser.add_argument('--foo')
# set up the bar parser, adding a staic "func" default
>>bar_parser = subparsers.add_parser('bar')
bar_parser.set_defaults(func=lambda: 'do something for bar')
bar_parser.add_argument('bar')
# parse the arguments and call whichever "func" was selected
>>args = parser.parse_args(['bar', '13'])
args.func()
'do something for bar'

I know optparse doesn't support sub-commands, but I can imagine that if
you were trying to hack optparse to do something similar you might find
it useful to be able to specify defaults that weren't ever set by
anything at the command line.

STeVe
Aug 17 '07 #2
mb*****@gmail.com wrote:
Some rather unexpected behavior in the set_default/set_defaults
methods for OptionParser that I noticed recently:
[...]
Why does set_default not raise an exception when passed a key that it
doesn't recognize?

Bad typysts bewaer.

The only reason I can think not to raise an exception is so that
defaults can be defined before the options are added. Is there some
use case that I'm not thinking of here? I realize that changing this
could break some existing scripts, but I'm still tempted to file this
as a bug.
If you file it as a bug then I can assure you that you'll be told of a
long list of use-cases that depend on that feature.
Aug 18 '07 #3

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

Similar topics

1
by: Andres Corrada-Emmanuel | last post by:
Hi, I'm getting a conflict between options defined between two different scripts and I'm wondering if it is a bug or the intended effect. script foo: <def some_function> <define some...
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...
5
by: Sébastien Boisgérault | last post by:
Any idea why the 'options' object in # optparse stuff (options, args) = parser.parse_args() is not/couldn't be a real dict ? Or why at least it does not support dict's usual methods ? The...
7
by: R. Bernstein | last post by:
optparse is way cool, far superior and cleaner than other options processing libraries I've used. In the next release of the Python debugger revision, I'd like to add debugger options: --help...
8
by: T | last post by:
I have a short program using optparse.OptionParser that prints out help message with -h flag: % myprog.py -h usage: myprog.py input_file options: -h, --help show this help...
4
by: Karthik Gurusamy | last post by:
Hi, I see that I can provide a default value for an option. But I couldn't find out any way if the user really entered the option or the option took that value because of default. A simple check...
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...
1
by: GustavoTabares | last post by:
Hello, I'm trying to figure out if the following is a bug or if I'm using the remove_option in the wrong way. #!/usr/bin/env python import optparse parser = optparse.OptionParser()...
10
by: James | last post by:
Hi, I would like to know your thoughts on a proposed change to optparse that I have planned. It is possible to add default values to multiple options using the set_defaults. However, when adding...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.