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-handling-boolean-options.html" /> | |
13 | <link rel="prev" href="optparse-understanding-option-actions.html" /> | |
14 | <link rel="parent" href="optparse-tutorial.html" /> | |
15 | <link rel="next" href="optparse-handling-boolean-options.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.21.2.2 The store action</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.1 Understanding option actions" | |
25 | href="optparse-understanding-option-actions.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.3 Handling boolean (flag)" | |
31 | href="optparse-handling-boolean-options.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-understanding-option-actions.html">6.21.2.1 Understanding option actions</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-handling-boolean-options.html">6.21.2.3 Handling boolean (flag)</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H3><A NAME="SECTION0082122000000000000000"></A><A NAME="optparse-store-action"></A> | |
56 | <BR> | |
57 | 6.21.2.2 The store action | |
58 | </H3> | |
59 | ||
60 | <P> | |
61 | The most common option action is <code>store</code>, which tells <tt class="module">optparse</tt> to take | |
62 | the next argument (or the remainder of the current argument), ensure | |
63 | that it is of the correct type, and store it to your chosen destination. | |
64 | ||
65 | <P> | |
66 | For example: | |
67 | <div class="verbatim"><pre> | |
68 | parser.add_option("-f", "--file", | |
69 | action="store", type="string", dest="filename") | |
70 | </pre></div> | |
71 | ||
72 | <P> | |
73 | Now let's make up a fake command line and ask <tt class="module">optparse</tt> to parse it: | |
74 | <div class="verbatim"><pre> | |
75 | args = ["-f", "foo.txt"] | |
76 | (options, args) = parser.parse_args(args) | |
77 | </pre></div> | |
78 | ||
79 | <P> | |
80 | When <tt class="module">optparse</tt> sees the option string <code>"-f"</code>, it consumes the next | |
81 | argument, <code>"foo.txt"</code>, and stores it in <code>options.filename</code>. So, | |
82 | after this call to <tt class="method">parse_args()</tt>, <code>options.filename</code> is | |
83 | <code>"foo.txt"</code>. | |
84 | ||
85 | <P> | |
86 | Some other option types supported by <tt class="module">optparse</tt> are <code>int</code> and <code>float</code>. | |
87 | Here's an option that expects an integer argument: | |
88 | <div class="verbatim"><pre> | |
89 | parser.add_option("-n", type="int", dest="num") | |
90 | </pre></div> | |
91 | ||
92 | <P> | |
93 | Note that this option has no long option string, which is perfectly | |
94 | acceptable. Also, there's no explicit action, since the default is | |
95 | <code>store</code>. | |
96 | ||
97 | <P> | |
98 | Let's parse another fake command-line. This time, we'll jam the option | |
99 | argument right up against the option: since <code>"-n42"</code> (one argument) is | |
100 | equivalent to <code>"-n 42"</code> (two arguments), the code | |
101 | <div class="verbatim"><pre> | |
102 | (options, args) = parser.parse_args(["-n42"]) | |
103 | print options.num | |
104 | </pre></div> | |
105 | ||
106 | <P> | |
107 | will print <code>"42"</code>. | |
108 | ||
109 | <P> | |
110 | If you don't specify a type, <tt class="module">optparse</tt> assumes <code>string</code>. Combined with the | |
111 | fact that the default action is <code>store</code>, that means our first example | |
112 | can be a lot shorter: | |
113 | <div class="verbatim"><pre> | |
114 | parser.add_option("-f", "--file", dest="filename") | |
115 | </pre></div> | |
116 | ||
117 | <P> | |
118 | If you don't supply a destination, <tt class="module">optparse</tt> figures out a sensible default | |
119 | from the option strings: if the first long option string is | |
120 | <code>"-foo-bar"</code>, then the default destination is <code>foo_bar</code>. If there | |
121 | are no long option strings, <tt class="module">optparse</tt> looks at the first short option | |
122 | string: the default destination for <code>"-f"</code> is <code>f</code>. | |
123 | ||
124 | <P> | |
125 | <tt class="module">optparse</tt> also includes built-in <code>long</code> and <code>complex</code> types. Adding | |
126 | types is covered in section <A HREF="#optparse-extending"><tex2html_cross_ref_visible_mark></A>, Extending <tt class="module">optparse</tt>. | |
127 | ||
128 | <P> | |
129 | ||
130 | <DIV CLASS="navigation"> | |
131 | <div class='online-navigation'> | |
132 | <p></p><hr /> | |
133 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
134 | <tr> | |
135 | <td class='online-navigation'><a rel="prev" title="6.21.2.1 Understanding option actions" | |
136 | href="optparse-understanding-option-actions.html"><img src='../icons/previous.png' | |
137 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
138 | <td class='online-navigation'><a rel="parent" title="6.21.2 Tutorial" | |
139 | href="optparse-tutorial.html"><img src='../icons/up.png' | |
140 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
141 | <td class='online-navigation'><a rel="next" title="6.21.2.3 Handling boolean (flag)" | |
142 | href="optparse-handling-boolean-options.html"><img src='../icons/next.png' | |
143 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
144 | <td align="center" width="100%">Python Library Reference</td> | |
145 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
146 | href="contents.html"><img src='../icons/contents.png' | |
147 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
148 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
149 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
150 | <td class='online-navigation'><a rel="index" title="Index" | |
151 | href="genindex.html"><img src='../icons/index.png' | |
152 | border='0' height='32' alt='Index' width='32' /></A></td> | |
153 | </tr></table> | |
154 | <div class='online-navigation'> | |
155 | <b class="navlabel">Previous:</b> | |
156 | <a class="sectref" rel="prev" href="optparse-understanding-option-actions.html">6.21.2.1 Understanding option actions</A> | |
157 | <b class="navlabel">Up:</b> | |
158 | <a class="sectref" rel="parent" href="optparse-tutorial.html">6.21.2 Tutorial</A> | |
159 | <b class="navlabel">Next:</b> | |
160 | <a class="sectref" rel="next" href="optparse-handling-boolean-options.html">6.21.2.3 Handling boolean (flag)</A> | |
161 | </div> | |
162 | </div> | |
163 | <hr /> | |
164 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
165 | </DIV> | |
166 | <!--End of Navigation Panel--> | |
167 | <ADDRESS> | |
168 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
169 | </ADDRESS> | |
170 | </BODY> | |
171 | </HTML> |