optparse question

GMTaglia liquid at kuht.it
Sat Sep 18 16:48:22 EDT 2004


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 at 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



More information about the Python-list mailing list