Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="optparse-printing-version-string.html" /> | |
13 | <link rel="prev" href="optparse-default-values.html" /> | |
14 | <link rel="parent" href="optparse-tutorial.html" /> | |
15 | <link rel="next" href="optparse-printing-version-string.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.21.2.6 Generating help</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="6.21.2.5 Default values" | |
25 | href="optparse-default-values.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="6.21.2 Tutorial" | |
28 | href="optparse-tutorial.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="6.21.2.7 Printing a version" | |
31 | href="optparse-printing-version-string.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="optparse-default-values.html">6.21.2.5 Default values</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="optparse-tutorial.html">6.21.2 Tutorial</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="optparse-printing-version-string.html">6.21.2.7 Printing a version</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H3><A NAME="SECTION0082126000000000000000"></A><A NAME="optparse-generating-help"></A> | |
56 | <BR> | |
57 | 6.21.2.6 Generating help | |
58 | </H3> | |
59 | ||
60 | <P> | |
61 | <tt class="module">optparse</tt>'s ability to generate help and usage text automatically is useful | |
62 | for creating user-friendly command-line interfaces. All you have to do | |
63 | is supply a <tt class="member">help</tt> value for each option, and optionally a short usage | |
64 | message for your whole program. Here's an OptionParser populated with | |
65 | user-friendly (documented) options: | |
66 | <div class="verbatim"><pre> | |
67 | usage = "usage: %prog [options] arg1 arg2" | |
68 | parser = OptionParser(usage=usage) | |
69 | parser.add_option("-v", "--verbose", | |
70 | action="store_true", dest="verbose", default=True, | |
71 | help="make lots of noise [default]") | |
72 | parser.add_option("-q", "--quiet", | |
73 | action="store_false", dest="verbose", | |
74 | help="be vewwy quiet (I'm hunting wabbits)") | |
75 | parser.add_option("-f", "--filename", | |
76 | metavar="FILE", help="write output to FILE"), | |
77 | parser.add_option("-m", "--mode", | |
78 | default="intermediate", | |
79 | help="interaction mode: novice, intermediate, " | |
80 | "or expert [default: %default]") | |
81 | </pre></div> | |
82 | ||
83 | <P> | |
84 | If <tt class="module">optparse</tt> encounters either <code>"-h"</code> or <code>"-help"</code> on the command-line, | |
85 | or if you just call <tt class="method">parser.print_help()</tt>, it prints the following to | |
86 | standard output: | |
87 | <div class="verbatim"><pre> | |
88 | usage: <yourscript> [options] arg1 arg2 | |
89 | ||
90 | options: | |
91 | -h, --help show this help message and exit | |
92 | -v, --verbose make lots of noise [default] | |
93 | -q, --quiet be vewwy quiet (I'm hunting wabbits) | |
94 | -f FILE, --filename=FILE | |
95 | write output to FILE | |
96 | -m MODE, --mode=MODE interaction mode: novice, intermediate, or | |
97 | expert [default: intermediate] | |
98 | </pre></div> | |
99 | ||
100 | <P> | |
101 | (If the help output is triggered by a help option, <tt class="module">optparse</tt> exits after | |
102 | printing the help text.) | |
103 | ||
104 | <P> | |
105 | There's a lot going on here to help <tt class="module">optparse</tt> generate the best possible | |
106 | help message: | |
107 | ||
108 | <UL> | |
109 | <LI> | |
110 | the script defines its own usage message: | |
111 | <div class="verbatim"><pre> | |
112 | usage = "usage: %prog [options] arg1 arg2" | |
113 | </pre></div> | |
114 | ||
115 | <P> | |
116 | <tt class="module">optparse</tt> expands <code>"%prog"</code> in the usage string to the name of the current | |
117 | program, i.e. <code>os.path.basename(sys.argv[0])</code>. The expanded string | |
118 | is then printed before the detailed option help. | |
119 | ||
120 | <P> | |
121 | If you don't supply a usage string, <tt class="module">optparse</tt> uses a bland but sensible | |
122 | default: ``<code>usage: %prog [options]"</code>, which is fine if your script | |
123 | doesn't take any positional arguments. | |
124 | ||
125 | <P> | |
126 | </LI> | |
127 | <LI> | |
128 | every option defines a help string, and doesn't worry about line- | |
129 | wrapping--<tt class="module">optparse</tt> takes care of wrapping lines and making the | |
130 | help output look good. | |
131 | ||
132 | <P> | |
133 | </LI> | |
134 | <LI> | |
135 | options that take a value indicate this fact in their | |
136 | automatically-generated help message, e.g. for the ``mode'' option: | |
137 | <div class="verbatim"><pre> | |
138 | -m MODE, --mode=MODE | |
139 | </pre></div> | |
140 | ||
141 | <P> | |
142 | Here, ``MODE'' is called the meta-variable: it stands for the argument | |
143 | that the user is expected to supply to <b class="programopt">-m</b>/<b class="programopt">--mode</b>. By default, | |
144 | <tt class="module">optparse</tt> converts the destination variable name to uppercase and uses | |
145 | that for the meta-variable. Sometimes, that's not what you want--for example, the <b class="programopt">--filename</b> option explicitly sets | |
146 | <code>metavar="FILE"</code>, resulting in this automatically-generated option | |
147 | description: | |
148 | <div class="verbatim"><pre> | |
149 | -f FILE, --filename=FILE | |
150 | </pre></div> | |
151 | ||
152 | <P> | |
153 | This is important for more than just saving space, though: the | |
154 | manually written help text uses the meta-variable ``FILE'' to clue the | |
155 | user in that there's a connection between the semi-formal syntax ``-f | |
156 | FILE'' and the informal semantic description ``write output to FILE''. | |
157 | This is a simple but effective way to make your help text a lot | |
158 | clearer and more useful for end users. | |
159 | ||
160 | <P> | |
161 | </LI> | |
162 | <LI> | |
163 | options that have a default value can include <code>%default</code> in | |
164 | the help string--<tt class="module">optparse</tt> will replace it with <tt class="function">str()</tt> of the | |
165 | option's default value. If an option has no default value (or the | |
166 | default value is <code>None</code>), <code>%default</code> expands to <code>none</code>. | |
167 | ||
168 | <P> | |
169 | </LI> | |
170 | </UL> | |
171 | ||
172 | <P> | |
173 | ||
174 | <DIV CLASS="navigation"> | |
175 | <div class='online-navigation'> | |
176 | <p></p><hr /> | |
177 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
178 | <tr> | |
179 | <td class='online-navigation'><a rel="prev" title="6.21.2.5 Default values" | |
180 | href="optparse-default-values.html"><img src='../icons/previous.png' | |
181 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
182 | <td class='online-navigation'><a rel="parent" title="6.21.2 Tutorial" | |
183 | href="optparse-tutorial.html"><img src='../icons/up.png' | |
184 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
185 | <td class='online-navigation'><a rel="next" title="6.21.2.7 Printing a version" | |
186 | href="optparse-printing-version-string.html"><img src='../icons/next.png' | |
187 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
188 | <td align="center" width="100%">Python Library Reference</td> | |
189 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
190 | href="contents.html"><img src='../icons/contents.png' | |
191 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
192 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
193 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
194 | <td class='online-navigation'><a rel="index" title="Index" | |
195 | href="genindex.html"><img src='../icons/index.png' | |
196 | border='0' height='32' alt='Index' width='32' /></A></td> | |
197 | </tr></table> | |
198 | <div class='online-navigation'> | |
199 | <b class="navlabel">Previous:</b> | |
200 | <a class="sectref" rel="prev" href="optparse-default-values.html">6.21.2.5 Default values</A> | |
201 | <b class="navlabel">Up:</b> | |
202 | <a class="sectref" rel="parent" href="optparse-tutorial.html">6.21.2 Tutorial</A> | |
203 | <b class="navlabel">Next:</b> | |
204 | <a class="sectref" rel="next" href="optparse-printing-version-string.html">6.21.2.7 Printing a version</A> | |
205 | </div> | |
206 | </div> | |
207 | <hr /> | |
208 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
209 | </DIV> | |
210 | <!--End of Navigation Panel--> | |
211 | <ADDRESS> | |
212 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
213 | </ADDRESS> | |
214 | </BODY> | |
215 | </HTML> |