Working with optparse i came across with this error and i can’t figure out what is going on, an the docs aren’t helping me to clarify why this is happening.
Code:
formats = ['xml', 'json', 'yaml']
parser = optparse.OptionParser(usage = u, version = v)
parser.add_option('-s', '--src', dest = 'source_file', metavar = 'single source file')
parser.add_option('-f', '--format', dest = 'frmt', type = 'choice', choices = formats)
parser.add_option('-o', '--output', dest = 'output_file')
parser.set_default(frmt = 'xml', output_file = 'doc.xml')
opts, args = parser.parse_args()
gen_doc(opts.source_file, opts.frmt, opts.output_file)
Error:
$ python main.py -s a -f xml -o a
Traceback (most recent call last):
File "main.py", line 35, in <module>
main()
File "main.py", line 29, in main
parser.set_default(frmt = 'xml', output_file = 'doc.xml')
TypeError: set_default() got an unexpected keyword argument 'frmt'
As far as i know set_default should recognize the instance members set on add_option.
Any ideas?
The method is
set_defaults; note the plural.set_default(in the singular) takes two parameters; a name of an option and its value.