Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / module-optparse.html
CommitLineData
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="module-tempfile.html" />
13<link rel="prev" href="module-getopt.html" />
14<link rel="parent" href="allos.html" />
15<link rel="next" href="optparse-background.html" />
16<meta name='aesop' content='information' />
17<title>6.21 optparse -- More powerful command line option parser</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.20 getopt "
25 href="module-getopt.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. Generic Operating System"
28 href="allos.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.1 Background"
31 href="optparse-background.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="module-getopt.html">6.20 getopt </A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="allos.html">6. Generic Operating System</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="optparse-background.html">6.21.1 Background</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION0082100000000000000000">
566.21 <tt class="module">optparse</tt> -- More powerful command line option parser</A>
57</H1>
58<A NAME="module-optparse"></A>
59
60<span class="versionnote">New in version 2.3.</span>
61
62<P>
63<code>optparse</code> is a more convenient, flexible, and powerful library for
64parsing command-line options than <code>getopt</code>. <code>optparse</code> uses a more
65declarative style of command-line parsing: you create an instance of
66<tt class="class">OptionParser</tt>, populate it with options, and parse the command line.
67<code>optparse</code> allows users to specify options in the conventional GNU/POSIX
68syntax, and additionally generates usage and help messages for you.
69
70<P>
71Here's an example of using <code>optparse</code> in a simple script:
72<div class="verbatim"><pre>
73from optparse import OptionParser
74[...]
75parser = OptionParser()
76parser.add_option("-f", "--file", dest="filename",
77 help="write report to FILE", metavar="FILE")
78parser.add_option("-q", "--quiet",
79 action="store_false", dest="verbose", default=True,
80 help="don't print status messages to stdout")
81
82(options, args) = parser.parse_args()
83</pre></div>
84
85<P>
86With these few lines of code, users of your script can now do the
87``usual thing'' on the command-line, for example:
88<div class="verbatim"><pre>
89&lt;yourscript&gt; --file=outfile -q
90</pre></div>
91
92<P>
93As it parses the command line, <code>optparse</code> sets attributes of the
94<code>options</code> object returned by <tt class="method">parse_args()</tt> based on user-supplied
95command-line values. When <tt class="method">parse_args()</tt> returns from parsing this
96command line, <code>options.filename</code> will be <code>"outfile"</code> and
97<code>options.verbose</code> will be <code>False</code>. <code>optparse</code> supports both long
98and short options, allows short options to be merged together, and
99allows options to be associated with their arguments in a variety of
100ways. Thus, the following command lines are all equivalent to the above
101example:
102<div class="verbatim"><pre>
103&lt;yourscript&gt; -f outfile --quiet
104&lt;yourscript&gt; --quiet --file outfile
105&lt;yourscript&gt; -q -foutfile
106&lt;yourscript&gt; -qfoutfile
107</pre></div>
108
109<P>
110Additionally, users can run one of
111<div class="verbatim"><pre>
112&lt;yourscript&gt; -h
113&lt;yourscript&gt; --help
114</pre></div>
115
116<P>
117and <code>optparse</code> will print out a brief summary of your script's
118options:
119<div class="verbatim"><pre>
120usage: &lt;yourscript&gt; [options]
121
122options:
123 -h, --help show this help message and exit
124 -f FILE, --file=FILE write report to FILE
125 -q, --quiet don't print status messages to stdout
126</pre></div>
127
128<P>
129where the value of <em>yourscript</em> is determined at runtime (normally
130from <code>sys.argv[0]</code>).
131
132<P>
133
134<p><br /></p><hr class='online-navigation' />
135<div class='online-navigation'>
136<!--Table of Child-Links-->
137<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
138
139<UL CLASS="ChildLinks">
140<LI><A href="optparse-background.html">6.21.1 Background</a>
141<UL>
142<LI><A href="optparse-terminology.html">6.21.1.1 Terminology</a>
143<LI><A href="optparse-what-options-for.html">6.21.1.2 What are options for?</a>
144<LI><A href="optparse-what-positional-arguments-for.html">6.21.1.3 What are positional arguments for?</a>
145</ul>
146<LI><A href="optparse-tutorial.html">6.21.2 Tutorial</a>
147<UL>
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<LI><A href="optparse-reference-guide.html">6.21.3 Reference Guide</a>
159<UL>
160<LI><A href="optparse-creating-parser.html">6.21.3.1 Creating the parser</a>
161<LI><A href="optparse-populating-parser.html">6.21.3.2 Populating the parser</a>
162<LI><A href="optparse-defining-options.html">6.21.3.3 Defining options</a>
163<LI><A href="optparse-standard-option-actions.html">6.21.3.4 Standard option actions</a>
164<LI><A href="optparse-option-attributes.html">6.21.3.5 Option attributes</a>
165<LI><A href="optparse-standard-option-types.html">6.21.3.6 Standard option types</a>
166<LI><A href="optparse-parsing-arguments.html">6.21.3.7 Parsing arguments</a>
167<LI><A href="optparse-querying-manipulating-option-parser.html">6.21.3.8 Querying and manipulating your option parser</a>
168<LI><A href="optparse-conflicts-between-options.html">6.21.3.9 Conflicts between options</a>
169<LI><A href="optparse-other-methods.html">6.21.3.10 Other methods</a>
170</ul>
171<LI><A href="optparse-option-callbacks.html">6.21.4 Option Callbacks</a>
172<UL>
173<LI><A href="optparse-defining-callback-option.html">6.21.4.1 Defining a callback option</a>
174<LI><A href="optparse-how-callbacks-called.html">6.21.4.2 How callbacks are called</a>
175<LI><A href="optparse-raising-errors-in-callback.html">6.21.4.3 Raising errors in a callback</a>
176<LI><A href="optparse-callback-example-1.html">6.21.4.4 Callback example 1: trivial callback</a>
177<LI><A href="optparse-callback-example-2.html">6.21.4.5 Callback example 2: check option order</a>
178<LI><A href="optparse-callback-example-3.html">6.21.4.6 Callback example 3: check option order (generalized)</a>
179<LI><A href="optparse-callback-example-4.html">6.21.4.7 Callback example 4: check arbitrary condition</a>
180<LI><A href="optparse-callback-example-5.html">6.21.4.8 Callback example 5: fixed arguments</a>
181<LI><A href="optparse-callback-example-6.html">6.21.4.9 Callback example 6: variable arguments</a>
182</ul>
183<LI><A href="optparse-extending-optparse.html">6.21.5 Extending <tt class="module">optparse</tt></a>
184<UL>
185<LI><A href="optparse-adding-new-types.html">6.21.5.1 Adding new types</a>
186<LI><A href="optparse-adding-new-actions.html">6.21.5.2 Adding new actions</a>
187<LI><A href="optparse-other-reasons-to-extend-optparse.html">6.21.5.3 Other reasons to extend <tt class="module">optparse</tt></a>
188</ul></ul>
189<!--End of Table of Child-Links-->
190</div>
191
192<DIV CLASS="navigation">
193<div class='online-navigation'>
194<p></p><hr />
195<table align="center" width="100%" cellpadding="0" cellspacing="2">
196<tr>
197<td class='online-navigation'><a rel="prev" title="6.20 getopt "
198 href="module-getopt.html"><img src='../icons/previous.png'
199 border='0' height='32' alt='Previous Page' width='32' /></A></td>
200<td class='online-navigation'><a rel="parent" title="6. Generic Operating System"
201 href="allos.html"><img src='../icons/up.png'
202 border='0' height='32' alt='Up One Level' width='32' /></A></td>
203<td class='online-navigation'><a rel="next" title="6.21.1 Background"
204 href="optparse-background.html"><img src='../icons/next.png'
205 border='0' height='32' alt='Next Page' width='32' /></A></td>
206<td align="center" width="100%">Python Library Reference</td>
207<td class='online-navigation'><a rel="contents" title="Table of Contents"
208 href="contents.html"><img src='../icons/contents.png'
209 border='0' height='32' alt='Contents' width='32' /></A></td>
210<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
211 border='0' height='32' alt='Module Index' width='32' /></a></td>
212<td class='online-navigation'><a rel="index" title="Index"
213 href="genindex.html"><img src='../icons/index.png'
214 border='0' height='32' alt='Index' width='32' /></A></td>
215</tr></table>
216<div class='online-navigation'>
217<b class="navlabel">Previous:</b>
218<a class="sectref" rel="prev" href="module-getopt.html">6.20 getopt </A>
219<b class="navlabel">Up:</b>
220<a class="sectref" rel="parent" href="allos.html">6. Generic Operating System</A>
221<b class="navlabel">Next:</b>
222<a class="sectref" rel="next" href="optparse-background.html">6.21.1 Background</A>
223</div>
224</div>
225<hr />
226<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
227</DIV>
228<!--End of Navigation Panel-->
229<ADDRESS>
230See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
231</ADDRESS>
232</BODY>
233</HTML>