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.OptionParser()
parser.add_option("-f", "--filein", dest="fileinput", help="filename to
read", metavar="FILE")
(options, args) = parser.parse_args()
count = 0
filename = open(options.fileinput, 'r')
for lines in filename.xreadlines():
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_option("-f", "--fileout", dest="fileoutput", 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 5 1928
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.OptionParser() parser.add_option("-f", "--filein", dest="fileinput", help="filename to read", metavar="FILE") (options, args) = parser.parse_args() count = 0 filename = open(options.fileinput, 'r') for lines in filename.xreadlines(): 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_option("-f", "--fileout", dest="fileoutput", 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
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
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
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(optparse.OptionParser):
def _match_long_opt(self, opt):
if opt in self._long_opt:
return opt
raise optparse.BadOptionError(_("no such option: %s") % opt)
if __name__ == "__main__":
p = OptionParser()
p.add_option("--long", action="store_true")
options, args = p.parse_args()
print "long:", options.long
Peter
* 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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: 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...
|
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...
|
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: 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...
|
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
|
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: 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: 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: 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...
|
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...
| |