Hello all. I'm trying to do a little script. Simply I want to make a list of all options with them default values. If the option is not specified in the command line, the script must try to read it in a config.ini file. If it's not present also there, it must set the default value.
The problem is I maked a simple list for this:
optname = [
[ "delete", False ],
[ "file", "file" ],
[ "dir", "" ],
But I must check that the option was specified in command line:
(options, args) = parser.parse_args()
for opt in optname :
if not options.opt[0] :
# read the options from config.ini
The problem is options is an instance, so options."delete", for example, is wrong; I should pass options.delete . How can I do?
--------------------------------------
Protect yourself from spam,
use http://sneakemail.com 2 1805
Lucas Malor wrote:
Hello all. I'm trying to do a little script. Simply I want to make a list
of all options with them default values. If the option is not specified in
the command line, the script must try to read it in a config.ini file. If
it's not present also there, it must set the default value.
The problem is I maked a simple list for this:
optname = [
[ "delete", False ],
[ "file", "file" ],
[ "dir", "" ],
But I must check that the option was specified in command line:
(options, args) = parser.parse_args()
for opt in optname :
if not options.opt[0] :
# read the options from config.ini
The problem is options is an instance, so options."delete", for example,
is wrong; I should pass options.delete . How can I do?
Use getattr():
for name, default_value in optname:
if getattr(options, name) == default_value:
value = ... # read value from config file
setattr(options, name, value)
Personally, I would always read the config file, use the values found there
to set up the parser and avoid such post-processing.
Peter
Peter Otten wrote:
Lucas Malor wrote:
>Hello all. I'm trying to do a little script. Simply I want to make a list of all options with them default values. If the option is not specified in the command line, the script must try to read it in a config.ini file. If it's not present also there, it must set the default value.
The problem is I maked a simple list for this:
optname = [ [ "delete", False ], [ "file", "file" ], [ "dir", "" ],
But I must check that the option was specified in command line:
(options, args) = parser.parse_args() for opt in optname : if not options.opt[0] : # read the options from config.ini
The problem is options is an instance, so options."delete", for example, is wrong; I should pass options.delete . How can I do?
Use getattr():
for name, default_value in optname:
if getattr(options, name) == default_value:
value = ... # read value from config file
setattr(options, name, value)
Personally, I would always read the config file, use the values found there
to set up the parser and avoid such post-proc
But then, if the command-line value == the default_value the program
will try to get a value from the config file. If the config file
overrides the defaults, then the command line can't re-override.
Stuck with this, I usually initialize with None, then after all the
option sources have been done, set anything that's still None to the
default. It's not tidy.
If even None could be a valid value, then a new None:
class LikeNothingElse:
'''Not a reasonable option value for anything'''
# ... various code
option_a = LikeNothingElse
option_b = LikeNothingElse
# ... process all the option sources
if option_a == LikeNothingElse:
option_a = None
Mel. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sam Smith |
last post by:
I am using optparse for the commandline parsing for my programs. I
was wondering if it is possible to detect if an option or option-arg
has been specified on the commandline by the user or not....
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Shatadal |
last post by:
In the python documentation section 14.3.2.6 (http://docs.python.org/
lib/optparse-generating-help.html) in the last line it is written
"options that have a default value can include %default in...
|
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 "--"...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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,...
|
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...
|
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...
|
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...
| |