MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: digitals (531)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: //usr/share/doc/python-docs-2.4.3/html/lib/optparse-generating-help.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<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>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<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>
</tr></table>
<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>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->

<H3><A NAME="SECTION0082126000000000000000"></A><A NAME="optparse-generating-help"></A>
<BR>
6.21.2.6 Generating help
</H3>

<P>
<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",
                  default="intermediate",
                  help="interaction mode: novice, intermediate, "
                       "or expert [default: %default]")
</pre></div>

<P>
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
standard output:
<div class="verbatim"><pre>
usage: &lt;yourscript&gt; [options] arg1 arg2

options:
  -h, --help            show this help message and exit
  -v, --verbose         make lots of noise [default]
  -q, --quiet           be vewwy quiet (I'm hunting wabbits)
  -f FILE, --filename=FILE
                        write output to FILE
  -m MODE, --mode=MODE  interaction mode: novice, intermediate, or
                        expert [default: intermediate]
</pre></div>

<P>
(If the help output is triggered by a help option, <tt class="module">optparse</tt> exits after
printing the help text.)

<P>
There's a lot going on here to help <tt class="module">optparse</tt> generate the best possible
help message:

<UL>
<LI> 
the script defines its own usage message:
<div class="verbatim"><pre>
usage = "usage: %prog [options] arg1 arg2"
</pre></div>

<P>
<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.

<P>
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.

<P>
</LI>
<LI> 
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
help output look good.

<P>
</LI>
<LI> 
options that take a value indicate this fact in their
automatically-generated help message, e.g. for the ``mode'' option:
<div class="verbatim"><pre>
-m MODE, --mode=MODE
</pre></div>

<P>
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
description:
<div class="verbatim"><pre>
-f FILE, --filename=FILE
</pre></div>

<P>
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.

<P>
</LI>
<LI> 
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>.

<P>
</LI>
</UL>

<P>

<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<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>
</tr></table>
<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>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.3, documentation updated on 29 March 2006.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>