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-standard-option-actions.html" /> | |
13 | <link rel="prev" href="optparse-populating-parser.html" /> | |
14 | <link rel="parent" href="optparse-reference-guide.html" /> | |
15 | <link rel="next" href="optparse-standard-option-actions.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.21.3.3 Defining options</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.3.2 Populating the parser" | |
25 | href="optparse-populating-parser.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.3 Reference Guide" | |
28 | href="optparse-reference-guide.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.3.4 Standard option actions" | |
31 | href="optparse-standard-option-actions.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-populating-parser.html">6.21.3.2 Populating the parser</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="optparse-reference-guide.html">6.21.3 Reference Guide</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="optparse-standard-option-actions.html">6.21.3.4 Standard option actions</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H3><A NAME="SECTION0082133000000000000000"></A><A NAME="optparse-defining-options"></A> | |
56 | <BR> | |
57 | 6.21.3.3 Defining options | |
58 | </H3> | |
59 | ||
60 | <P> | |
61 | Each Option instance represents a set of synonymous command-line option | |
62 | strings, e.g. <b class="programopt">-f</b> and <b class="programopt">--file</b>. You can | |
63 | specify any number of short or long option strings, but you must specify | |
64 | at least one overall option string. | |
65 | ||
66 | <P> | |
67 | The canonical way to create an Option instance is with the | |
68 | <tt class="method">add_option()</tt> method of <tt class="class">OptionParser</tt>: | |
69 | <div class="verbatim"><pre> | |
70 | parser.add_option(opt_str[, ...], attr=value, ...) | |
71 | </pre></div> | |
72 | ||
73 | <P> | |
74 | To define an option with only a short option string: | |
75 | <div class="verbatim"><pre> | |
76 | parser.add_option("-f", attr=value, ...) | |
77 | </pre></div> | |
78 | ||
79 | <P> | |
80 | And to define an option with only a long option string: | |
81 | <div class="verbatim"><pre> | |
82 | parser.add_option("--foo", attr=value, ...) | |
83 | </pre></div> | |
84 | ||
85 | <P> | |
86 | The keyword arguments define attributes of the new Option object. The | |
87 | most important option attribute is <tt class="member">action</tt>, and it largely determines | |
88 | which other attributes are relevant or required. If you pass irrelevant | |
89 | option attributes, or fail to pass required ones, <tt class="module">optparse</tt> raises an | |
90 | OptionError exception explaining your mistake. | |
91 | ||
92 | <P> | |
93 | An options's <em>action</em> determines what <tt class="module">optparse</tt> does when it encounters this | |
94 | option on the command-line. The standard option actions hard-coded into | |
95 | <tt class="module">optparse</tt> are: | |
96 | <DL> | |
97 | <DT><STRONG><code>store</code></STRONG></DT> | |
98 | <DD>store this option's argument (default) | |
99 | </DD> | |
100 | <DT><STRONG><code>store_const</code></STRONG></DT> | |
101 | <DD>store a constant value | |
102 | </DD> | |
103 | <DT><STRONG><code>store_true</code></STRONG></DT> | |
104 | <DD>store a true value | |
105 | </DD> | |
106 | <DT><STRONG><code>store_false</code></STRONG></DT> | |
107 | <DD>store a false value | |
108 | </DD> | |
109 | <DT><STRONG><code>append</code></STRONG></DT> | |
110 | <DD>append this option's argument to a list | |
111 | </DD> | |
112 | <DT><STRONG><code>count</code></STRONG></DT> | |
113 | <DD>increment a counter by one | |
114 | </DD> | |
115 | <DT><STRONG><code>callback</code></STRONG></DT> | |
116 | <DD>call a specified function | |
117 | </DD> | |
118 | <DT><STRONG><tt class="member">help</tt></STRONG></DT> | |
119 | <DD>print a usage message including all options and the | |
120 | documentation for them | |
121 | </DD> | |
122 | </DL> | |
123 | ||
124 | <P> | |
125 | (If you don't supply an action, the default is <code>store</code>. For this | |
126 | action, you may also supply <tt class="member">type</tt> and <tt class="member">dest</tt> option attributes; see | |
127 | below.) | |
128 | ||
129 | <P> | |
130 | As you can see, most actions involve storing or updating a value | |
131 | somewhere. <tt class="module">optparse</tt> always creates a special object for this, | |
132 | conventionally called <code>options</code> (it happens to be an instance of | |
133 | <code>optparse.Values</code>). Option arguments (and various other values) are | |
134 | stored as attributes of this object, according to the <tt class="member">dest</tt> | |
135 | (destination) option attribute. | |
136 | ||
137 | <P> | |
138 | For example, when you call | |
139 | <div class="verbatim"><pre> | |
140 | parser.parse_args() | |
141 | </pre></div> | |
142 | ||
143 | <P> | |
144 | one of the first things <tt class="module">optparse</tt> does is create the <code>options</code> object: | |
145 | <div class="verbatim"><pre> | |
146 | options = Values() | |
147 | </pre></div> | |
148 | ||
149 | <P> | |
150 | If one of the options in this parser is defined with | |
151 | <div class="verbatim"><pre> | |
152 | parser.add_option("-f", "--file", action="store", type="string", dest="filename") | |
153 | </pre></div> | |
154 | ||
155 | <P> | |
156 | and the command-line being parsed includes any of the following: | |
157 | <div class="verbatim"><pre> | |
158 | -ffoo | |
159 | -f foo | |
160 | --file=foo | |
161 | --file foo | |
162 | </pre></div> | |
163 | ||
164 | <P> | |
165 | then <tt class="module">optparse</tt>, on seeing this option, will do the equivalent of | |
166 | <div class="verbatim"><pre> | |
167 | options.filename = "foo" | |
168 | </pre></div> | |
169 | ||
170 | <P> | |
171 | The <tt class="member">type</tt> and <tt class="member">dest</tt> option attributes are almost as important as | |
172 | <tt class="member">action</tt>, but <tt class="member">action</tt> is the only one that makes sense for <em>all</em> | |
173 | options. | |
174 | ||
175 | <P> | |
176 | ||
177 | <DIV CLASS="navigation"> | |
178 | <div class='online-navigation'> | |
179 | <p></p><hr /> | |
180 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
181 | <tr> | |
182 | <td class='online-navigation'><a rel="prev" title="6.21.3.2 Populating the parser" | |
183 | href="optparse-populating-parser.html"><img src='../icons/previous.png' | |
184 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
185 | <td class='online-navigation'><a rel="parent" title="6.21.3 Reference Guide" | |
186 | href="optparse-reference-guide.html"><img src='../icons/up.png' | |
187 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
188 | <td class='online-navigation'><a rel="next" title="6.21.3.4 Standard option actions" | |
189 | href="optparse-standard-option-actions.html"><img src='../icons/next.png' | |
190 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
191 | <td align="center" width="100%">Python Library Reference</td> | |
192 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
193 | href="contents.html"><img src='../icons/contents.png' | |
194 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
195 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
196 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
197 | <td class='online-navigation'><a rel="index" title="Index" | |
198 | href="genindex.html"><img src='../icons/index.png' | |
199 | border='0' height='32' alt='Index' width='32' /></A></td> | |
200 | </tr></table> | |
201 | <div class='online-navigation'> | |
202 | <b class="navlabel">Previous:</b> | |
203 | <a class="sectref" rel="prev" href="optparse-populating-parser.html">6.21.3.2 Populating the parser</A> | |
204 | <b class="navlabel">Up:</b> | |
205 | <a class="sectref" rel="parent" href="optparse-reference-guide.html">6.21.3 Reference Guide</A> | |
206 | <b class="navlabel">Next:</b> | |
207 | <a class="sectref" rel="next" href="optparse-standard-option-actions.html">6.21.3.4 Standard option actions</A> | |
208 | </div> | |
209 | </div> | |
210 | <hr /> | |
211 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
212 | </DIV> | |
213 | <!--End of Navigation Panel--> | |
214 | <ADDRESS> | |
215 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
216 | </ADDRESS> | |
217 | </BODY> | |
218 | </HTML> |