| 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-optparse.html" /> |
| 13 | <link rel="prev" href="module-curses.panel.html" /> |
| 14 | <link rel="parent" href="allos.html" /> |
| 15 | <link rel="next" href="module-optparse.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>6.20 getopt -- Parser for command line options</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.19.2 Panel Objects" |
| 25 | href="curses-panel-objects.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 optparse " |
| 31 | href="module-optparse.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="curses-panel-objects.html">6.19.2 Panel Objects</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="module-optparse.html">6.21 optparse </A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION0082000000000000000000"> |
| 56 | 6.20 <tt class="module">getopt</tt> -- |
| 57 | Parser for command line options</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-getopt"></A> |
| 62 | |
| 63 | <P> |
| 64 | This module helps scripts to parse the command line arguments in |
| 65 | <code>sys.argv</code>. |
| 66 | It supports the same conventions as the <span class="Unix">Unix</span> <tt class="cfunction">getopt()</tt> |
| 67 | function (including the special meanings of arguments of the form |
| 68 | `<code>-</code>' and `<code>-</code><code>-</code>'). |
| 69 | Long options similar to those supported by |
| 70 | GNU software may be used as well via an optional third argument. |
| 71 | This module provides a single function and an exception: |
| 72 | |
| 73 | <P> |
| 74 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 75 | <td><nobr><b><tt id='l2h-2193' xml:id='l2h-2193' class="function">getopt</tt></b>(</nobr></td> |
| 76 | <td><var>args, options</var><big>[</big><var>, long_options</var><big>]</big><var></var>)</td></tr></table></dt> |
| 77 | <dd> |
| 78 | Parses command line options and parameter list. <var>args</var> is the |
| 79 | argument list to be parsed, without the leading reference to the |
| 80 | running program. Typically, this means "<tt class="samp">sys.argv[1:]</tt>". |
| 81 | <var>options</var> is the string of option letters that the script wants to |
| 82 | recognize, with options that require an argument followed by a colon |
| 83 | ("<tt class="character">:</tt>"; i.e., the same format that <span class="Unix">Unix</span> |
| 84 | <tt class="cfunction">getopt()</tt> uses). |
| 85 | |
| 86 | <P> |
| 87 | <span class="note"><b class="label">Note:</b> |
| 88 | Unlike GNU <tt class="cfunction">getopt()</tt>, after a non-option |
| 89 | argument, all further arguments are considered also non-options. |
| 90 | This is similar to the way non-GNU <span class="Unix">Unix</span> systems work.</span> |
| 91 | |
| 92 | <P> |
| 93 | <var>long_options</var>, if specified, must be a list of strings with the |
| 94 | names of the long options which should be supported. The leading |
| 95 | <code>'-</code><code>-'</code> characters should not be included in the option |
| 96 | name. Long options which require an argument should be followed by an |
| 97 | equal sign ("<tt class="character">=</tt>"). To accept only long options, |
| 98 | <var>options</var> should be an empty string. Long options on the command |
| 99 | line can be recognized so long as they provide a prefix of the option |
| 100 | name that matches exactly one of the accepted options. For example, |
| 101 | if <var>long_options</var> is <code>['foo', 'frob']</code>, the option |
| 102 | <b class="programopt">--fo</b> will match as <b class="programopt">--foo</b>, but |
| 103 | <b class="programopt">--f</b> will not match uniquely, so <tt class="exception">GetoptError</tt> |
| 104 | will be raised. |
| 105 | |
| 106 | <P> |
| 107 | The return value consists of two elements: the first is a list of |
| 108 | <code>(<var>option</var>, <var>value</var>)</code> pairs; the second is the list of |
| 109 | program arguments left after the option list was stripped (this is a |
| 110 | trailing slice of <var>args</var>). Each option-and-value pair returned |
| 111 | has the option as its first element, prefixed with a hyphen for short |
| 112 | options (e.g., <code>'-x'</code>) or two hyphens for long options (e.g., |
| 113 | <code>'-</code><code>-long-option'</code>), and the option argument as its second |
| 114 | element, or an empty string if the option has no argument. The |
| 115 | options occur in the list in the same order in which they were found, |
| 116 | thus allowing multiple occurrences. Long and short options may be |
| 117 | mixed. |
| 118 | </dl> |
| 119 | |
| 120 | <P> |
| 121 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 122 | <td><nobr><b><tt id='l2h-2194' xml:id='l2h-2194' class="function">gnu_getopt</tt></b>(</nobr></td> |
| 123 | <td><var>args, options</var><big>[</big><var>, long_options</var><big>]</big><var></var>)</td></tr></table></dt> |
| 124 | <dd> |
| 125 | This function works like <tt class="function">getopt()</tt>, except that GNU style |
| 126 | scanning mode is used by default. This means that option and |
| 127 | non-option arguments may be intermixed. The <tt class="function">getopt()</tt> |
| 128 | function stops processing options as soon as a non-option argument is |
| 129 | encountered. |
| 130 | |
| 131 | <P> |
| 132 | If the first character of the option string is `+', or if the |
| 133 | environment variable POSIXLY_CORRECT is set, then option processing |
| 134 | stops as soon as a non-option argument is encountered. |
| 135 | </dl> |
| 136 | |
| 137 | <P> |
| 138 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-2195' xml:id='l2h-2195' class="exception">GetoptError</tt></b></dt> |
| 139 | <dd> |
| 140 | This is raised when an unrecognized option is found in the argument |
| 141 | list or when an option requiring an argument is given none. |
| 142 | The argument to the exception is a string indicating the cause of the |
| 143 | error. For long options, an argument given to an option which does |
| 144 | not require one will also cause this exception to be raised. The |
| 145 | attributes <tt class="member">msg</tt> and <tt class="member">opt</tt> give the error message and |
| 146 | related option; if there is no specific option to which the exception |
| 147 | relates, <tt class="member">opt</tt> is an empty string. |
| 148 | |
| 149 | <P> |
| 150 | |
| 151 | <span class="versionnote">Changed in version 1.6: |
| 152 | Introduced <tt class="exception">GetoptError</tt> as a synonym for |
| 153 | <tt class="exception">error</tt>.</span> |
| 154 | |
| 155 | </dd></dl> |
| 156 | |
| 157 | <P> |
| 158 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-2196' xml:id='l2h-2196' class="exception">error</tt></b></dt> |
| 159 | <dd> |
| 160 | Alias for <tt class="exception">GetoptError</tt>; for backward compatibility. |
| 161 | </dd></dl> |
| 162 | |
| 163 | <P> |
| 164 | An example using only <span class="Unix">Unix</span> style options: |
| 165 | |
| 166 | <P> |
| 167 | <div class="verbatim"><pre> |
| 168 | >>> import getopt |
| 169 | >>> args = '-a -b -cfoo -d bar a1 a2'.split() |
| 170 | >>> args |
| 171 | ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
| 172 | >>> optlist, args = getopt.getopt(args, 'abc:d:') |
| 173 | >>> optlist |
| 174 | [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
| 175 | >>> args |
| 176 | ['a1', 'a2'] |
| 177 | </pre></div> |
| 178 | |
| 179 | <P> |
| 180 | Using long option names is equally easy: |
| 181 | |
| 182 | <P> |
| 183 | <div class="verbatim"><pre> |
| 184 | >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' |
| 185 | >>> args = s.split() |
| 186 | >>> args |
| 187 | ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] |
| 188 | >>> optlist, args = getopt.getopt(args, 'x', [ |
| 189 | ... 'condition=', 'output-file=', 'testing']) |
| 190 | >>> optlist |
| 191 | [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', |
| 192 | '')] |
| 193 | >>> args |
| 194 | ['a1', 'a2'] |
| 195 | </pre></div> |
| 196 | |
| 197 | <P> |
| 198 | In a script, typical usage is something like this: |
| 199 | |
| 200 | <P> |
| 201 | <div class="verbatim"><pre> |
| 202 | import getopt, sys |
| 203 | |
| 204 | def main(): |
| 205 | try: |
| 206 | opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="]) |
| 207 | except getopt.GetoptError: |
| 208 | # print help information and exit: |
| 209 | usage() |
| 210 | sys.exit(2) |
| 211 | output = None |
| 212 | verbose = False |
| 213 | for o, a in opts: |
| 214 | if o == "-v": |
| 215 | verbose = True |
| 216 | if o in ("-h", "--help"): |
| 217 | usage() |
| 218 | sys.exit() |
| 219 | if o in ("-o", "--output"): |
| 220 | output = a |
| 221 | # ... |
| 222 | |
| 223 | if __name__ == "__main__": |
| 224 | main() |
| 225 | </pre></div> |
| 226 | |
| 227 | <P> |
| 228 | <div class="seealso"> |
| 229 | <p class="heading">See Also:</p> |
| 230 | |
| 231 | <dl compact="compact" class="seemodule"> |
| 232 | <dt>Module <b><tt class="module"><a href="module-optparse.html">optparse</a></tt>:</b> |
| 233 | <dd>More object-oriented command line option parsing. |
| 234 | </dl> |
| 235 | </div> |
| 236 | |
| 237 | <DIV CLASS="navigation"> |
| 238 | <div class='online-navigation'> |
| 239 | <p></p><hr /> |
| 240 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 241 | <tr> |
| 242 | <td class='online-navigation'><a rel="prev" title="6.19.2 Panel Objects" |
| 243 | href="curses-panel-objects.html"><img src='../icons/previous.png' |
| 244 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 245 | <td class='online-navigation'><a rel="parent" title="6. Generic Operating System" |
| 246 | href="allos.html"><img src='../icons/up.png' |
| 247 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 248 | <td class='online-navigation'><a rel="next" title="6.21 optparse " |
| 249 | href="module-optparse.html"><img src='../icons/next.png' |
| 250 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 251 | <td align="center" width="100%">Python Library Reference</td> |
| 252 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 253 | href="contents.html"><img src='../icons/contents.png' |
| 254 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 255 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 256 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 257 | <td class='online-navigation'><a rel="index" title="Index" |
| 258 | href="genindex.html"><img src='../icons/index.png' |
| 259 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 260 | </tr></table> |
| 261 | <div class='online-navigation'> |
| 262 | <b class="navlabel">Previous:</b> |
| 263 | <a class="sectref" rel="prev" href="curses-panel-objects.html">6.19.2 Panel Objects</A> |
| 264 | <b class="navlabel">Up:</b> |
| 265 | <a class="sectref" rel="parent" href="allos.html">6. Generic Operating System</A> |
| 266 | <b class="navlabel">Next:</b> |
| 267 | <a class="sectref" rel="next" href="module-optparse.html">6.21 optparse </A> |
| 268 | </div> |
| 269 | </div> |
| 270 | <hr /> |
| 271 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 272 | </DIV> |
| 273 | <!--End of Navigation Panel--> |
| 274 | <ADDRESS> |
| 275 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 276 | </ADDRESS> |
| 277 | </BODY> |
| 278 | </HTML> |