Code: Alles auswählen
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-i", "--infile", dest="infile",
help="read data from Input-File", default="infile.txt")
parser.add_option("-o", "--outfile", dest="outfile",
help="write data to Output-File", default="outfile.txt")
(options, args) = parser.parse_args()
if len(args) < 1:
parser.error("incorrect number of arguments ("+str(len(args))+")")
else:
# my sourcecode
wenn ich das Programm wie folgt aufrufe, meckert es immer, dass keine Parameter übergeben wurden:
Code: Alles auswählen
python3 myprog.py -i myinputfile.txt
Code: Alles auswählen
python3 myprog.py --help
Code: Alles auswählen
Usage: myprog.py [options]
Options:
-h, --help show this help message and exit
-i INFILE, --infile=INFILE
read data from Input-File
-o OUTFILE, --outfile=OUTFILE
write data to Output-File