<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"optparse-printing-version-string.html" />
<link rel=
"prev" href=
"optparse-default-values.html" />
<link rel=
"parent" href=
"optparse-tutorial.html" />
<link rel=
"next" href=
"optparse-printing-version-string.html" />
<meta name='aesop' content='information'
/>
<title>6.21.2.6 Generating help
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.21.2.5 Default values"
href=
"optparse-default-values.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"6.21.2 Tutorial"
href=
"optparse-tutorial.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"6.21.2.7 Printing a version"
href=
"optparse-printing-version-string.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"optparse-default-values.html">6.21.2.5 Default values
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"optparse-tutorial.html">6.21.2 Tutorial
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"optparse-printing-version-string.html">6.21.2.7 Printing a version
</A>
<!--End of Navigation Panel-->
<H3><A NAME=
"SECTION0082126000000000000000"></A><A NAME=
"optparse-generating-help"></A>
<tt class=
"module">optparse
</tt>'s ability to generate help and usage text automatically is useful
for creating user-friendly command-line interfaces. All you have to do
is supply a
<tt class=
"member">help
</tt> value for each option, and optionally a short usage
message for your whole program. Here's an OptionParser populated with
user-friendly (documented) options:
<div class=
"verbatim"><pre>
usage =
"usage: %prog [options] arg1 arg2"
parser = OptionParser(usage=usage)
parser.add_option(
"-v",
"--verbose",
action=
"store_true",
dest=
"verbose", default=True,
help=
"make lots of noise [default]")
parser.add_option(
"-q",
"--quiet",
action=
"store_false",
dest=
"verbose",
help=
"be vewwy quiet (I'm hunting wabbits)")
parser.add_option(
"-f",
"--filename",
metavar=
"FILE",
help=
"write output to FILE"),
parser.add_option(
"-m",
"--mode",
help=
"interaction mode: novice, intermediate, "
"or expert [default: %default]")
If
<tt class=
"module">optparse
</tt> encounters either
<code>"-h"</code> or
<code>"-help"</code> on the command-line,
or if you just call
<tt class=
"method">parser.print_help()
</tt>, it prints the following to
<div class=
"verbatim"><pre>
usage:
<yourscript
> [options] arg1 arg2
-h, --help show this help message and exit
-v, --verbose make lots of noise [default]
-q, --quiet be vewwy quiet (I'm hunting wabbits)
-m MODE, --mode=MODE interaction mode: novice, intermediate, or
expert [default: intermediate]
(If the help output is triggered by a help option,
<tt class=
"module">optparse
</tt> exits after
There's a lot going on here to help
<tt class=
"module">optparse
</tt> generate the best possible
the script defines its own usage message:
<div class=
"verbatim"><pre>
usage =
"usage: %prog [options] arg1 arg2"
<tt class=
"module">optparse
</tt> expands
<code>"%prog"</code> in the usage string to the name of the current
program, i.e.
<code>os.path.basename(sys.argv[
0])
</code>. The expanded string
is then printed before the detailed option help.
If you don't supply a usage string,
<tt class=
"module">optparse
</tt> uses a bland but sensible
default: ``
<code>usage: %prog [options]
"</code>, which is fine if your script
doesn't take any positional arguments.
every option defines a help string, and doesn't worry about line-
wrapping--<tt class="module
">optparse</tt> takes care of wrapping lines and making the
options that take a value indicate this fact in their
automatically-generated help message, e.g. for the ``mode'' option:
<div class="verbatim
"><pre>
Here, ``MODE'' is called the meta-variable: it stands for the argument
that the user is expected to supply to <b class="programopt
">-m</b>/<b class="programopt
">--mode</b>. By default,
<tt class="module
">optparse</tt> converts the destination variable name to uppercase and uses
that for the meta-variable. Sometimes, that's not what you want--for example, the <b class="programopt
">--filename</b> option explicitly sets
<code>metavar="FILE
"</code>, resulting in this automatically-generated option
<div class="verbatim
"><pre>
This is important for more than just saving space, though: the
manually written help text uses the meta-variable ``FILE'' to clue the
user in that there's a connection between the semi-formal syntax ``-f
FILE'' and the informal semantic description ``write output to FILE''.
This is a simple but effective way to make your help text a lot
clearer and more useful for end users.
options that have a default value can include <code>%default</code> in
the help string--<tt class="module
">optparse</tt> will replace it with <tt class="function
">str()</tt> of the
option's default value. If an option has no default value (or the
default value is <code>None</code>), <code>%default</code> expands to <code>none</code>.
<div class='online-navigation'>
<table align="center
" width="100%
" cellpadding="0" cellspacing="2">
<td class='online-navigation'><a rel="prev
" title="6.21.2.5 Default values
"
href="optparse-default-values.html
"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent
" title="6.21.2 Tutorial
"
href="optparse-tutorial.html
"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next
" title="6.21.2.7 Printing a version
"
href="optparse-printing-version-string.html
"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center
" width="100%
">Python Library Reference</td>
<td class='online-navigation'><a rel="contents
" title="Table of Contents
"
href="contents.html
"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html
" title="Module Index
"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index
" title="Index
"
href="genindex.html
"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
<div class='online-navigation'>
<b class="navlabel
">Previous:</b>
<a class="sectref
" rel="prev
" href="optparse-default-values.html
">6.21.2.5 Default values</A>
<b class="navlabel
">Up:</b>
<a class="sectref
" rel="parent
" href="optparse-tutorial.html
">6.21.2 Tutorial</A>
<b class="navlabel
">Next:</b>
<a class="sectref
" rel="next
" href="optparse-printing-version-string.html
">6.21.2.7 Printing a version</A>
<span class="release-info
">Release 2.4.2, documentation updated on 28 September 2005.</span>
<!--End of Navigation Panel-->
See <i><a href="about.html
">About this document...</a></i> for information on suggesting changes.