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!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.