Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / lib / optparse-tutorial.html
CommitLineData
86530b38
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-reference-guide.html" />
13<link rel="prev" href="optparse-background.html" />
14<link rel="parent" href="module-optparse.html" />
15<link rel="next" href="optparse-understanding-option-actions.html" />
16<meta name='aesop' content='information' />
17<title>6.21.2 Tutorial</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.1.3 What are positional"
25 href="optparse-what-positional-arguments-for.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 optparse "
28 href="module-optparse.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.1 Understanding option actions"
31 href="optparse-understanding-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-what-positional-arguments-for.html">6.21.1.3 What are positional</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="module-optparse.html">6.21 optparse </A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="optparse-understanding-option-actions.html">6.21.2.1 Understanding option actions</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION0082120000000000000000"></A><A NAME="optparse-tutorial"></A>
56<BR>
576.21.2 Tutorial
58</H2>
59
60<P>
61While <tt class="module">optparse</tt> is quite flexible and powerful, it's also straightforward to
62use in most cases. This section covers the code patterns that are
63common to any <tt class="module">optparse</tt>-based program.
64
65<P>
66First, you need to import the OptionParser class; then, early in the
67main program, create an OptionParser instance:
68<div class="verbatim"><pre>
69from optparse import OptionParser
70[...]
71parser = OptionParser()
72</pre></div>
73
74<P>
75Then you can start defining options. The basic syntax is:
76<div class="verbatim"><pre>
77parser.add_option(opt_str, ...,
78 attr=value, ...)
79</pre></div>
80
81<P>
82Each option has one or more option strings, such as <code>"-f"</code> or
83<code>"-file"</code>, and several option attributes that tell <tt class="module">optparse</tt> what to
84expect and what to do when it encounters that option on the command
85line.
86
87<P>
88Typically, each option will have one short option string and one long
89option string, e.g.:
90<div class="verbatim"><pre>
91parser.add_option("-f", "--file", ...)
92</pre></div>
93
94<P>
95You're free to define as many short option strings and as many long
96option strings as you like (including zero), as long as there is at
97least one option string overall.
98
99<P>
100The option strings passed to <tt class="method">add_option()</tt> are effectively labels for
101the option defined by that call. For brevity, we will frequently refer
102to <em>encountering an option</em> on the command line; in reality, <tt class="module">optparse</tt>
103encounters <em>option strings</em> and looks up options from them.
104
105<P>
106Once all of your options are defined, instruct <tt class="module">optparse</tt> to parse your
107program's command line:
108<div class="verbatim"><pre>
109(options, args) = parser.parse_args()
110</pre></div>
111
112<P>
113(If you like, you can pass a custom argument list to <tt class="method">parse_args()</tt>,
114but that's rarely necessary: by default it uses <code>sys.argv[1:]</code>.)
115
116<P>
117<tt class="method">parse_args()</tt> returns two values:
118
119<UL>
120<LI>
121<code>options</code>, an object containing values for all of your options--e.g. if <code>"-file"</code> takes a single string argument, then
122<code>options.file</code> will be the filename supplied by the user, or
123<code>None</code> if the user did not supply that option
124
125<P>
126</LI>
127<LI>
128<code>args</code>, the list of positional arguments leftover after parsing
129options
130
131<P>
132</LI>
133</UL>
134
135<P>
136This tutorial section only covers the four most important option
137attributes: <tt class="member">action</tt>, <tt class="member">type</tt>, <tt class="member">dest</tt> (destination), and <tt class="member">help</tt>.
138Of these, <tt class="member">action</tt> is the most fundamental.
139
140<P>
141
142<p><br /></p><hr class='online-navigation' />
143<div class='online-navigation'>
144<!--Table of Child-Links-->
145<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
146
147<UL CLASS="ChildLinks">
148<LI><A href="optparse-understanding-option-actions.html">6.21.2.1 Understanding option actions</a>
149<LI><A href="optparse-store-action.html">6.21.2.2 The store action</a>
150<LI><A href="optparse-handling-boolean-options.html">6.21.2.3 Handling boolean (flag) options</a>
151<LI><A href="optparse-other-actions.html">6.21.2.4 Other actions</a>
152<LI><A href="optparse-default-values.html">6.21.2.5 Default values</a>
153<LI><A href="optparse-generating-help.html">6.21.2.6 Generating help</a>
154<LI><A href="optparse-printing-version-string.html">6.21.2.7 Printing a version string</a>
155<LI><A href="optparse-how-optparse-handles-errors.html">6.21.2.8 How <tt class="module">optparse</tt> handles errors</a>
156<LI><A href="optparse-putting-it-all-together.html">6.21.2.9 Putting it all together</a>
157</ul>
158<!--End of Table of Child-Links-->
159</div>
160
161<DIV CLASS="navigation">
162<div class='online-navigation'>
163<p></p><hr />
164<table align="center" width="100%" cellpadding="0" cellspacing="2">
165<tr>
166<td class='online-navigation'><a rel="prev" title="6.21.1.3 What are positional"
167 href="optparse-what-positional-arguments-for.html"><img src='../icons/previous.png'
168 border='0' height='32' alt='Previous Page' width='32' /></A></td>
169<td class='online-navigation'><a rel="parent" title="6.21 optparse "
170 href="module-optparse.html"><img src='../icons/up.png'
171 border='0' height='32' alt='Up One Level' width='32' /></A></td>
172<td class='online-navigation'><a rel="next" title="6.21.2.1 Understanding option actions"
173 href="optparse-understanding-option-actions.html"><img src='../icons/next.png'
174 border='0' height='32' alt='Next Page' width='32' /></A></td>
175<td align="center" width="100%">Python Library Reference</td>
176<td class='online-navigation'><a rel="contents" title="Table of Contents"
177 href="contents.html"><img src='../icons/contents.png'
178 border='0' height='32' alt='Contents' width='32' /></A></td>
179<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
180 border='0' height='32' alt='Module Index' width='32' /></a></td>
181<td class='online-navigation'><a rel="index" title="Index"
182 href="genindex.html"><img src='../icons/index.png'
183 border='0' height='32' alt='Index' width='32' /></A></td>
184</tr></table>
185<div class='online-navigation'>
186<b class="navlabel">Previous:</b>
187<a class="sectref" rel="prev" href="optparse-what-positional-arguments-for.html">6.21.1.3 What are positional</A>
188<b class="navlabel">Up:</b>
189<a class="sectref" rel="parent" href="module-optparse.html">6.21 optparse </A>
190<b class="navlabel">Next:</b>
191<a class="sectref" rel="next" href="optparse-understanding-option-actions.html">6.21.2.1 Understanding option actions</A>
192</div>
193</div>
194<hr />
195<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
196</DIV>
197<!--End of Navigation Panel-->
198<ADDRESS>
199See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
200</ADDRESS>
201</BODY>
202</HTML>