473,473 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3475
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: washu | last post by:
Hi, I'm was going through the module help located on http://docs.python.org/lib/optparse-store-action.html and I tried modifying it a tiny bit and things don't work. If someone could tell me...
5
by: Norbert Thek | last post by:
Hi I'm using Python 24 on Windows > (2k) Is there an easy way to convince optparse to accept newline in the helpstring? and more importand also in the 'desc' string. I tried everything (from...
8
by: T | last post by:
I have a short program using optparse.OptionParser that prints out help message with -h flag: % myprog.py -h usage: myprog.py input_file options: -h, --help show this help...
1
by: Pupeno | last post by:
Hello, I am doing some extreme use of optparse, that is, extending it as explained on http://docs.python.org/lib/optparse-other-reasons-to-extend-optparse.html I have subclassed OptionParser and...
4
by: rick | last post by:
Consider the following piece of code: parser = optparse.OptionParser(usage="usage: %prog <input filename> <output filename", add_help_option=False) parser.add_option("-d", type="string",...
4
by: Shatadal | last post by:
In the python documentation section 14.3.2.6 (http://docs.python.org/ lib/optparse-generating-help.html) in the last line it is written "options that have a default value can include %default in...
3
by: Dan | last post by:
I've been using optparse for a while, and I have an option with a number of sub-actions I want to describe in the help section: parser.add_option("-a", "--action", help=\ """Current supported...
10
by: James | last post by:
Hi, I would like to know your thoughts on a proposed change to optparse that I have planned. It is possible to add default values to multiple options using the set_defaults. However, when adding...
2
by: hofer | last post by:
Hi, I get following warning with a python script: optparse.py:668: FutureWarning: %u/%o/%x/%X of negative int will return a signed string in Python 2.4 and up my code:
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.