<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>6.18 getopt -- Parser for command line options</title> <META NAME="description" CONTENT="6.18 getopt -- Parser for command line options"> <META NAME="keywords" CONTENT="lib"> <META NAME="resource-type" CONTENT="document"> <META NAME="distribution" CONTENT="global"> <meta http-equiv="Content-Type" content="text/html; charset="> <link rel="STYLESHEET" href="lib.css"> <link rel="first" href="lib.html"> <link rel="contents" href="contents.html" title="Contents"> <link rel="index" href="genindex.html" title="Index"> <LINK REL="next" href="module-tempfile.html"> <LINK REL="previous" href="module-curses.panel.html"> <LINK REL="up" href="allos.html"> <LINK REL="next" href="module-tempfile.html"> </head> <body> <DIV CLASS="navigation"> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="curses-panel-objects.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="allos.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="module-tempfile.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Python Library Reference</td> <td><A href="contents.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></A></td> <td><a href="modindex.html" title="Module Index"><img src="../icons/modules.gif" border="0" height="32" alt="Module Index" width="32"></a></td> <td><A href="genindex.html"><img src="../icons/index.gif" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="curses-panel-objects.html">6.17.2 Panel Objects</A> <b class="navlabel">Up:</b> <a class="sectref" href="allos.html">6. Generic Operating System</A> <b class="navlabel">Next:</b> <a class="sectref" href="module-tempfile.html">6.19 tempfile </A> <br><hr> </DIV> <!--End of Navigation Panel--> <H1><A NAME="SECTION0081800000000000000000"> 6.18 <tt class="module">getopt</tt> -- Parser for command line options</A> </H1> <P> <P> This module helps scripts to parse the command line arguments in <code>sys.argv</code>. It supports the same conventions as the Unix <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><b><a name="l2h-1608"><tt class="function">getopt</tt></a></b>(<var>args, options</var><big>[</big><var>, long_options</var><big>]</big>) <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 Unix <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 Unix 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, it <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><b><span class="typelabel">exception</span> <a name="l2h-1609"><tt class="exception">GetoptError</tt></a></b> <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> </dl> <P> <dl><dt><b><span class="typelabel">exception</span> <a name="l2h-1610"><tt class="exception">error</tt></a></b> <dd> Alias for <tt class="exception">GetoptError</tt>; for backward compatibility. </dl> <P> An example using only Unix style options: <P> <dl><dd><pre class="verbatim"> >>> import getopt >>> args = '-a -b -cfoo -d bar a1 a2'.split() >>> args ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] >>> optlist, args = getopt.getopt(args, 'abc:d:') >>> optlist [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] >>> args ['a1', 'a2'] </pre></dl> <P> Using long option names is equally easy: <P> <dl><dd><pre class="verbatim"> >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' >>> args = s.split() >>> args ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] >>> optlist, args = getopt.getopt(args, 'x', [ ... 'condition=', 'output-file=', 'testing']) >>> optlist [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')] >>> args ['a1', 'a2'] </pre></dl> <P> In a script, typical usage is something like this: <P> <dl><dd><pre class="verbatim"> import getopt, sys def main(): try: opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="]) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) output = None for o, a in opts: if o in ("-h", "--help"): usage() sys.exit() if o in ("-o", "--output"): output = a # ... if __name__ == "__main__": main() </pre></dl> <DIV CLASS="navigation"> <p><hr> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="curses-panel-objects.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="allos.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="module-tempfile.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Python Library Reference</td> <td><A href="contents.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></A></td> <td><a href="modindex.html" title="Module Index"><img src="../icons/modules.gif" border="0" height="32" alt="Module Index" width="32"></a></td> <td><A href="genindex.html"><img src="../icons/index.gif" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="curses-panel-objects.html">6.17.2 Panel Objects</A> <b class="navlabel">Up:</b> <a class="sectref" href="allos.html">6. Generic Operating System</A> <b class="navlabel">Next:</b> <a class="sectref" href="module-tempfile.html">6.19 tempfile </A> <hr> <span class="release-info">Release 2.2, documentation updated on December 21, 2001.</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>