By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

optparse: add trailing text in help message?

P: n/a
Is there a standard way with optparse to include a blurb of text after
the usage section, description, and the list of options? This is
often useful to include examples or closing comments when the help
message is printed out. Many of the GNU commands do this.

It would look something like this:

% program --help

usage: program [options]

This is my description text.

options:
-f BAR, --foo=BAR
A sample option
--version show program's version number and exit
--help show this help message and exit

==Now how about closing text after the options list?
Oct 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Count László de Almásy wrote:
Is there a standard way with optparse to include a blurb of text after
the usage section, description, and the list of options? This is
often useful to include examples or closing comments when the help
message is printed out. Many of the GNU commands do this.

It would look something like this:

% program --help

usage: program [options]

This is my description text.

options:
-f BAR, --foo=BAR
A sample option
--version show program's version number and exit
--help show this help message and exit

==Now how about closing text after the options list?

Maybe subclass OptionParser?
pyfrom optparse import OptionParser
py>
pyclass OptionParserSpecial(OptionParser):
.... def format_help(self, *args, **kwargs):
.... result = OptionParser.format_help(self, *args, **kwargs)
.... if hasattr(self, 'trailing_text'):
.... return "%s\n%s\n" % (result, self.trailing_text)
.... else:
.... return result
....
py>
pyusage = 'usage: dosomething [options] path'
pyparser = OptionParserSpecial(usage)
pyparser.add_option("-a", "--all", dest="all",
.... action='store_true', default=False,
.... help="don't skip hidden or binary files")
<Option at 0x404f61ac: -a/--all>
pyparser.trailing_text = 'Some extra info here.'
py>
pyoptions, args = parser.parse_args()
py>
pyparser.print_help()
Usage: dosomething [options] path

Options:
-h, --help show this help message and exit
-a, --all don't skip hidden or binary files

Some extra info here.
James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Oct 14 '06 #2

P: n/a
Count László de Almásy wrote:
Is there a standard way with optparse to include a blurb of text after
the usage section, description, and the list of options? This is
often useful to include examples or closing comments when the help
message is printed out. Many of the GNU commands do this.

It would look something like this:

% program --help

usage: program [options]

This is my description text.

options:
-f BAR, --foo=BAR
A sample option
--version show program's version number and exit
--help show this help message and exit

==Now how about closing text after the options list?
What would you like this API to look like? I could add this pretty
easily to argparse_ this week. Maybe something like::

parser = argparse.ArgumentParser(
description='This is my ...',
tail='Now how about closing ...'
)

Or is there another API that would be better?

... _argparse: http://argparse.python-hosting.com/

STeVe
Oct 14 '06 #3

P: n/a
"Count László de Almásy" <ca*****@gmail.comwrote:
Is there a standard way with optparse to include a blurb of text after
the usage section, description, and the list of options? This is
often useful to include examples or closing comments when the help
message is printed out. Many of the GNU commands do this.
Yes, but only if you are running in Python 2.5.

From "What's new in Python 2.5":
The optparse module was updated to version 1.5.1 of the Optik library.
The OptionParser class gained an epilog attribute, a string that will
be printed after the help message, and a destroy() method to break
reference cycles created by the object. (Contributed by Greg Ward.)
In other words:
>>from optparse import OptionParser
usage = "usage: %prog [options] arg1 arg2"
epilog = "that's all folks!"
parser = OptionParser(usage=usage, epilog=epilog)
parser.parse_args(['', '--help'])
Usage: [options] arg1 arg2

Options:
-h, --help show this help message and exit

that's all folks!

Oct 14 '06 #4

P: n/a
Duncan Booth wrote:
>>>from optparse import OptionParser
usage = "usage: %prog [options] arg1 arg2"
epilog = "that's all folks!"
parser = OptionParser(usage=usage, epilog=epilog)
parser.parse_args(['', '--help'])
Usage: [options] arg1 arg2

Options:
-h, --help show this help message and exit

that's all folks!
Very cool. Thanks! I'll update argparse to share this API.

STeVe
Oct 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.