Is there a way to make Python's optparse print the default value of an option or flag when showing the help with --help?
+21
A:
Try using the %default
string placeholder:
# This example taken from http://docs.python.org/library/optparse.html#generating-help
parser.add_option("-m", "--mode",
default="intermediate",
help="interaction mode: novice, intermediate, "
"or expert [default: %default]")
Jarret Hardie
2009-08-10 12:01:33
Great to know! I wasn't aware of this...
jkp
2009-08-10 12:03:09
+4
A:
And if you need programmatic access to the default values, you can get to them via the defaults
attribute of the parser (it's a dict)
scrible
2009-08-10 12:04:04