473,756 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

optparse -- anyway to find if the user entered an option?

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 for value with
default may not always work as the user might have manually
entered the same default value.

Let's assume I want to take in the ip-address using -i <ip-addr>.
If user didn't give it explicitly, I am going to use socket interface
to figure out this host's IP address.

ip_addr_default = '100.100.100.10 0'

parser.add_opti on("-i", "--ip-address", dest="ip",
default=ip_addr _default,
metavar="IP-ADDRESS", help="IP address. default:" +
ip_addr_default + "e.g. --i=1.1.1.1"
)

(options, args) = parser.parse_ar gs()

Now if options.ip == ip_addr_default , I still can't be 100% sure that
the user did not type -i 100.100.100.100 .
Any way to figure out from options that the user typed it or not?

(The reason I want to know this is if user did not mention -i, I can
compute IP later
using socket module)

I could think of a hack of using None as default and since no user can
ever
enter a None value, I can be sure that the user didn't provide -i.
I'm wondering if there is a cleaner approach -- something like
parser.opt_seen ("-i")

Thanks,
Karthik

Apr 14 '07 #1
4 4577
Karthik Gurusamy wrote:
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 for value with
default may not always work as the user might have manually
entered the same default value.

Let's assume I want to take in the ip-address using -i <ip-addr>.
If user didn't give it explicitly, I am going to use socket interface
to figure out this host's IP address.

ip_addr_default = '100.100.100.10 0'

parser.add_opti on("-i", "--ip-address", dest="ip",
default=ip_addr _default,
metavar="IP-ADDRESS", help="IP address. default:" +
ip_addr_default + "e.g. --i=1.1.1.1"
)

(options, args) = parser.parse_ar gs()

Now if options.ip == ip_addr_default , I still can't be 100% sure that
the user did not type -i 100.100.100.100 .
Any way to figure out from options that the user typed it or not?

(The reason I want to know this is if user did not mention -i, I can
compute IP later
using socket module)

I could think of a hack of using None as default and since no user can
ever
enter a None value, I can be sure that the user didn't provide -i.
I'm wondering if there is a cleaner approach -- something like
parser.opt_seen ("-i")

Thanks,
Karthik
Using None wouldn't be a hack, it would rather be a common and
straightforward python idiom.

Compare:

if parser.opt_seen ("-i"):
do_whatever()

to

if options.ip is None:
do_whatever()

Looks like the second even saves a little typing. After using the former
a while, I would venture to guess that you might realize how the latter
is actually cleaner.

James
Apr 15 '07 #2
On Sat, 14 Apr 2007 16:49:22 -0700, Karthik Gurusamy wrote:
I'm wondering if there is a cleaner approach -- something like
parser.opt_seen ("-i")
What do dir(parser) and help(parser) say?

--
Steven.
Apr 15 '07 #3
On Apr 14, 7:54 pm, Steven D'Aprano
<s...@REMOVE.TH IS.cybersource. com.auwrote:
On Sat, 14 Apr 2007 16:49:22 -0700, Karthik Gurusamy wrote:
I'm wondering if there is a cleaner approach -- something like
parser.opt_seen ("-i")

What do dir(parser) and help(parser) say?
They don't seem to convey existence of a routine like the
one I'm looking for. I did check the lib reference - I guess
such a support is not available. Most likely the 'None'
solution will work for me. I will go ahead with it.

Karthik

>>dir(parser)
['__doc__', '__init__', '__module__', '_add_help_opti on',
'_add_version_o ption', '_check_conflic t', '_create_option _list',
'_create_option _mappings', '_get_all_optio ns', '_get_args',
'_get_encoding' , '_init_parsing_ state', '_long_opt',
'_match_long_op t', '_populate_opti on_list', '_process_args' ,
'_process_long_ opt', '_process_short _opts', '_share_option_ mappings',
'_short_opt', 'add_option', 'add_option_gro up', 'add_options',
'allow_interspe rsed_args', 'check_values', 'conflict_handl er',
'defaults', 'description', 'destroy', 'disable_inters persed_args',
'enable_intersp ersed_args', 'epilog', 'error', 'exit',
'expand_prog_na me', 'format_descrip tion', 'format_epilog' ,
'format_help', 'format_option_ help', 'formatter',
'get_default_va lues', 'get_descriptio n', 'get_option',
'get_option_gro up', 'get_prog_name' , 'get_usage', 'get_version',
'has_option', 'largs', 'option_class', 'option_groups' , 'option_list',
'parse_args', 'print_help', 'print_usage', 'print_version' ,
'process_defaul t_values', 'prog', 'rargs', 'remove_option' ,
'set_conflict_h andler', 'set_default', 'set_defaults',
'set_descriptio n', 'set_process_de fault_values', 'set_usage',
'standard_optio n_list', 'usage', 'values', 'version']
>>>
print sys.version
2.5 (r25:51908, Sep 29 2006, 12:35:59)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-54)]

help(parser) just gives info on a generic instance.
>
--
Steven.

Apr 15 '07 #4
James Stroud wrote:
Karthik Gurusamy wrote:
>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 for value with
default may not always work as the user might have manually
entered the same default value.

Let's assume I want to take in the ip-address using -i <ip-addr>.
If user didn't give it explicitly, I am going to use socket interface
to figure out this host's IP address.

ip_addr_defaul t = '100.100.100.10 0'

parser.add_opt ion("-i", "--ip-address", dest="ip",
default=ip_add r_default,
metavar="IP-ADDRESS", help="IP address. default:" +
ip_addr_default + "e.g. --i=1.1.1.1"
)

(options, args) = parser.parse_ar gs()

Now if options.ip == ip_addr_default , I still can't be 100% sure that
the user did not type -i 100.100.100.100 .
Any way to figure out from options that the user typed it or not?

(The reason I want to know this is if user did not mention -i, I can
compute IP later
using socket module)

I could think of a hack of using None as default and since no user can
ever
enter a None value, I can be sure that the user didn't provide -i.
I'm wondering if there is a cleaner approach -- something like
parser.opt_see n("-i")

Thanks,
Karthik

Using None wouldn't be a hack, it would rather be a common and
straightforward python idiom.
I agree. Also, remember that in optparse the default default (if you
will) is None.
--
Michael Hoffman
Apr 15 '07 #5

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

Similar topics

4
4007
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. Please do not suggest default value solutions. Thanks.
8
2309
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
2791
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,...
7
1706
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 and POSIX-shell style line trace (similar to "set -x") being two of the obvious ones. So I'm wondering how to arrange optparse to handle its options, but not touch the script's options.
8
9946
by: Ritesh Raj Sarraf | last post by:
Hi, I'm having some minor problems with optparse. I'm just worried that someone shouldn't say that multiple argument feature isn't implemented in optpartse. How to tackle multiple arguments to an option ? As far as I dug, I've found that >1 arguments are being ignored. parser.add_option("", "--my-option", dest="my_option", action="store",
1
1776
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...
1
1996
by: Mariano Mara | last post by:
Hi everyone. I'm building a script with optparse. One of the parameters will be a password. How can I code the optparse to accept/handle/format the password so that the user does not have to write it in plain/visible text in the terminal? TIA, Mariano.
2
1704
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
2086
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
9455
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
9271
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
10031
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
9869
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
9838
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
9708
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...
1
7242
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
6534
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();...
0
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.