optparse: add trailing text in help message?

  • Thread starter =?ISO-8859-1?Q?Count_L=E1szl=F3_de_Alm=E1sy?=
  • Start date
?

=?ISO-8859-1?Q?Count_L=E1szl=F3_de_Alm=E1sy?=

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?
 
J

James Stroud

Count said:
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?


py> from optparse import OptionParser
py>
py> class 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>
py> usage = 'usage: dosomething [options] path'
py> parser = OptionParserSpecial(usage)
py> parser.add_option("-a", "--all", dest="all",
.... action='store_true', default=False,
.... help="don't skip hidden or binary files")
<Option at 0x404f61ac: -a/--all>
py> parser.trailing_text = 'Some extra info here.'
py>
py> options, args = parser.parse_args()
py>
py> parser.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/
 
S

Steven Bethard

Count said:
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
 
D

Duncan Booth

Count László de Almásy said:
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!
 
S

Steven Bethard

Duncan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top