Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / module-getopt.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-optparse.html" />
<link rel="prev" href="module-curses.panel.html" />
<link rel="parent" href="allos.html" />
<link rel="next" href="module-optparse.html" />
<meta name='aesop' content='information' />
<title>6.20 getopt -- Parser for command line options</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.19.2 Panel Objects"
href="curses-panel-objects.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="6. Generic Operating System"
href="allos.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="6.21 optparse "
href="module-optparse.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="curses-panel-objects.html">6.19.2 Panel Objects</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="allos.html">6. Generic Operating System</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-optparse.html">6.21 optparse </A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION0082000000000000000000">
6.20 <tt class="module">getopt</tt> --
Parser for command line options</A>
</H1>
<P>
<A NAME="module-getopt"></A>
<P>
This module helps scripts to parse the command line arguments in
<code>sys.argv</code>.
It supports the same conventions as the <span class="Unix">Unix</span> <tt class="cfunction">getopt()</tt>
function (including the special meanings of arguments of the form
`<code>-</code>' and `<code>-</code><code>-</code>').
Long options similar to those supported by
GNU software may be used as well via an optional third argument.
This module provides a single function and an exception:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2193' xml:id='l2h-2193' class="function">getopt</tt></b>(</nobr></td>
<td><var>args, options</var><big>[</big><var>, long_options</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Parses command line options and parameter list. <var>args</var> is the
argument list to be parsed, without the leading reference to the
running program. Typically, this means "<tt class="samp">sys.argv[1:]</tt>".
<var>options</var> is the string of option letters that the script wants to
recognize, with options that require an argument followed by a colon
("<tt class="character">:</tt>"; i.e., the same format that <span class="Unix">Unix</span>
<tt class="cfunction">getopt()</tt> uses).
<P>
<span class="note"><b class="label">Note:</b>
Unlike GNU <tt class="cfunction">getopt()</tt>, after a non-option
argument, all further arguments are considered also non-options.
This is similar to the way non-GNU <span class="Unix">Unix</span> systems work.</span>
<P>
<var>long_options</var>, if specified, must be a list of strings with the
names of the long options which should be supported. The leading
<code>'-</code><code>-'</code> characters should not be included in the option
name. Long options which require an argument should be followed by an
equal sign ("<tt class="character">=</tt>"). To accept only long options,
<var>options</var> should be an empty string. Long options on the command
line can be recognized so long as they provide a prefix of the option
name that matches exactly one of the accepted options. For example,
if <var>long_options</var> is <code>['foo', 'frob']</code>, the option
<b class="programopt">--fo</b> will match as <b class="programopt">--foo</b>, but
<b class="programopt">--f</b> will not match uniquely, so <tt class="exception">GetoptError</tt>
will be raised.
<P>
The return value consists of two elements: the first is a list of
<code>(<var>option</var>, <var>value</var>)</code> pairs; the second is the list of
program arguments left after the option list was stripped (this is a
trailing slice of <var>args</var>). Each option-and-value pair returned
has the option as its first element, prefixed with a hyphen for short
options (e.g., <code>'-x'</code>) or two hyphens for long options (e.g.,
<code>'-</code><code>-long-option'</code>), and the option argument as its second
element, or an empty string if the option has no argument. The
options occur in the list in the same order in which they were found,
thus allowing multiple occurrences. Long and short options may be
mixed.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2194' xml:id='l2h-2194' class="function">gnu_getopt</tt></b>(</nobr></td>
<td><var>args, options</var><big>[</big><var>, long_options</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
This function works like <tt class="function">getopt()</tt>, except that GNU style
scanning mode is used by default. This means that option and
non-option arguments may be intermixed. The <tt class="function">getopt()</tt>
function stops processing options as soon as a non-option argument is
encountered.
<P>
If the first character of the option string is `+', or if the
environment variable POSIXLY_CORRECT is set, then option processing
stops as soon as a non-option argument is encountered.
</dl>
<P>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-2195' xml:id='l2h-2195' class="exception">GetoptError</tt></b></dt>
<dd>
This is raised when an unrecognized option is found in the argument
list or when an option requiring an argument is given none.
The argument to the exception is a string indicating the cause of the
error. For long options, an argument given to an option which does
not require one will also cause this exception to be raised. The
attributes <tt class="member">msg</tt> and <tt class="member">opt</tt> give the error message and
related option; if there is no specific option to which the exception
relates, <tt class="member">opt</tt> is an empty string.
<P>
<span class="versionnote">Changed in version 1.6:
Introduced <tt class="exception">GetoptError</tt> as a synonym for
<tt class="exception">error</tt>.</span>
</dd></dl>
<P>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-2196' xml:id='l2h-2196' class="exception">error</tt></b></dt>
<dd>
Alias for <tt class="exception">GetoptError</tt>; for backward compatibility.
</dd></dl>
<P>
An example using only <span class="Unix">Unix</span> style options:
<P>
<div class="verbatim"><pre>
&gt;&gt;&gt; import getopt
&gt;&gt;&gt; args = '-a -b -cfoo -d bar a1 a2'.split()
&gt;&gt;&gt; args
['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
&gt;&gt;&gt; optlist, args = getopt.getopt(args, 'abc:d:')
&gt;&gt;&gt; optlist
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
&gt;&gt;&gt; args
['a1', 'a2']
</pre></div>
<P>
Using long option names is equally easy:
<P>
<div class="verbatim"><pre>
&gt;&gt;&gt; s = '--condition=foo --testing --output-file abc.def -x a1 a2'
&gt;&gt;&gt; args = s.split()
&gt;&gt;&gt; args
['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
&gt;&gt;&gt; optlist, args = getopt.getopt(args, 'x', [
... 'condition=', 'output-file=', 'testing'])
&gt;&gt;&gt; optlist
[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x',
'')]
&gt;&gt;&gt; args
['a1', 'a2']
</pre></div>
<P>
In a script, typical usage is something like this:
<P>
<div class="verbatim"><pre>
import getopt, sys
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
except getopt.GetoptError:
# print help information and exit:
usage()
sys.exit(2)
output = None
verbose = False
for o, a in opts:
if o == "-v":
verbose = True
if o in ("-h", "--help"):
usage()
sys.exit()
if o in ("-o", "--output"):
output = a
# ...
if __name__ == "__main__":
main()
</pre></div>
<P>
<div class="seealso">
<p class="heading">See Also:</p>
<dl compact="compact" class="seemodule">
<dt>Module <b><tt class="module"><a href="module-optparse.html">optparse</a></tt>:</b>
<dd>More object-oriented command line option parsing.
</dl>
</div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.19.2 Panel Objects"
href="curses-panel-objects.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="6. Generic Operating System"
href="allos.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="6.21 optparse "
href="module-optparse.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="curses-panel-objects.html">6.19.2 Panel Objects</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="allos.html">6. Generic Operating System</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-optparse.html">6.21 optparse </A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>