468,305 Members | 1,475 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,305 developers. It's quick & easy.

optparse: add trailing text in help message?

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
4 3315
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
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
"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
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.

Similar topics

5 posts views Thread by Norbert Thek | last post: by
8 posts views Thread by T | last post: by
4 posts views Thread by rick | last post: by
4 posts views Thread by Shatadal | last post: by
3 posts views Thread by Dan | last post: by
10 posts views Thread by James | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.