"T" <ty*****@yahoo.comwrites:
[...]
What I would like to do is insert some string *before* the "usage = "
string, which is right after the command I type at the command prompt.
So I would like to make it look like this:
% myprog.py -h
************ THIS IS NEWLY INSERTED STRING ************
usage: myprog.py [options] input_file
options:
-h, --help show this help message and exit
-v, --verbose print program's version number and exit
-o FILE Output file
HelpFormatter is what you need. Seems undocumented in the official
docs, but doesn't look risky to use (famous last words). Seems just
that nobody got around to documenting it.
import optparse
class NonstandardHelpFormatter(optparse.HelpFormatter):
def __init__(self,
indent_increment=2,
max_help_position=24,
width=None,
short_first=1):
optparse.HelpFormatter.__init__(
self, indent_increment, max_help_position, width, short_first)
def format_usage(self, usage):
return "************ THIS IS NEWLY INSERTED STRING ************\nusage: %s\n" % usage
def format_heading(self, heading):
return "%*s%s:\n" % (self.current_indent, "", heading)
parser = optparse.OptionParser(
usage="%prog [options] input_file",
formatter=NonstandardHelpFormatter())
parser.parse_args()
John