gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

466 lines
32KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="getopt — C-style parser for command line options" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/getopt.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/getopt.py This module helps scripts to parse the command line arguments in sys.argv. It supports the same conventions as the Unix getopt() function (including the special meanings ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/getopt.py This module helps scripts to parse the command line arguments in sys.argv. It supports the same conventions as the Unix getopt() function (including the special meanings ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>getopt — C-style parser for command line options &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="logging — Logging facility for Python" href="logging.html" />
  33. <link rel="prev" title="argparse — Parser for command-line options, arguments and sub-commands" href="argparse.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/getopt.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h4>Previous topic</h4>
  84. <p class="topless"><a href="argparse.html"
  85. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">argparse</span></code> — Parser for command-line options, arguments and sub-commands</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="logging.html"
  90. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code> — Logging facility for Python</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/library/getopt.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="logging.html" title="logging — Logging facility for Python"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="argparse.html" title="argparse — Parser for command-line options, arguments and sub-commands"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  133. <li class="nav-item nav-item-2"><a href="allos.html" accesskey="U">Generic Operating System Services</a> &#187;</li>
  134. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">getopt</span></code> — C-style parser for command line options</a></li>
  135. <li class="right">
  136. <div class="inline-search" role="search">
  137. <form class="inline-search" action="../search.html" method="get">
  138. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  139. <input type="submit" value="Go" />
  140. </form>
  141. </div>
  142. |
  143. </li>
  144. <li class="right">
  145. <label class="theme-selector-label">
  146. Theme
  147. <select class="theme-selector" oninput="activateTheme(this.value)">
  148. <option value="auto" selected>Auto</option>
  149. <option value="light">Light</option>
  150. <option value="dark">Dark</option>
  151. </select>
  152. </label> |</li>
  153. </ul>
  154. </div>
  155. <div class="document">
  156. <div class="documentwrapper">
  157. <div class="bodywrapper">
  158. <div class="body" role="main">
  159. <section id="module-getopt">
  160. <span id="getopt-c-style-parser-for-command-line-options"></span><h1><a class="reference internal" href="#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><code class="xref py py-mod docutils literal notranslate"><span class="pre">getopt</span></code></a> — C-style parser for command line options<a class="headerlink" href="#module-getopt" title="Link to this heading">¶</a></h1>
  161. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/getopt.py">Lib/getopt.py</a></p>
  162. <div class="admonition note">
  163. <p class="admonition-title">Note</p>
  164. <p>The <a class="reference internal" href="#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><code class="xref py py-mod docutils literal notranslate"><span class="pre">getopt</span></code></a> module is a parser for command line options whose API is
  165. designed to be familiar to users of the C <code class="xref c c-func docutils literal notranslate"><span class="pre">getopt()</span></code> function. Users who
  166. are unfamiliar with the C <code class="xref c c-func docutils literal notranslate"><span class="pre">getopt()</span></code> function or who would like to write
  167. less code and get better help and error messages should consider using the
  168. <a class="reference internal" href="argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">argparse</span></code></a> module instead.</p>
  169. </div>
  170. <hr class="docutils" />
  171. <p>This module helps scripts to parse the command line arguments in <code class="docutils literal notranslate"><span class="pre">sys.argv</span></code>.
  172. It supports the same conventions as the Unix <code class="xref c c-func docutils literal notranslate"><span class="pre">getopt()</span></code> function (including
  173. the special meanings of arguments of the form ‘<code class="docutils literal notranslate"><span class="pre">-</span></code>’ and ‘<code class="docutils literal notranslate"><span class="pre">--</span></code>‘). Long
  174. options similar to those supported by GNU software may be used as well via an
  175. optional third argument.</p>
  176. <p>This module provides two functions and an
  177. exception:</p>
  178. <dl class="py function">
  179. <dt class="sig sig-object py" id="getopt.getopt">
  180. <span class="sig-prename descclassname"><span class="pre">getopt.</span></span><span class="sig-name descname"><span class="pre">getopt</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">shortopts</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">longopts</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">[]</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#getopt.getopt" title="Link to this definition">¶</a></dt>
  181. <dd><p>Parses command line options and parameter list. <em>args</em> is the argument list to
  182. be parsed, without the leading reference to the running program. Typically, this
  183. means <code class="docutils literal notranslate"><span class="pre">sys.argv[1:]</span></code>. <em>shortopts</em> is the string of option letters that the
  184. script wants to recognize, with options that require an argument followed by a
  185. colon (<code class="docutils literal notranslate"><span class="pre">':'</span></code>; i.e., the same format that Unix <code class="xref c c-func docutils literal notranslate"><span class="pre">getopt()</span></code> uses).</p>
  186. <div class="admonition note">
  187. <p class="admonition-title">Note</p>
  188. <p>Unlike GNU <code class="xref c c-func docutils literal notranslate"><span class="pre">getopt()</span></code>, after a non-option argument, all further
  189. arguments are considered also non-options. This is similar to the way
  190. non-GNU Unix systems work.</p>
  191. </div>
  192. <p><em>longopts</em>, if specified, must be a list of strings with the names of the
  193. long options which should be supported. The leading <code class="docutils literal notranslate"><span class="pre">'--'</span></code> characters
  194. should not be included in the option name. Long options which require an
  195. argument should be followed by an equal sign (<code class="docutils literal notranslate"><span class="pre">'='</span></code>). Optional arguments
  196. are not supported. To accept only long options, <em>shortopts</em> should be an
  197. empty string. Long options on the command line can be recognized so long as
  198. they provide a prefix of the option name that matches exactly one of the
  199. accepted options. For example, if <em>longopts</em> is <code class="docutils literal notranslate"><span class="pre">['foo',</span> <span class="pre">'frob']</span></code>, the
  200. option <code class="docutils literal notranslate"><span class="pre">--fo</span></code> will match as <code class="docutils literal notranslate"><span class="pre">--foo</span></code>, but <code class="docutils literal notranslate"><span class="pre">--f</span></code> will
  201. not match uniquely, so <a class="reference internal" href="#getopt.GetoptError" title="getopt.GetoptError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">GetoptError</span></code></a> will be raised.</p>
  202. <p>The return value consists of two elements: the first is a list of <code class="docutils literal notranslate"><span class="pre">(option,</span>
  203. <span class="pre">value)</span></code> pairs; the second is the list of program arguments left after the
  204. option list was stripped (this is a trailing slice of <em>args</em>). Each
  205. option-and-value pair returned has the option as its first element, prefixed
  206. with a hyphen for short options (e.g., <code class="docutils literal notranslate"><span class="pre">'-x'</span></code>) or two hyphens for long
  207. options (e.g., <code class="docutils literal notranslate"><span class="pre">'--long-option'</span></code>), and the option argument as its
  208. second element, or an empty string if the option has no argument. The
  209. options occur in the list in the same order in which they were found, thus
  210. allowing multiple occurrences. Long and short options may be mixed.</p>
  211. </dd></dl>
  212. <dl class="py function">
  213. <dt class="sig sig-object py" id="getopt.gnu_getopt">
  214. <span class="sig-prename descclassname"><span class="pre">getopt.</span></span><span class="sig-name descname"><span class="pre">gnu_getopt</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">shortopts</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">longopts</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">[]</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#getopt.gnu_getopt" title="Link to this definition">¶</a></dt>
  215. <dd><p>This function works like <a class="reference internal" href="#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><code class="xref py py-func docutils literal notranslate"><span class="pre">getopt()</span></code></a>, except that GNU style scanning mode is
  216. used by default. This means that option and non-option arguments may be
  217. intermixed. The <a class="reference internal" href="#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><code class="xref py py-func docutils literal notranslate"><span class="pre">getopt()</span></code></a> function stops processing options as soon as a
  218. non-option argument is encountered.</p>
  219. <p>If the first character of the option string is <code class="docutils literal notranslate"><span class="pre">'+'</span></code>, or if the environment
  220. variable <span class="target" id="index-0"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">POSIXLY_CORRECT</span></code> is set, then option processing stops as
  221. soon as a non-option argument is encountered.</p>
  222. </dd></dl>
  223. <dl class="py exception">
  224. <dt class="sig sig-object py" id="getopt.GetoptError">
  225. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">getopt.</span></span><span class="sig-name descname"><span class="pre">GetoptError</span></span><a class="headerlink" href="#getopt.GetoptError" title="Link to this definition">¶</a></dt>
  226. <dd><p>This is raised when an unrecognized option is found in the argument list or when
  227. an option requiring an argument is given none. The argument to the exception is
  228. a string indicating the cause of the error. For long options, an argument given
  229. to an option which does not require one will also cause this exception to be
  230. raised. The attributes <code class="xref py py-attr docutils literal notranslate"><span class="pre">msg</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">opt</span></code> give the error message and
  231. related option; if there is no specific option to which the exception relates,
  232. <code class="xref py py-attr docutils literal notranslate"><span class="pre">opt</span></code> is an empty string.</p>
  233. </dd></dl>
  234. <dl class="py exception">
  235. <dt class="sig sig-object py" id="getopt.error">
  236. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">getopt.</span></span><span class="sig-name descname"><span class="pre">error</span></span><a class="headerlink" href="#getopt.error" title="Link to this definition">¶</a></dt>
  237. <dd><p>Alias for <a class="reference internal" href="#getopt.GetoptError" title="getopt.GetoptError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">GetoptError</span></code></a>; for backward compatibility.</p>
  238. </dd></dl>
  239. <p>An example using only Unix style options:</p>
  240. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">getopt</span>
  241. <span class="gp">&gt;&gt;&gt; </span><span class="n">args</span> <span class="o">=</span> <span class="s1">&#39;-a -b -cfoo -d bar a1 a2&#39;</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
  242. <span class="gp">&gt;&gt;&gt; </span><span class="n">args</span>
  243. <span class="go">[&#39;-a&#39;, &#39;-b&#39;, &#39;-cfoo&#39;, &#39;-d&#39;, &#39;bar&#39;, &#39;a1&#39;, &#39;a2&#39;]</span>
  244. <span class="gp">&gt;&gt;&gt; </span><span class="n">optlist</span><span class="p">,</span> <span class="n">args</span> <span class="o">=</span> <span class="n">getopt</span><span class="o">.</span><span class="n">getopt</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="s1">&#39;abc:d:&#39;</span><span class="p">)</span>
  245. <span class="gp">&gt;&gt;&gt; </span><span class="n">optlist</span>
  246. <span class="go">[(&#39;-a&#39;, &#39;&#39;), (&#39;-b&#39;, &#39;&#39;), (&#39;-c&#39;, &#39;foo&#39;), (&#39;-d&#39;, &#39;bar&#39;)]</span>
  247. <span class="gp">&gt;&gt;&gt; </span><span class="n">args</span>
  248. <span class="go">[&#39;a1&#39;, &#39;a2&#39;]</span>
  249. </pre></div>
  250. </div>
  251. <p>Using long option names is equally easy:</p>
  252. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;--condition=foo --testing --output-file abc.def -x a1 a2&#39;</span>
  253. <span class="gp">&gt;&gt;&gt; </span><span class="n">args</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
  254. <span class="gp">&gt;&gt;&gt; </span><span class="n">args</span>
  255. <span class="go">[&#39;--condition=foo&#39;, &#39;--testing&#39;, &#39;--output-file&#39;, &#39;abc.def&#39;, &#39;-x&#39;, &#39;a1&#39;, &#39;a2&#39;]</span>
  256. <span class="gp">&gt;&gt;&gt; </span><span class="n">optlist</span><span class="p">,</span> <span class="n">args</span> <span class="o">=</span> <span class="n">getopt</span><span class="o">.</span><span class="n">getopt</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="p">[</span>
  257. <span class="gp">... </span> <span class="s1">&#39;condition=&#39;</span><span class="p">,</span> <span class="s1">&#39;output-file=&#39;</span><span class="p">,</span> <span class="s1">&#39;testing&#39;</span><span class="p">])</span>
  258. <span class="gp">&gt;&gt;&gt; </span><span class="n">optlist</span>
  259. <span class="go">[(&#39;--condition&#39;, &#39;foo&#39;), (&#39;--testing&#39;, &#39;&#39;), (&#39;--output-file&#39;, &#39;abc.def&#39;), (&#39;-x&#39;, &#39;&#39;)]</span>
  260. <span class="gp">&gt;&gt;&gt; </span><span class="n">args</span>
  261. <span class="go">[&#39;a1&#39;, &#39;a2&#39;]</span>
  262. </pre></div>
  263. </div>
  264. <p>In a script, typical usage is something like this:</p>
  265. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">getopt</span><span class="o">,</span> <span class="nn">sys</span>
  266. <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  267. <span class="k">try</span><span class="p">:</span>
  268. <span class="n">opts</span><span class="p">,</span> <span class="n">args</span> <span class="o">=</span> <span class="n">getopt</span><span class="o">.</span><span class="n">getopt</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="s2">&quot;ho:v&quot;</span><span class="p">,</span> <span class="p">[</span><span class="s2">&quot;help&quot;</span><span class="p">,</span> <span class="s2">&quot;output=&quot;</span><span class="p">])</span>
  269. <span class="k">except</span> <span class="n">getopt</span><span class="o">.</span><span class="n">GetoptError</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
  270. <span class="c1"># print help information and exit:</span>
  271. <span class="nb">print</span><span class="p">(</span><span class="n">err</span><span class="p">)</span> <span class="c1"># will print something like &quot;option -a not recognized&quot;</span>
  272. <span class="n">usage</span><span class="p">()</span>
  273. <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
  274. <span class="n">output</span> <span class="o">=</span> <span class="kc">None</span>
  275. <span class="n">verbose</span> <span class="o">=</span> <span class="kc">False</span>
  276. <span class="k">for</span> <span class="n">o</span><span class="p">,</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">opts</span><span class="p">:</span>
  277. <span class="k">if</span> <span class="n">o</span> <span class="o">==</span> <span class="s2">&quot;-v&quot;</span><span class="p">:</span>
  278. <span class="n">verbose</span> <span class="o">=</span> <span class="kc">True</span>
  279. <span class="k">elif</span> <span class="n">o</span> <span class="ow">in</span> <span class="p">(</span><span class="s2">&quot;-h&quot;</span><span class="p">,</span> <span class="s2">&quot;--help&quot;</span><span class="p">):</span>
  280. <span class="n">usage</span><span class="p">()</span>
  281. <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>
  282. <span class="k">elif</span> <span class="n">o</span> <span class="ow">in</span> <span class="p">(</span><span class="s2">&quot;-o&quot;</span><span class="p">,</span> <span class="s2">&quot;--output&quot;</span><span class="p">):</span>
  283. <span class="n">output</span> <span class="o">=</span> <span class="n">a</span>
  284. <span class="k">else</span><span class="p">:</span>
  285. <span class="k">assert</span> <span class="kc">False</span><span class="p">,</span> <span class="s2">&quot;unhandled option&quot;</span>
  286. <span class="c1"># ...</span>
  287. <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
  288. <span class="n">main</span><span class="p">()</span>
  289. </pre></div>
  290. </div>
  291. <p>Note that an equivalent command line interface could be produced with less code
  292. and more informative help and error messages by using the <a class="reference internal" href="argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">argparse</span></code></a> module:</p>
  293. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">argparse</span>
  294. <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
  295. <span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">()</span>
  296. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;-o&#39;</span><span class="p">,</span> <span class="s1">&#39;--output&#39;</span><span class="p">)</span>
  297. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;-v&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;verbose&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">)</span>
  298. <span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
  299. <span class="c1"># ... do something with args.output ...</span>
  300. <span class="c1"># ... do something with args.verbose ..</span>
  301. </pre></div>
  302. </div>
  303. <div class="admonition seealso">
  304. <p class="admonition-title">See also</p>
  305. <dl class="simple">
  306. <dt>Module <a class="reference internal" href="argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">argparse</span></code></a></dt><dd><p>Alternative command line option and argument parsing library.</p>
  307. </dd>
  308. </dl>
  309. </div>
  310. </section>
  311. <div class="clearer"></div>
  312. </div>
  313. </div>
  314. </div>
  315. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  316. <div class="sphinxsidebarwrapper">
  317. <div>
  318. <h4>Previous topic</h4>
  319. <p class="topless"><a href="argparse.html"
  320. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">argparse</span></code> — Parser for command-line options, arguments and sub-commands</a></p>
  321. </div>
  322. <div>
  323. <h4>Next topic</h4>
  324. <p class="topless"><a href="logging.html"
  325. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code> — Logging facility for Python</a></p>
  326. </div>
  327. <div role="note" aria-label="source link">
  328. <h3>This Page</h3>
  329. <ul class="this-page-menu">
  330. <li><a href="../bugs.html">Report a Bug</a></li>
  331. <li>
  332. <a href="https://github.com/python/cpython/blob/main/Doc/library/getopt.rst"
  333. rel="nofollow">Show Source
  334. </a>
  335. </li>
  336. </ul>
  337. </div>
  338. </div>
  339. <div id="sidebarbutton" title="Collapse sidebar">
  340. <span>«</span>
  341. </div>
  342. </div>
  343. <div class="clearer"></div>
  344. </div>
  345. <div class="related" role="navigation" aria-label="related navigation">
  346. <h3>Navigation</h3>
  347. <ul>
  348. <li class="right" style="margin-right: 10px">
  349. <a href="../genindex.html" title="General Index"
  350. >index</a></li>
  351. <li class="right" >
  352. <a href="../py-modindex.html" title="Python Module Index"
  353. >modules</a> |</li>
  354. <li class="right" >
  355. <a href="logging.html" title="logging — Logging facility for Python"
  356. >next</a> |</li>
  357. <li class="right" >
  358. <a href="argparse.html" title="argparse — Parser for command-line options, arguments and sub-commands"
  359. >previous</a> |</li>
  360. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  361. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  362. <li class="switchers">
  363. <div class="language_switcher_placeholder"></div>
  364. <div class="version_switcher_placeholder"></div>
  365. </li>
  366. <li>
  367. </li>
  368. <li id="cpython-language-and-version">
  369. <a href="../index.html">3.12.3 Documentation</a> &#187;
  370. </li>
  371. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  372. <li class="nav-item nav-item-2"><a href="allos.html" >Generic Operating System Services</a> &#187;</li>
  373. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">getopt</span></code> — C-style parser for command line options</a></li>
  374. <li class="right">
  375. <div class="inline-search" role="search">
  376. <form class="inline-search" action="../search.html" method="get">
  377. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  378. <input type="submit" value="Go" />
  379. </form>
  380. </div>
  381. |
  382. </li>
  383. <li class="right">
  384. <label class="theme-selector-label">
  385. Theme
  386. <select class="theme-selector" oninput="activateTheme(this.value)">
  387. <option value="auto" selected>Auto</option>
  388. <option value="light">Light</option>
  389. <option value="dark">Dark</option>
  390. </select>
  391. </label> |</li>
  392. </ul>
  393. </div>
  394. <div class="footer">
  395. &copy;
  396. <a href="../copyright.html">
  397. Copyright
  398. </a>
  399. 2001-2024, Python Software Foundation.
  400. <br />
  401. This page is licensed under the Python Software Foundation License Version 2.
  402. <br />
  403. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  404. <br />
  405. See <a href="/license.html">History and License</a> for more information.<br />
  406. <br />
  407. The Python Software Foundation is a non-profit corporation.
  408. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  409. <br />
  410. <br />
  411. Last updated on Apr 09, 2024 (13:47 UTC).
  412. <a href="/bugs.html">Found a bug</a>?
  413. <br />
  414. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  415. </div>
  416. </body>
  417. </html>
上海开阖软件有限公司 沪ICP备12045867号-1