Using python\'s optparse module I would like to add extra example lines below the regular usage output. My current help_print() output looks like this:
usage: ch
parser = optparse.OptionParser(epilog="otherstuff")
The default format_epilog
strips the newlines (uses textwrap), so you would need to override format_epilog
in your parser like this.
def main():
class MyParser(optparse.OptionParser):
def format_epilog(self, formatter):
return self.epilog
parser =MyParser(epilog=
"""Examples:
check_dell -c all
check_dell -c fans memory voltage
check_dell -s
""")
...
Here's a bit more detail.
If you look in optparse.py
in the class OptionParser
there is a method called format_epilog
which is called by format_help
here is the snippet from optparse.py
def format_epilog(self, formatter):
return formatter.format_epilog(self.epilog)
def format_help(self, formatter=None):
if formatter is None:
formatter = self.formatter
result = []
if self.usage:
result.append(self.get_usage() + "\n")
if self.description:
result.append(self.format_description(formatter) + "\n")
result.append(self.format_option_help(formatter))
result.append(self.format_epilog(formatter))
return "".join(result)
The default behaviour of formatter.format_epilog
is to use textwrap.fill
which amongst other things, strips the newlines from the epilog. Since we want the newlines to be preserved, we subclass OptionParser
and change the behaviour of format_epilog