473,830 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

optparse question

Hi guys,

I was wondering why optparse accept an option if in the command line is not
*exactly* the one present in the source code....may be an example will
explain better....

#!/usr/bin/env python

import optparse as opt

parser = opt.OptionParse r()
parser.add_opti on("-f", "--filein", dest="fileinput ", help="filename to
read", metavar="FILE")
(options, args) = parser.parse_ar gs()
count = 0
filename = open(options.fi leinput, 'r')
for lines in filename.xreadl ines():
count += 1
print count

if you run this dumb example in this way:

liquid@jupiter liquid $ ./testi.py --fil elenco_tm.txt
367

it goes on, but the option must be --filein not --fil

Is true that if you have another option like

parser.add_opti on("-f", "--fileout", dest="fileoutpu t", help="filename to
write", metavar="FILE")

the program will stop raising a conflict option, but why accept only the
beginning of the long option instead of the exact one?

Have a nice sunday, and as usual sorry for the english...
GMario
Jul 18 '05 #1
5 1946
GMTaglia wrote:
Hi guys,

I was wondering why optparse accept an option if in the command line is not
*exactly* the one present in the source code....may be an example will
explain better....

#!/usr/bin/env python

import optparse as opt

parser = opt.OptionParse r()
parser.add_opti on("-f", "--filein", dest="fileinput ", help="filename to
read", metavar="FILE")
(options, args) = parser.parse_ar gs()
count = 0
filename = open(options.fi leinput, 'r')
for lines in filename.xreadl ines():
count += 1
print count

if you run this dumb example in this way:

liquid@jupiter liquid $ ./testi.py --fil elenco_tm.txt
367

it goes on, but the option must be --filein not --fil

Is true that if you have another option like

parser.add_opti on("-f", "--fileout", dest="fileoutpu t", help="filename to
write", metavar="FILE")

the program will stop raising a conflict option, but why accept only the
beginning of the long option instead of the exact one?


I encountered the same question, looked at the source and found
the answer.

Quote from the document of getopt module:
http://docs.python.org/lib/module-getopt.html

Long options on the command line can be recognized so long as they
provide a prefix of the option name that matches exactly one of
the accepted options. For example, it long_options is ['foo',
'frob'], the option --fo will match as --foo, but --f will not
match uniquely, so GetoptError will be raised.

optparse is same as getopt in this respect.

George
Jul 18 '05 #2
George Yoshida wrote:

optparse is same as getopt in this respect.
that's fine but....why?

I think it will be better if only the *exact* option will match, it will
avoid typing mistakes and/or accidentally wrong matches, imho.

George


ciao
GMario
Jul 18 '05 #3
GMTaglia wrote:
I think it will be better if only the *exact* option will match, it will
avoid typing mistakes and/or accidentally wrong matches, imho.


I think this is because getopt module is designed as a pure Python
implementation of Unix(Linux?) getopt function.

I tested the sample code in C and it handled long options as
Python does.

George
Jul 18 '05 #4
GMTaglia wrote:
I think it will be better if only the exact option will match, it will
avoid typing mistakes and/or accidentally wrong matches, imho.


The source is there to override, even if it scares you away with abundant
underscores.

import optparse

def _(s): return s

class OptionParser(op tparse.OptionPa rser):
def _match_long_opt (self, opt):
if opt in self._long_opt:
return opt
raise optparse.BadOpt ionError(_("no such option: %s") % opt)

if __name__ == "__main__":
p = OptionParser()
p.add_option("--long", action="store_t rue")
options, args = p.parse_args()
print "long:", options.long

Peter

Jul 18 '05 #5
* GMTaglia (2004-09-18 23:39 +0200)
George Yoshida wrote:
optparse is same as getopt in this respect.
that's fine but....why?


To avoid unneccesary typing when the typed option is unambiguous. It's
the same with [y]es and [n]o. It makes things faster but increases
typo mistakes.
I think it will be better if only the *exact* option will match, it will
avoid typing mistakes and/or accidentally wrong matches, imho.


It won't avoid them but it will make them less likely. On the other
hand you could disable short options and make your long options at
least fifteen characters long to avoid typing mistakes and accidental
wrong matches.

*I* myself always use the complete long option when I use one but
maybe your users will find typing only a part comfortable?

On the other hand: if you have a lot of similar options, consider
renaming them so that they start with a different character or use a
config file (ConfigParser). May gpg be your guiding nightmare.

Thorsten
Jul 18 '05 #6

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

Similar topics

7
2796
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,...
3
2469
by: Tomi Silander | last post by:
Hi, this must have been asked 1000 times (or nobody is as stupid as me), but since I could not find the answer, here is the question. My program mitvit.py: -------------- import optparse optparse.OptionParser().parse_args() -------------- gives me
8
3532
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 message and exit -v, --verbose print program's version number and exit -o FILE Output file
0
928
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
9791
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
10771
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...
1
10525
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
10202
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
7745
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
6950
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
5617
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...
1
4411
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
2
3958
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.