0

I am learning parse command line options by optparse,when I want to parse input file from command line and then use part of it as output file name:

import optparse

parser=optparse.OptionParser()
parser.add_option("-input", dest="inputFile")
parser.add_option("-output", action="store", dest="outputFile", default=options.inputFile.rstrip(.txt), type="string")

(options, args)=parser.parse_args()

print "options.inputFile=", options.inputFile
print "options.outputFile=", options.outputFile

When I call the program:
python mytest.py -input test.txt

The output somewhat like this:

options.inputFile=test.txt
options.outputFile=options.inputFile.rstrip(.txt)

but not what I want:
options.outputFile=test
I know that I sort of called options module before I define it, but cannot figure out how to do it. Any suggestions, Thanks a lot!

Edited by pythonlearning: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by woooee
0

I get an error because .txt was not in quotes. Also note that you will get an aditional error because "options" is declared after the --output parser statement.

Edited by woooee: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.