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.

2297 line
268KB

  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="optparse — Parser for command line options" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/optparse.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/optparse.py optparse is a more convenient, flexible, and powerful library for parsing command-line options than the old getopt module. optparse uses a more declarative style of com..." />
  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/optparse.py optparse is a more convenient, flexible, and powerful library for parsing command-line options than the old getopt module. optparse uses a more declarative style of com..." />
  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>optparse — 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="ossaudiodev — Access to OSS-compatible audio devices" href="ossaudiodev.html" />
  33. <link rel="prev" title="nntplib — NNTP protocol client" href="nntplib.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/optparse.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. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code> — Parser for command line options</a><ul>
  86. <li><a class="reference internal" href="#background">Background</a><ul>
  87. <li><a class="reference internal" href="#terminology">Terminology</a></li>
  88. <li><a class="reference internal" href="#what-are-options-for">What are options for?</a></li>
  89. <li><a class="reference internal" href="#what-are-positional-arguments-for">What are positional arguments for?</a></li>
  90. </ul>
  91. </li>
  92. <li><a class="reference internal" href="#tutorial">Tutorial</a><ul>
  93. <li><a class="reference internal" href="#understanding-option-actions">Understanding option actions</a></li>
  94. <li><a class="reference internal" href="#the-store-action">The store action</a></li>
  95. <li><a class="reference internal" href="#handling-boolean-flag-options">Handling boolean (flag) options</a></li>
  96. <li><a class="reference internal" href="#other-actions">Other actions</a></li>
  97. <li><a class="reference internal" href="#default-values">Default values</a></li>
  98. <li><a class="reference internal" href="#generating-help">Generating help</a><ul>
  99. <li><a class="reference internal" href="#grouping-options">Grouping Options</a></li>
  100. </ul>
  101. </li>
  102. <li><a class="reference internal" href="#printing-a-version-string">Printing a version string</a></li>
  103. <li><a class="reference internal" href="#how-optparse-handles-errors">How <code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code> handles errors</a></li>
  104. <li><a class="reference internal" href="#putting-it-all-together">Putting it all together</a></li>
  105. </ul>
  106. </li>
  107. <li><a class="reference internal" href="#reference-guide">Reference Guide</a><ul>
  108. <li><a class="reference internal" href="#creating-the-parser">Creating the parser</a></li>
  109. <li><a class="reference internal" href="#populating-the-parser">Populating the parser</a></li>
  110. <li><a class="reference internal" href="#defining-options">Defining options</a></li>
  111. <li><a class="reference internal" href="#option-attributes">Option attributes</a></li>
  112. <li><a class="reference internal" href="#standard-option-actions">Standard option actions</a></li>
  113. <li><a class="reference internal" href="#standard-option-types">Standard option types</a></li>
  114. <li><a class="reference internal" href="#parsing-arguments">Parsing arguments</a></li>
  115. <li><a class="reference internal" href="#querying-and-manipulating-your-option-parser">Querying and manipulating your option parser</a></li>
  116. <li><a class="reference internal" href="#conflicts-between-options">Conflicts between options</a></li>
  117. <li><a class="reference internal" href="#cleanup">Cleanup</a></li>
  118. <li><a class="reference internal" href="#other-methods">Other methods</a></li>
  119. </ul>
  120. </li>
  121. <li><a class="reference internal" href="#option-callbacks">Option Callbacks</a><ul>
  122. <li><a class="reference internal" href="#defining-a-callback-option">Defining a callback option</a></li>
  123. <li><a class="reference internal" href="#how-callbacks-are-called">How callbacks are called</a></li>
  124. <li><a class="reference internal" href="#raising-errors-in-a-callback">Raising errors in a callback</a></li>
  125. <li><a class="reference internal" href="#callback-example-1-trivial-callback">Callback example 1: trivial callback</a></li>
  126. <li><a class="reference internal" href="#callback-example-2-check-option-order">Callback example 2: check option order</a></li>
  127. <li><a class="reference internal" href="#callback-example-3-check-option-order-generalized">Callback example 3: check option order (generalized)</a></li>
  128. <li><a class="reference internal" href="#callback-example-4-check-arbitrary-condition">Callback example 4: check arbitrary condition</a></li>
  129. <li><a class="reference internal" href="#callback-example-5-fixed-arguments">Callback example 5: fixed arguments</a></li>
  130. <li><a class="reference internal" href="#callback-example-6-variable-arguments">Callback example 6: variable arguments</a></li>
  131. </ul>
  132. </li>
  133. <li><a class="reference internal" href="#extending-optparse">Extending <code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a><ul>
  134. <li><a class="reference internal" href="#adding-new-types">Adding new types</a></li>
  135. <li><a class="reference internal" href="#adding-new-actions">Adding new actions</a></li>
  136. </ul>
  137. </li>
  138. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  139. </ul>
  140. </li>
  141. </ul>
  142. </div>
  143. <div>
  144. <h4>Previous topic</h4>
  145. <p class="topless"><a href="nntplib.html"
  146. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">nntplib</span></code> — NNTP protocol client</a></p>
  147. </div>
  148. <div>
  149. <h4>Next topic</h4>
  150. <p class="topless"><a href="ossaudiodev.html"
  151. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ossaudiodev</span></code> — Access to OSS-compatible audio devices</a></p>
  152. </div>
  153. <div role="note" aria-label="source link">
  154. <h3>This Page</h3>
  155. <ul class="this-page-menu">
  156. <li><a href="../bugs.html">Report a Bug</a></li>
  157. <li>
  158. <a href="https://github.com/python/cpython/blob/main/Doc/library/optparse.rst"
  159. rel="nofollow">Show Source
  160. </a>
  161. </li>
  162. </ul>
  163. </div>
  164. </nav>
  165. </div>
  166. </div>
  167. <div class="related" role="navigation" aria-label="related navigation">
  168. <h3>Navigation</h3>
  169. <ul>
  170. <li class="right" style="margin-right: 10px">
  171. <a href="../genindex.html" title="General Index"
  172. accesskey="I">index</a></li>
  173. <li class="right" >
  174. <a href="../py-modindex.html" title="Python Module Index"
  175. >modules</a> |</li>
  176. <li class="right" >
  177. <a href="ossaudiodev.html" title="ossaudiodev — Access to OSS-compatible audio devices"
  178. accesskey="N">next</a> |</li>
  179. <li class="right" >
  180. <a href="nntplib.html" title="nntplib — NNTP protocol client"
  181. accesskey="P">previous</a> |</li>
  182. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  183. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  184. <li class="switchers">
  185. <div class="language_switcher_placeholder"></div>
  186. <div class="version_switcher_placeholder"></div>
  187. </li>
  188. <li>
  189. </li>
  190. <li id="cpython-language-and-version">
  191. <a href="../index.html">3.12.3 Documentation</a> &#187;
  192. </li>
  193. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  194. <li class="nav-item nav-item-2"><a href="superseded.html" accesskey="U">Superseded Modules</a> &#187;</li>
  195. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code> — Parser for command line options</a></li>
  196. <li class="right">
  197. <div class="inline-search" role="search">
  198. <form class="inline-search" action="../search.html" method="get">
  199. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  200. <input type="submit" value="Go" />
  201. </form>
  202. </div>
  203. |
  204. </li>
  205. <li class="right">
  206. <label class="theme-selector-label">
  207. Theme
  208. <select class="theme-selector" oninput="activateTheme(this.value)">
  209. <option value="auto" selected>Auto</option>
  210. <option value="light">Light</option>
  211. <option value="dark">Dark</option>
  212. </select>
  213. </label> |</li>
  214. </ul>
  215. </div>
  216. <div class="document">
  217. <div class="documentwrapper">
  218. <div class="bodywrapper">
  219. <div class="body" role="main">
  220. <section id="module-optparse">
  221. <span id="optparse-parser-for-command-line-options"></span><h1><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> — Parser for command line options<a class="headerlink" href="#module-optparse" title="Link to this heading">¶</a></h1>
  222. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/optparse.py">Lib/optparse.py</a></p>
  223. <div class="deprecated">
  224. <p><span class="versionmodified deprecated">Deprecated since version 3.2: </span>The <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> module is deprecated and will not be developed further;
  225. development will continue with 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>
  226. </div>
  227. <hr class="docutils" />
  228. <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> is a more convenient, flexible, and powerful library for parsing
  229. command-line options than the old <a class="reference internal" href="getopt.html#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. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> uses a
  230. more declarative style of command-line parsing: you create an instance of
  231. <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionParser</span></code></a>, populate it with options, and parse the command
  232. line. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> allows users to specify options in the conventional
  233. GNU/POSIX syntax, and additionally generates usage and help messages for you.</p>
  234. <p>Here’s an example of using <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> in a simple script:</p>
  235. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">OptionParser</span>
  236. <span class="o">...</span>
  237. <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">()</span>
  238. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-f&quot;</span><span class="p">,</span> <span class="s2">&quot;--file&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;filename&quot;</span><span class="p">,</span>
  239. <span class="n">help</span><span class="o">=</span><span class="s2">&quot;write report to FILE&quot;</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s2">&quot;FILE&quot;</span><span class="p">)</span>
  240. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-q&quot;</span><span class="p">,</span> <span class="s2">&quot;--quiet&quot;</span><span class="p">,</span>
  241. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
  242. <span class="n">help</span><span class="o">=</span><span class="s2">&quot;don&#39;t print status messages to stdout&quot;</span><span class="p">)</span>
  243. <span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
  244. </pre></div>
  245. </div>
  246. <p>With these few lines of code, users of your script can now do the “usual thing”
  247. on the command-line, for example:</p>
  248. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="n">yourscript</span><span class="o">&gt;</span> <span class="o">--</span><span class="n">file</span><span class="o">=</span><span class="n">outfile</span> <span class="o">-</span><span class="n">q</span>
  249. </pre></div>
  250. </div>
  251. <p>As it parses the command line, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> sets attributes of the
  252. <code class="docutils literal notranslate"><span class="pre">options</span></code> object returned by <a class="reference internal" href="#optparse.OptionParser.parse_args" title="optparse.OptionParser.parse_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse_args()</span></code></a> based on user-supplied
  253. command-line values. When <a class="reference internal" href="#optparse.OptionParser.parse_args" title="optparse.OptionParser.parse_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse_args()</span></code></a> returns from parsing this command
  254. line, <code class="docutils literal notranslate"><span class="pre">options.filename</span></code> will be <code class="docutils literal notranslate"><span class="pre">&quot;outfile&quot;</span></code> and <code class="docutils literal notranslate"><span class="pre">options.verbose</span></code> will be
  255. <code class="docutils literal notranslate"><span class="pre">False</span></code>. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> supports both long and short options, allows short
  256. options to be merged together, and allows options to be associated with their
  257. arguments in a variety of ways. Thus, the following command lines are all
  258. equivalent to the above example:</p>
  259. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="n">yourscript</span><span class="o">&gt;</span> <span class="o">-</span><span class="n">f</span> <span class="n">outfile</span> <span class="o">--</span><span class="n">quiet</span>
  260. <span class="o">&lt;</span><span class="n">yourscript</span><span class="o">&gt;</span> <span class="o">--</span><span class="n">quiet</span> <span class="o">--</span><span class="n">file</span> <span class="n">outfile</span>
  261. <span class="o">&lt;</span><span class="n">yourscript</span><span class="o">&gt;</span> <span class="o">-</span><span class="n">q</span> <span class="o">-</span><span class="n">foutfile</span>
  262. <span class="o">&lt;</span><span class="n">yourscript</span><span class="o">&gt;</span> <span class="o">-</span><span class="n">qfoutfile</span>
  263. </pre></div>
  264. </div>
  265. <p>Additionally, users can run one of the following</p>
  266. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="n">yourscript</span><span class="o">&gt;</span> <span class="o">-</span><span class="n">h</span>
  267. <span class="o">&lt;</span><span class="n">yourscript</span><span class="o">&gt;</span> <span class="o">--</span><span class="n">help</span>
  268. </pre></div>
  269. </div>
  270. <p>and <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> will print out a brief summary of your script’s options:</p>
  271. <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>Usage: &lt;yourscript&gt; [options]
  272. Options:
  273. -h, --help show this help message and exit
  274. -f FILE, --file=FILE write report to FILE
  275. -q, --quiet don&#39;t print status messages to stdout
  276. </pre></div>
  277. </div>
  278. <p>where the value of <em>yourscript</em> is determined at runtime (normally from
  279. <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code>).</p>
  280. <section id="background">
  281. <span id="optparse-background"></span><h2>Background<a class="headerlink" href="#background" title="Link to this heading">¶</a></h2>
  282. <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> was explicitly designed to encourage the creation of programs
  283. with straightforward, conventional command-line interfaces. To that end, it
  284. supports only the most common command-line syntax and semantics conventionally
  285. used under Unix. If you are unfamiliar with these conventions, read this
  286. section to acquaint yourself with them.</p>
  287. <section id="terminology">
  288. <span id="optparse-terminology"></span><h3>Terminology<a class="headerlink" href="#terminology" title="Link to this heading">¶</a></h3>
  289. <dl>
  290. <dt>argument</dt><dd><p>a string entered on the command-line, and passed by the shell to <code class="docutils literal notranslate"><span class="pre">execl()</span></code>
  291. or <code class="docutils literal notranslate"><span class="pre">execv()</span></code>. In Python, arguments are elements of <code class="docutils literal notranslate"><span class="pre">sys.argv[1:]</span></code>
  292. (<code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> is the name of the program being executed). Unix shells
  293. also use the term “word”.</p>
  294. <p>It is occasionally desirable to substitute an argument list other than
  295. <code class="docutils literal notranslate"><span class="pre">sys.argv[1:]</span></code>, so you should read “argument” as “an element of
  296. <code class="docutils literal notranslate"><span class="pre">sys.argv[1:]</span></code>, or of some other list provided as a substitute for
  297. <code class="docutils literal notranslate"><span class="pre">sys.argv[1:]</span></code>”.</p>
  298. </dd>
  299. <dt>option</dt><dd><p>an argument used to supply extra information to guide or customize the
  300. execution of a program. There are many different syntaxes for options; the
  301. traditional Unix syntax is a hyphen (“-”) followed by a single letter,
  302. e.g. <code class="docutils literal notranslate"><span class="pre">-x</span></code> or <code class="docutils literal notranslate"><span class="pre">-F</span></code>. Also, traditional Unix syntax allows multiple
  303. options to be merged into a single argument, e.g. <code class="docutils literal notranslate"><span class="pre">-x</span> <span class="pre">-F</span></code> is equivalent
  304. to <code class="docutils literal notranslate"><span class="pre">-xF</span></code>. The GNU project introduced <code class="docutils literal notranslate"><span class="pre">--</span></code> followed by a series of
  305. hyphen-separated words, e.g. <code class="docutils literal notranslate"><span class="pre">--file</span></code> or <code class="docutils literal notranslate"><span class="pre">--dry-run</span></code>. These are the
  306. only two option syntaxes provided by <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>.</p>
  307. <p>Some other option syntaxes that the world has seen include:</p>
  308. <ul class="simple">
  309. <li><p>a hyphen followed by a few letters, e.g. <code class="docutils literal notranslate"><span class="pre">-pf</span></code> (this is <em>not</em> the same
  310. as multiple options merged into a single argument)</p></li>
  311. <li><p>a hyphen followed by a whole word, e.g. <code class="docutils literal notranslate"><span class="pre">-file</span></code> (this is technically
  312. equivalent to the previous syntax, but they aren’t usually seen in the same
  313. program)</p></li>
  314. <li><p>a plus sign followed by a single letter, or a few letters, or a word, e.g.
  315. <code class="docutils literal notranslate"><span class="pre">+f</span></code>, <code class="docutils literal notranslate"><span class="pre">+rgb</span></code></p></li>
  316. <li><p>a slash followed by a letter, or a few letters, or a word, e.g. <code class="docutils literal notranslate"><span class="pre">/f</span></code>,
  317. <code class="docutils literal notranslate"><span class="pre">/file</span></code></p></li>
  318. </ul>
  319. <p>These option syntaxes are not supported by <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>, and they never
  320. will be. This is deliberate: the first three are non-standard on any
  321. environment, and the last only makes sense if you’re exclusively targeting
  322. Windows or certain legacy platforms (e.g. VMS, MS-DOS).</p>
  323. </dd>
  324. <dt>option argument</dt><dd><p>an argument that follows an option, is closely associated with that option,
  325. and is consumed from the argument list when that option is. With
  326. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>, option arguments may either be in a separate argument from
  327. their option:</p>
  328. <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>-f foo
  329. --file foo
  330. </pre></div>
  331. </div>
  332. <p>or included in the same argument:</p>
  333. <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>-ffoo
  334. --file=foo
  335. </pre></div>
  336. </div>
  337. <p>Typically, a given option either takes an argument or it doesn’t. Lots of
  338. people want an “optional option arguments” feature, meaning that some options
  339. will take an argument if they see it, and won’t if they don’t. This is
  340. somewhat controversial, because it makes parsing ambiguous: if <code class="docutils literal notranslate"><span class="pre">-a</span></code> takes
  341. an optional argument and <code class="docutils literal notranslate"><span class="pre">-b</span></code> is another option entirely, how do we
  342. interpret <code class="docutils literal notranslate"><span class="pre">-ab</span></code>? Because of this ambiguity, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> does not
  343. support this feature.</p>
  344. </dd>
  345. <dt>positional argument</dt><dd><p>something leftover in the argument list after options have been parsed, i.e.
  346. after options and their arguments have been parsed and removed from the
  347. argument list.</p>
  348. </dd>
  349. <dt>required option</dt><dd><p>an option that must be supplied on the command-line; note that the phrase
  350. “required option” is self-contradictory in English. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> doesn’t
  351. prevent you from implementing required options, but doesn’t give you much
  352. help at it either.</p>
  353. </dd>
  354. </dl>
  355. <p>For example, consider this hypothetical command-line:</p>
  356. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">prog</span> <span class="o">-</span><span class="n">v</span> <span class="o">--</span><span class="n">report</span> <span class="n">report</span><span class="o">.</span><span class="n">txt</span> <span class="n">foo</span> <span class="n">bar</span>
  357. </pre></div>
  358. </div>
  359. <p><code class="docutils literal notranslate"><span class="pre">-v</span></code> and <code class="docutils literal notranslate"><span class="pre">--report</span></code> are both options. Assuming that <code class="docutils literal notranslate"><span class="pre">--report</span></code>
  360. takes one argument, <code class="docutils literal notranslate"><span class="pre">report.txt</span></code> is an option argument. <code class="docutils literal notranslate"><span class="pre">foo</span></code> and
  361. <code class="docutils literal notranslate"><span class="pre">bar</span></code> are positional arguments.</p>
  362. </section>
  363. <section id="what-are-options-for">
  364. <span id="optparse-what-options-for"></span><h3>What are options for?<a class="headerlink" href="#what-are-options-for" title="Link to this heading">¶</a></h3>
  365. <p>Options are used to provide extra information to tune or customize the execution
  366. of a program. In case it wasn’t clear, options are usually <em>optional</em>. A
  367. program should be able to run just fine with no options whatsoever. (Pick a
  368. random program from the Unix or GNU toolsets. Can it run without any options at
  369. all and still make sense? The main exceptions are <code class="docutils literal notranslate"><span class="pre">find</span></code>, <code class="docutils literal notranslate"><span class="pre">tar</span></code>, and
  370. <code class="docutils literal notranslate"><span class="pre">dd</span></code>—all of which are mutant oddballs that have been rightly criticized
  371. for their non-standard syntax and confusing interfaces.)</p>
  372. <p>Lots of people want their programs to have “required options”. Think about it.
  373. If it’s required, then it’s <em>not optional</em>! If there is a piece of information
  374. that your program absolutely requires in order to run successfully, that’s what
  375. positional arguments are for.</p>
  376. <p>As an example of good command-line interface design, consider the humble <code class="docutils literal notranslate"><span class="pre">cp</span></code>
  377. utility, for copying files. It doesn’t make much sense to try to copy files
  378. without supplying a destination and at least one source. Hence, <code class="docutils literal notranslate"><span class="pre">cp</span></code> fails if
  379. you run it with no arguments. However, it has a flexible, useful syntax that
  380. does not require any options at all:</p>
  381. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">cp</span> <span class="n">SOURCE</span> <span class="n">DEST</span>
  382. <span class="n">cp</span> <span class="n">SOURCE</span> <span class="o">...</span> <span class="n">DEST</span><span class="o">-</span><span class="n">DIR</span>
  383. </pre></div>
  384. </div>
  385. <p>You can get pretty far with just that. Most <code class="docutils literal notranslate"><span class="pre">cp</span></code> implementations provide a
  386. bunch of options to tweak exactly how the files are copied: you can preserve
  387. mode and modification time, avoid following symlinks, ask before clobbering
  388. existing files, etc. But none of this distracts from the core mission of
  389. <code class="docutils literal notranslate"><span class="pre">cp</span></code>, which is to copy either one file to another, or several files to another
  390. directory.</p>
  391. </section>
  392. <section id="what-are-positional-arguments-for">
  393. <span id="optparse-what-positional-arguments-for"></span><h3>What are positional arguments for?<a class="headerlink" href="#what-are-positional-arguments-for" title="Link to this heading">¶</a></h3>
  394. <p>Positional arguments are for those pieces of information that your program
  395. absolutely, positively requires to run.</p>
  396. <p>A good user interface should have as few absolute requirements as possible. If
  397. your program requires 17 distinct pieces of information in order to run
  398. successfully, it doesn’t much matter <em>how</em> you get that information from the
  399. user—most people will give up and walk away before they successfully run the
  400. program. This applies whether the user interface is a command-line, a
  401. configuration file, or a GUI: if you make that many demands on your users, most
  402. of them will simply give up.</p>
  403. <p>In short, try to minimize the amount of information that users are absolutely
  404. required to supply—use sensible defaults whenever possible. Of course, you
  405. also want to make your programs reasonably flexible. That’s what options are
  406. for. Again, it doesn’t matter if they are entries in a config file, widgets in
  407. the “Preferences” dialog of a GUI, or command-line options—the more options
  408. you implement, the more flexible your program is, and the more complicated its
  409. implementation becomes. Too much flexibility has drawbacks as well, of course;
  410. too many options can overwhelm users and make your code much harder to maintain.</p>
  411. </section>
  412. </section>
  413. <section id="tutorial">
  414. <span id="optparse-tutorial"></span><h2>Tutorial<a class="headerlink" href="#tutorial" title="Link to this heading">¶</a></h2>
  415. <p>While <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> is quite flexible and powerful, it’s also straightforward
  416. to use in most cases. This section covers the code patterns that are common to
  417. any <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>-based program.</p>
  418. <p>First, you need to import the OptionParser class; then, early in the main
  419. program, create an OptionParser instance:</p>
  420. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">OptionParser</span>
  421. <span class="o">...</span>
  422. <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">()</span>
  423. </pre></div>
  424. </div>
  425. <p>Then you can start defining options. The basic syntax is:</p>
  426. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="n">opt_str</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span>
  427. <span class="n">attr</span><span class="o">=</span><span class="n">value</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
  428. </pre></div>
  429. </div>
  430. <p>Each option has one or more option strings, such as <code class="docutils literal notranslate"><span class="pre">-f</span></code> or <code class="docutils literal notranslate"><span class="pre">--file</span></code>,
  431. and several option attributes that tell <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> what to expect and what
  432. to do when it encounters that option on the command line.</p>
  433. <p>Typically, each option will have one short option string and one long option
  434. string, e.g.:</p>
  435. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-f&quot;</span><span class="p">,</span> <span class="s2">&quot;--file&quot;</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
  436. </pre></div>
  437. </div>
  438. <p>You’re free to define as many short option strings and as many long option
  439. strings as you like (including zero), as long as there is at least one option
  440. string overall.</p>
  441. <p>The option strings passed to <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><code class="xref py py-meth docutils literal notranslate"><span class="pre">OptionParser.add_option()</span></code></a> are effectively
  442. labels for the
  443. option defined by that call. For brevity, we will frequently refer to
  444. <em>encountering an option</em> on the command line; in reality, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>
  445. encounters <em>option strings</em> and looks up options from them.</p>
  446. <p>Once all of your options are defined, instruct <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> to parse your
  447. program’s command line:</p>
  448. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
  449. </pre></div>
  450. </div>
  451. <p>(If you like, you can pass a custom argument list to <a class="reference internal" href="#optparse.OptionParser.parse_args" title="optparse.OptionParser.parse_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse_args()</span></code></a>, but
  452. that’s rarely necessary: by default it uses <code class="docutils literal notranslate"><span class="pre">sys.argv[1:]</span></code>.)</p>
  453. <p><a class="reference internal" href="#optparse.OptionParser.parse_args" title="optparse.OptionParser.parse_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse_args()</span></code></a> returns two values:</p>
  454. <ul class="simple">
  455. <li><p><code class="docutils literal notranslate"><span class="pre">options</span></code>, an object containing values for all of your options—e.g. if
  456. <code class="docutils literal notranslate"><span class="pre">--file</span></code> takes a single string argument, then <code class="docutils literal notranslate"><span class="pre">options.file</span></code> will be the
  457. filename supplied by the user, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the user did not supply that
  458. option</p></li>
  459. <li><p><code class="docutils literal notranslate"><span class="pre">args</span></code>, the list of positional arguments leftover after parsing options</p></li>
  460. </ul>
  461. <p>This tutorial section only covers the four most important option attributes:
  462. <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><code class="xref py py-attr docutils literal notranslate"><span class="pre">action</span></code></a>, <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>
  463. (destination), and <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><code class="xref py py-attr docutils literal notranslate"><span class="pre">help</span></code></a>. Of these, <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><code class="xref py py-attr docutils literal notranslate"><span class="pre">action</span></code></a> is the
  464. most fundamental.</p>
  465. <section id="understanding-option-actions">
  466. <span id="optparse-understanding-option-actions"></span><h3>Understanding option actions<a class="headerlink" href="#understanding-option-actions" title="Link to this heading">¶</a></h3>
  467. <p>Actions tell <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> what to do when it encounters an option on the
  468. command line. There is a fixed set of actions hard-coded into <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>;
  469. adding new actions is an advanced topic covered in section
  470. <a class="reference internal" href="#optparse-extending-optparse"><span class="std std-ref">Extending optparse</span></a>. Most actions tell <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> to store
  471. a value in some variable—for example, take a string from the command line and
  472. store it in an attribute of <code class="docutils literal notranslate"><span class="pre">options</span></code>.</p>
  473. <p>If you don’t specify an option action, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> defaults to <code class="docutils literal notranslate"><span class="pre">store</span></code>.</p>
  474. </section>
  475. <section id="the-store-action">
  476. <span id="optparse-store-action"></span><h3>The store action<a class="headerlink" href="#the-store-action" title="Link to this heading">¶</a></h3>
  477. <p>The most common option action is <code class="docutils literal notranslate"><span class="pre">store</span></code>, which tells <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> to take
  478. the next argument (or the remainder of the current argument), ensure that it is
  479. of the correct type, and store it to your chosen destination.</p>
  480. <p>For example:</p>
  481. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-f&quot;</span><span class="p">,</span> <span class="s2">&quot;--file&quot;</span><span class="p">,</span>
  482. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s2">&quot;string&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;filename&quot;</span><span class="p">)</span>
  483. </pre></div>
  484. </div>
  485. <p>Now let’s make up a fake command line and ask <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> to parse it:</p>
  486. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">args</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;-f&quot;</span><span class="p">,</span> <span class="s2">&quot;foo.txt&quot;</span><span class="p">]</span>
  487. <span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
  488. </pre></div>
  489. </div>
  490. <p>When <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> sees the option string <code class="docutils literal notranslate"><span class="pre">-f</span></code>, it consumes the next
  491. argument, <code class="docutils literal notranslate"><span class="pre">foo.txt</span></code>, and stores it in <code class="docutils literal notranslate"><span class="pre">options.filename</span></code>. So, after this
  492. call to <a class="reference internal" href="#optparse.OptionParser.parse_args" title="optparse.OptionParser.parse_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse_args()</span></code></a>, <code class="docutils literal notranslate"><span class="pre">options.filename</span></code> is <code class="docutils literal notranslate"><span class="pre">&quot;foo.txt&quot;</span></code>.</p>
  493. <p>Some other option types supported by <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> are <code class="docutils literal notranslate"><span class="pre">int</span></code> and <code class="docutils literal notranslate"><span class="pre">float</span></code>.
  494. Here’s an option that expects an integer argument:</p>
  495. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-n&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s2">&quot;int&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;num&quot;</span><span class="p">)</span>
  496. </pre></div>
  497. </div>
  498. <p>Note that this option has no long option string, which is perfectly acceptable.
  499. Also, there’s no explicit action, since the default is <code class="docutils literal notranslate"><span class="pre">store</span></code>.</p>
  500. <p>Let’s parse another fake command-line. This time, we’ll jam the option argument
  501. right up against the option: since <code class="docutils literal notranslate"><span class="pre">-n42</span></code> (one argument) is equivalent to
  502. <code class="docutils literal notranslate"><span class="pre">-n</span> <span class="pre">42</span></code> (two arguments), the code</p>
  503. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s2">&quot;-n42&quot;</span><span class="p">])</span>
  504. <span class="nb">print</span><span class="p">(</span><span class="n">options</span><span class="o">.</span><span class="n">num</span><span class="p">)</span>
  505. </pre></div>
  506. </div>
  507. <p>will print <code class="docutils literal notranslate"><span class="pre">42</span></code>.</p>
  508. <p>If you don’t specify a type, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> assumes <code class="docutils literal notranslate"><span class="pre">string</span></code>. Combined with
  509. the fact that the default action is <code class="docutils literal notranslate"><span class="pre">store</span></code>, that means our first example can
  510. be a lot shorter:</p>
  511. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-f&quot;</span><span class="p">,</span> <span class="s2">&quot;--file&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;filename&quot;</span><span class="p">)</span>
  512. </pre></div>
  513. </div>
  514. <p>If you don’t supply a destination, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> figures out a sensible
  515. default from the option strings: if the first long option string is
  516. <code class="docutils literal notranslate"><span class="pre">--foo-bar</span></code>, then the default destination is <code class="docutils literal notranslate"><span class="pre">foo_bar</span></code>. If there are no
  517. long option strings, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> looks at the first short option string: the
  518. default destination for <code class="docutils literal notranslate"><span class="pre">-f</span></code> is <code class="docutils literal notranslate"><span class="pre">f</span></code>.</p>
  519. <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> also includes the built-in <code class="docutils literal notranslate"><span class="pre">complex</span></code> type. Adding
  520. types is covered in section <a class="reference internal" href="#optparse-extending-optparse"><span class="std std-ref">Extending optparse</span></a>.</p>
  521. </section>
  522. <section id="handling-boolean-flag-options">
  523. <span id="optparse-handling-boolean-options"></span><h3>Handling boolean (flag) options<a class="headerlink" href="#handling-boolean-flag-options" title="Link to this heading">¶</a></h3>
  524. <p>Flag options—set a variable to true or false when a particular option is
  525. seen—are quite common. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> supports them with two separate actions,
  526. <code class="docutils literal notranslate"><span class="pre">store_true</span></code> and <code class="docutils literal notranslate"><span class="pre">store_false</span></code>. For example, you might have a <code class="docutils literal notranslate"><span class="pre">verbose</span></code>
  527. flag that is turned on with <code class="docutils literal notranslate"><span class="pre">-v</span></code> and off with <code class="docutils literal notranslate"><span class="pre">-q</span></code>:</p>
  528. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-v&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">)</span>
  529. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-q&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">)</span>
  530. </pre></div>
  531. </div>
  532. <p>Here we have two different options with the same destination, which is perfectly
  533. OK. (It just means you have to be a bit careful when setting default
  534. values—see below.)</p>
  535. <p>When <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> encounters <code class="docutils literal notranslate"><span class="pre">-v</span></code> on the command line, it sets
  536. <code class="docutils literal notranslate"><span class="pre">options.verbose</span></code> to <code class="docutils literal notranslate"><span class="pre">True</span></code>; when it encounters <code class="docutils literal notranslate"><span class="pre">-q</span></code>,
  537. <code class="docutils literal notranslate"><span class="pre">options.verbose</span></code> is set to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  538. </section>
  539. <section id="other-actions">
  540. <span id="optparse-other-actions"></span><h3>Other actions<a class="headerlink" href="#other-actions" title="Link to this heading">¶</a></h3>
  541. <p>Some other actions supported by <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> are:</p>
  542. <dl class="simple">
  543. <dt><code class="docutils literal notranslate"><span class="pre">&quot;store_const&quot;</span></code></dt><dd><p>store a constant value, pre-set via <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Option.const</span></code></a></p>
  544. </dd>
  545. <dt><code class="docutils literal notranslate"><span class="pre">&quot;append&quot;</span></code></dt><dd><p>append this option’s argument to a list</p>
  546. </dd>
  547. <dt><code class="docutils literal notranslate"><span class="pre">&quot;count&quot;</span></code></dt><dd><p>increment a counter by one</p>
  548. </dd>
  549. <dt><code class="docutils literal notranslate"><span class="pre">&quot;callback&quot;</span></code></dt><dd><p>call a specified function</p>
  550. </dd>
  551. </dl>
  552. <p>These are covered in section <a class="reference internal" href="#optparse-reference-guide"><span class="std std-ref">Reference Guide</span></a>,
  553. and section <a class="reference internal" href="#optparse-option-callbacks"><span class="std std-ref">Option Callbacks</span></a>.</p>
  554. </section>
  555. <section id="default-values">
  556. <span id="optparse-default-values"></span><h3>Default values<a class="headerlink" href="#default-values" title="Link to this heading">¶</a></h3>
  557. <p>All of the above examples involve setting some variable (the “destination”) when
  558. certain command-line options are seen. What happens if those options are never
  559. seen? Since we didn’t supply any defaults, they are all set to <code class="docutils literal notranslate"><span class="pre">None</span></code>. This
  560. is usually fine, but sometimes you want more control. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> lets you
  561. supply a default value for each destination, which is assigned before the
  562. command line is parsed.</p>
  563. <p>First, consider the verbose/quiet example. If we want <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> to set
  564. <code class="docutils literal notranslate"><span class="pre">verbose</span></code> to <code class="docutils literal notranslate"><span class="pre">True</span></code> unless <code class="docutils literal notranslate"><span class="pre">-q</span></code> is seen, then we can do this:</p>
  565. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-v&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  566. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-q&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">)</span>
  567. </pre></div>
  568. </div>
  569. <p>Since default values apply to the <em>destination</em> rather than to any particular
  570. option, and these two options happen to have the same destination, this is
  571. exactly equivalent:</p>
  572. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-v&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">)</span>
  573. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-q&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  574. </pre></div>
  575. </div>
  576. <p>Consider this:</p>
  577. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-v&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
  578. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-q&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  579. </pre></div>
  580. </div>
  581. <p>Again, the default value for <code class="docutils literal notranslate"><span class="pre">verbose</span></code> will be <code class="docutils literal notranslate"><span class="pre">True</span></code>: the last default
  582. value supplied for any particular destination is the one that counts.</p>
  583. <p>A clearer way to specify default values is the <code class="xref py py-meth docutils literal notranslate"><span class="pre">set_defaults()</span></code> method of
  584. OptionParser, which you can call at any time before calling
  585. <a class="reference internal" href="#optparse.OptionParser.parse_args" title="optparse.OptionParser.parse_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse_args()</span></code></a>:</p>
  586. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">set_defaults</span><span class="p">(</span><span class="n">verbose</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  587. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
  588. <span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
  589. </pre></div>
  590. </div>
  591. <p>As before, the last value specified for a given option destination is the one
  592. that counts. For clarity, try to use one method or the other of setting default
  593. values, not both.</p>
  594. </section>
  595. <section id="generating-help">
  596. <span id="optparse-generating-help"></span><h3>Generating help<a class="headerlink" href="#generating-help" title="Link to this heading">¶</a></h3>
  597. <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>’s ability to generate help and usage text automatically is
  598. useful for creating user-friendly command-line interfaces. All you have to do
  599. is supply a <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><code class="xref py py-attr docutils literal notranslate"><span class="pre">help</span></code></a> value for each option, and optionally a short
  600. usage message for your whole program. Here’s an OptionParser populated with
  601. user-friendly (documented) options:</p>
  602. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">usage</span> <span class="o">=</span> <span class="s2">&quot;usage: %prog [options] arg1 arg2&quot;</span>
  603. <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">usage</span><span class="o">=</span><span class="n">usage</span><span class="p">)</span>
  604. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-v&quot;</span><span class="p">,</span> <span class="s2">&quot;--verbose&quot;</span><span class="p">,</span>
  605. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
  606. <span class="n">help</span><span class="o">=</span><span class="s2">&quot;make lots of noise [default]&quot;</span><span class="p">)</span>
  607. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-q&quot;</span><span class="p">,</span> <span class="s2">&quot;--quiet&quot;</span><span class="p">,</span>
  608. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">,</span>
  609. <span class="n">help</span><span class="o">=</span><span class="s2">&quot;be vewwy quiet (I&#39;m hunting wabbits)&quot;</span><span class="p">)</span>
  610. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-f&quot;</span><span class="p">,</span> <span class="s2">&quot;--filename&quot;</span><span class="p">,</span>
  611. <span class="n">metavar</span><span class="o">=</span><span class="s2">&quot;FILE&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;write output to FILE&quot;</span><span class="p">)</span>
  612. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-m&quot;</span><span class="p">,</span> <span class="s2">&quot;--mode&quot;</span><span class="p">,</span>
  613. <span class="n">default</span><span class="o">=</span><span class="s2">&quot;intermediate&quot;</span><span class="p">,</span>
  614. <span class="n">help</span><span class="o">=</span><span class="s2">&quot;interaction mode: novice, intermediate, &quot;</span>
  615. <span class="s2">&quot;or expert [default: </span><span class="si">%d</span><span class="s2">efault]&quot;</span><span class="p">)</span>
  616. </pre></div>
  617. </div>
  618. <p>If <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> encounters either <code class="docutils literal notranslate"><span class="pre">-h</span></code> or <code class="docutils literal notranslate"><span class="pre">--help</span></code> on the
  619. command-line, or if you just call <code class="xref py py-meth docutils literal notranslate"><span class="pre">parser.print_help()</span></code>, it prints the
  620. following to standard output:</p>
  621. <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>Usage: &lt;yourscript&gt; [options] arg1 arg2
  622. Options:
  623. -h, --help show this help message and exit
  624. -v, --verbose make lots of noise [default]
  625. -q, --quiet be vewwy quiet (I&#39;m hunting wabbits)
  626. -f FILE, --filename=FILE
  627. write output to FILE
  628. -m MODE, --mode=MODE interaction mode: novice, intermediate, or
  629. expert [default: intermediate]
  630. </pre></div>
  631. </div>
  632. <p>(If the help output is triggered by a help option, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> exits after
  633. printing the help text.)</p>
  634. <p>There’s a lot going on here to help <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> generate the best possible
  635. help message:</p>
  636. <ul>
  637. <li><p>the script defines its own usage message:</p>
  638. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">usage</span> <span class="o">=</span> <span class="s2">&quot;usage: %prog [options] arg1 arg2&quot;</span>
  639. </pre></div>
  640. </div>
  641. <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> expands <code class="docutils literal notranslate"><span class="pre">%prog</span></code> in the usage string to the name of the
  642. current program, i.e. <code class="docutils literal notranslate"><span class="pre">os.path.basename(sys.argv[0])</span></code>. The expanded string
  643. is then printed before the detailed option help.</p>
  644. <p>If you don’t supply a usage string, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> uses a bland but sensible
  645. default: <code class="docutils literal notranslate"><span class="pre">&quot;Usage:</span> <span class="pre">%prog</span> <span class="pre">[options]&quot;</span></code>, which is fine if your script doesn’t
  646. take any positional arguments.</p>
  647. </li>
  648. <li><p>every option defines a help string, and doesn’t worry about
  649. line-wrapping—<a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> takes care of wrapping lines and making
  650. the help output look good.</p></li>
  651. <li><p>options that take a value indicate this fact in their automatically generated
  652. help message, e.g. for the “mode” option:</p>
  653. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">-</span><span class="n">m</span> <span class="n">MODE</span><span class="p">,</span> <span class="o">--</span><span class="n">mode</span><span class="o">=</span><span class="n">MODE</span>
  654. </pre></div>
  655. </div>
  656. <p>Here, “MODE” is called the meta-variable: it stands for the argument that the
  657. user is expected to supply to <code class="docutils literal notranslate"><span class="pre">-m</span></code>/<code class="docutils literal notranslate"><span class="pre">--mode</span></code>. By default,
  658. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> converts the destination variable name to uppercase and uses
  659. that for the meta-variable. Sometimes, that’s not what you want—for
  660. example, the <code class="docutils literal notranslate"><span class="pre">--filename</span></code> option explicitly sets <code class="docutils literal notranslate"><span class="pre">metavar=&quot;FILE&quot;</span></code>,
  661. resulting in this automatically generated option description:</p>
  662. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">-</span><span class="n">f</span> <span class="n">FILE</span><span class="p">,</span> <span class="o">--</span><span class="n">filename</span><span class="o">=</span><span class="n">FILE</span>
  663. </pre></div>
  664. </div>
  665. <p>This is important for more than just saving space, though: the manually
  666. written help text uses the meta-variable <code class="docutils literal notranslate"><span class="pre">FILE</span></code> to clue the user in that
  667. there’s a connection between the semi-formal syntax <code class="docutils literal notranslate"><span class="pre">-f</span> <span class="pre">FILE</span></code> and the informal
  668. semantic description “write output to FILE”. This is a simple but effective
  669. way to make your help text a lot clearer and more useful for end users.</p>
  670. </li>
  671. <li><p>options that have a default value can include <code class="docutils literal notranslate"><span class="pre">%default</span></code> in the help
  672. string—<a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> will replace it with <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> of the option’s
  673. default value. If an option has no default value (or the default value is
  674. <code class="docutils literal notranslate"><span class="pre">None</span></code>), <code class="docutils literal notranslate"><span class="pre">%default</span></code> expands to <code class="docutils literal notranslate"><span class="pre">none</span></code>.</p></li>
  675. </ul>
  676. <section id="grouping-options">
  677. <h4>Grouping Options<a class="headerlink" href="#grouping-options" title="Link to this heading">¶</a></h4>
  678. <p>When dealing with many options, it is convenient to group these options for
  679. better help output. An <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionParser</span></code></a> can contain several option groups,
  680. each of which can contain several options.</p>
  681. <p>An option group is obtained using the class <a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionGroup</span></code></a>:</p>
  682. <dl class="py class">
  683. <dt class="sig sig-object py" id="optparse.OptionGroup">
  684. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">optparse.</span></span><span class="sig-name descname"><span class="pre">OptionGroup</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">parser</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">title</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">description</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionGroup" title="Link to this definition">¶</a></dt>
  685. <dd><p>where</p>
  686. <ul class="simple">
  687. <li><p>parser is the <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionParser</span></code></a> instance the group will be inserted in
  688. to</p></li>
  689. <li><p>title is the group title</p></li>
  690. <li><p>description, optional, is a long description of the group</p></li>
  691. </ul>
  692. </dd></dl>
  693. <p><a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionGroup</span></code></a> inherits from <code class="xref py py-class docutils literal notranslate"><span class="pre">OptionContainer</span></code> (like
  694. <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionParser</span></code></a>) and so the <code class="xref py py-meth docutils literal notranslate"><span class="pre">add_option()</span></code> method can be used to add
  695. an option to the group.</p>
  696. <p>Once all the options are declared, using the <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionParser</span></code></a> method
  697. <code class="xref py py-meth docutils literal notranslate"><span class="pre">add_option_group()</span></code> the group is added to the previously defined parser.</p>
  698. <p>Continuing with the parser defined in the previous section, adding an
  699. <a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionGroup</span></code></a> to a parser is easy:</p>
  700. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">group</span> <span class="o">=</span> <span class="n">OptionGroup</span><span class="p">(</span><span class="n">parser</span><span class="p">,</span> <span class="s2">&quot;Dangerous Options&quot;</span><span class="p">,</span>
  701. <span class="s2">&quot;Caution: use these options at your own risk. &quot;</span>
  702. <span class="s2">&quot;It is believed that some of them bite.&quot;</span><span class="p">)</span>
  703. <span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-g&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Group option.&quot;</span><span class="p">)</span>
  704. <span class="n">parser</span><span class="o">.</span><span class="n">add_option_group</span><span class="p">(</span><span class="n">group</span><span class="p">)</span>
  705. </pre></div>
  706. </div>
  707. <p>This would result in the following help output:</p>
  708. <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>Usage: &lt;yourscript&gt; [options] arg1 arg2
  709. Options:
  710. -h, --help show this help message and exit
  711. -v, --verbose make lots of noise [default]
  712. -q, --quiet be vewwy quiet (I&#39;m hunting wabbits)
  713. -f FILE, --filename=FILE
  714. write output to FILE
  715. -m MODE, --mode=MODE interaction mode: novice, intermediate, or
  716. expert [default: intermediate]
  717. Dangerous Options:
  718. Caution: use these options at your own risk. It is believed that some
  719. of them bite.
  720. -g Group option.
  721. </pre></div>
  722. </div>
  723. <p>A bit more complete example might involve using more than one group: still
  724. extending the previous example:</p>
  725. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">group</span> <span class="o">=</span> <span class="n">OptionGroup</span><span class="p">(</span><span class="n">parser</span><span class="p">,</span> <span class="s2">&quot;Dangerous Options&quot;</span><span class="p">,</span>
  726. <span class="s2">&quot;Caution: use these options at your own risk. &quot;</span>
  727. <span class="s2">&quot;It is believed that some of them bite.&quot;</span><span class="p">)</span>
  728. <span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-g&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Group option.&quot;</span><span class="p">)</span>
  729. <span class="n">parser</span><span class="o">.</span><span class="n">add_option_group</span><span class="p">(</span><span class="n">group</span><span class="p">)</span>
  730. <span class="n">group</span> <span class="o">=</span> <span class="n">OptionGroup</span><span class="p">(</span><span class="n">parser</span><span class="p">,</span> <span class="s2">&quot;Debug Options&quot;</span><span class="p">)</span>
  731. <span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-d&quot;</span><span class="p">,</span> <span class="s2">&quot;--debug&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span>
  732. <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Print debug information&quot;</span><span class="p">)</span>
  733. <span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-s&quot;</span><span class="p">,</span> <span class="s2">&quot;--sql&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span>
  734. <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Print all SQL statements executed&quot;</span><span class="p">)</span>
  735. <span class="n">group</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-e&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Print every action done&quot;</span><span class="p">)</span>
  736. <span class="n">parser</span><span class="o">.</span><span class="n">add_option_group</span><span class="p">(</span><span class="n">group</span><span class="p">)</span>
  737. </pre></div>
  738. </div>
  739. <p>that results in the following output:</p>
  740. <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>Usage: &lt;yourscript&gt; [options] arg1 arg2
  741. Options:
  742. -h, --help show this help message and exit
  743. -v, --verbose make lots of noise [default]
  744. -q, --quiet be vewwy quiet (I&#39;m hunting wabbits)
  745. -f FILE, --filename=FILE
  746. write output to FILE
  747. -m MODE, --mode=MODE interaction mode: novice, intermediate, or expert
  748. [default: intermediate]
  749. Dangerous Options:
  750. Caution: use these options at your own risk. It is believed that some
  751. of them bite.
  752. -g Group option.
  753. Debug Options:
  754. -d, --debug Print debug information
  755. -s, --sql Print all SQL statements executed
  756. -e Print every action done
  757. </pre></div>
  758. </div>
  759. <p>Another interesting method, in particular when working programmatically with
  760. option groups is:</p>
  761. <dl class="py method">
  762. <dt class="sig sig-object py" id="optparse.OptionParser.get_option_group">
  763. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">get_option_group</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">opt_str</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.get_option_group" title="Link to this definition">¶</a></dt>
  764. <dd><p>Return the <a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionGroup</span></code></a> to which the short or long option
  765. string <em>opt_str</em> (e.g. <code class="docutils literal notranslate"><span class="pre">'-o'</span></code> or <code class="docutils literal notranslate"><span class="pre">'--option'</span></code>) belongs. If
  766. there’s no such <a class="reference internal" href="#optparse.OptionGroup" title="optparse.OptionGroup"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionGroup</span></code></a>, return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  767. </dd></dl>
  768. </section>
  769. </section>
  770. <section id="printing-a-version-string">
  771. <span id="optparse-printing-version-string"></span><h3>Printing a version string<a class="headerlink" href="#printing-a-version-string" title="Link to this heading">¶</a></h3>
  772. <p>Similar to the brief usage string, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> can also print a version
  773. string for your program. You have to supply the string as the <code class="docutils literal notranslate"><span class="pre">version</span></code>
  774. argument to OptionParser:</p>
  775. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">usage</span><span class="o">=</span><span class="s2">&quot;%prog [-f] [-q]&quot;</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="s2">&quot;%prog 1.0&quot;</span><span class="p">)</span>
  776. </pre></div>
  777. </div>
  778. <p><code class="docutils literal notranslate"><span class="pre">%prog</span></code> is expanded just like it is in <code class="docutils literal notranslate"><span class="pre">usage</span></code>. Apart from that,
  779. <code class="docutils literal notranslate"><span class="pre">version</span></code> can contain anything you like. When you supply it, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>
  780. automatically adds a <code class="docutils literal notranslate"><span class="pre">--version</span></code> option to your parser. If it encounters
  781. this option on the command line, it expands your <code class="docutils literal notranslate"><span class="pre">version</span></code> string (by
  782. replacing <code class="docutils literal notranslate"><span class="pre">%prog</span></code>), prints it to stdout, and exits.</p>
  783. <p>For example, if your script is called <code class="docutils literal notranslate"><span class="pre">/usr/bin/foo</span></code>:</p>
  784. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>/usr/bin/foo<span class="w"> </span>--version
  785. <span class="go">foo 1.0</span>
  786. </pre></div>
  787. </div>
  788. <p>The following two methods can be used to print and get the <code class="docutils literal notranslate"><span class="pre">version</span></code> string:</p>
  789. <dl class="py method">
  790. <dt class="sig sig-object py" id="optparse.OptionParser.print_version">
  791. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">print_version</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.print_version" title="Link to this definition">¶</a></dt>
  792. <dd><p>Print the version message for the current program (<code class="docutils literal notranslate"><span class="pre">self.version</span></code>) to
  793. <em>file</em> (default stdout). As with <a class="reference internal" href="#optparse.OptionParser.print_usage" title="optparse.OptionParser.print_usage"><code class="xref py py-meth docutils literal notranslate"><span class="pre">print_usage()</span></code></a>, any occurrence
  794. of <code class="docutils literal notranslate"><span class="pre">%prog</span></code> in <code class="docutils literal notranslate"><span class="pre">self.version</span></code> is replaced with the name of the current
  795. program. Does nothing if <code class="docutils literal notranslate"><span class="pre">self.version</span></code> is empty or undefined.</p>
  796. </dd></dl>
  797. <dl class="py method">
  798. <dt class="sig sig-object py" id="optparse.OptionParser.get_version">
  799. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">get_version</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.get_version" title="Link to this definition">¶</a></dt>
  800. <dd><p>Same as <a class="reference internal" href="#optparse.OptionParser.print_version" title="optparse.OptionParser.print_version"><code class="xref py py-meth docutils literal notranslate"><span class="pre">print_version()</span></code></a> but returns the version string instead of
  801. printing it.</p>
  802. </dd></dl>
  803. </section>
  804. <section id="how-optparse-handles-errors">
  805. <span id="optparse-how-optparse-handles-errors"></span><h3>How <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> handles errors<a class="headerlink" href="#how-optparse-handles-errors" title="Link to this heading">¶</a></h3>
  806. <p>There are two broad classes of errors that <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> has to worry about:
  807. programmer errors and user errors. Programmer errors are usually erroneous
  808. calls to <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><code class="xref py py-func docutils literal notranslate"><span class="pre">OptionParser.add_option()</span></code></a>, e.g. invalid option strings, unknown
  809. option attributes, missing option attributes, etc. These are dealt with in the
  810. usual way: raise an exception (either <a class="reference internal" href="#optparse.OptionError" title="optparse.OptionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">optparse.OptionError</span></code></a> or
  811. <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>) and let the program crash.</p>
  812. <p>Handling user errors is much more important, since they are guaranteed to happen
  813. no matter how stable your code is. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> can automatically detect
  814. some user errors, such as bad option arguments (passing <code class="docutils literal notranslate"><span class="pre">-n</span> <span class="pre">4x</span></code> where
  815. <code class="docutils literal notranslate"><span class="pre">-n</span></code> takes an integer argument), missing arguments (<code class="docutils literal notranslate"><span class="pre">-n</span></code> at the end
  816. of the command line, where <code class="docutils literal notranslate"><span class="pre">-n</span></code> takes an argument of any type). Also,
  817. you can call <code class="xref py py-func docutils literal notranslate"><span class="pre">OptionParser.error()</span></code> to signal an application-defined error
  818. condition:</p>
  819. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
  820. <span class="o">...</span>
  821. <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">a</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">b</span><span class="p">:</span>
  822. <span class="n">parser</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">&quot;options -a and -b are mutually exclusive&quot;</span><span class="p">)</span>
  823. </pre></div>
  824. </div>
  825. <p>In either case, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> handles the error the same way: it prints the
  826. program’s usage message and an error message to standard error and exits with
  827. error status 2.</p>
  828. <p>Consider the first example above, where the user passes <code class="docutils literal notranslate"><span class="pre">4x</span></code> to an option
  829. that takes an integer:</p>
  830. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>/usr/bin/foo<span class="w"> </span>-n<span class="w"> </span>4x
  831. <span class="go">Usage: foo [options]</span>
  832. <span class="go">foo: error: option -n: invalid integer value: &#39;4x&#39;</span>
  833. </pre></div>
  834. </div>
  835. <p>Or, where the user fails to pass a value at all:</p>
  836. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>/usr/bin/foo<span class="w"> </span>-n
  837. <span class="go">Usage: foo [options]</span>
  838. <span class="go">foo: error: -n option requires an argument</span>
  839. </pre></div>
  840. </div>
  841. <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>-generated error messages take care always to mention the
  842. option involved in the error; be sure to do the same when calling
  843. <code class="xref py py-func docutils literal notranslate"><span class="pre">OptionParser.error()</span></code> from your application code.</p>
  844. <p>If <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>’s default error-handling behaviour does not suit your needs,
  845. you’ll need to subclass OptionParser and override its <code class="xref py py-meth docutils literal notranslate"><span class="pre">exit()</span></code>
  846. and/or <code class="xref py py-meth docutils literal notranslate"><span class="pre">error()</span></code> methods.</p>
  847. </section>
  848. <section id="putting-it-all-together">
  849. <span id="optparse-putting-it-all-together"></span><h3>Putting it all together<a class="headerlink" href="#putting-it-all-together" title="Link to this heading">¶</a></h3>
  850. <p>Here’s what <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>-based scripts usually look like:</p>
  851. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">OptionParser</span>
  852. <span class="o">...</span>
  853. <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
  854. <span class="n">usage</span> <span class="o">=</span> <span class="s2">&quot;usage: %prog [options] arg&quot;</span>
  855. <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">usage</span><span class="p">)</span>
  856. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-f&quot;</span><span class="p">,</span> <span class="s2">&quot;--file&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;filename&quot;</span><span class="p">,</span>
  857. <span class="n">help</span><span class="o">=</span><span class="s2">&quot;read data from FILENAME&quot;</span><span class="p">)</span>
  858. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-v&quot;</span><span class="p">,</span> <span class="s2">&quot;--verbose&quot;</span><span class="p">,</span>
  859. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">)</span>
  860. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-q&quot;</span><span class="p">,</span> <span class="s2">&quot;--quiet&quot;</span><span class="p">,</span>
  861. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">)</span>
  862. <span class="o">...</span>
  863. <span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
  864. <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
  865. <span class="n">parser</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">&quot;incorrect number of arguments&quot;</span><span class="p">)</span>
  866. <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">verbose</span><span class="p">:</span>
  867. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;reading </span><span class="si">%s</span><span class="s2">...&quot;</span> <span class="o">%</span> <span class="n">options</span><span class="o">.</span><span class="n">filename</span><span class="p">)</span>
  868. <span class="o">...</span>
  869. <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>
  870. <span class="n">main</span><span class="p">()</span>
  871. </pre></div>
  872. </div>
  873. </section>
  874. </section>
  875. <section id="reference-guide">
  876. <span id="optparse-reference-guide"></span><h2>Reference Guide<a class="headerlink" href="#reference-guide" title="Link to this heading">¶</a></h2>
  877. <section id="creating-the-parser">
  878. <span id="optparse-creating-parser"></span><h3>Creating the parser<a class="headerlink" href="#creating-the-parser" title="Link to this heading">¶</a></h3>
  879. <p>The first step in using <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> is to create an OptionParser instance.</p>
  880. <dl class="py class">
  881. <dt class="sig sig-object py" id="optparse.OptionParser">
  882. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">optparse.</span></span><span class="sig-name descname"><span class="pre">OptionParser</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser" title="Link to this definition">¶</a></dt>
  883. <dd><p>The OptionParser constructor has no required arguments, but a number of
  884. optional keyword arguments. You should always pass them as keyword
  885. arguments, i.e. do not rely on the order in which the arguments are declared.</p>
  886. <dl class="simple">
  887. <dt><code class="docutils literal notranslate"><span class="pre">usage</span></code> (default: <code class="docutils literal notranslate"><span class="pre">&quot;%prog</span> <span class="pre">[options]&quot;</span></code>)</dt><dd><p>The usage summary to print when your program is run incorrectly or with a
  888. help option. When <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> prints the usage string, it expands
  889. <code class="docutils literal notranslate"><span class="pre">%prog</span></code> to <code class="docutils literal notranslate"><span class="pre">os.path.basename(sys.argv[0])</span></code> (or to <code class="docutils literal notranslate"><span class="pre">prog</span></code> if you
  890. passed that keyword argument). To suppress a usage message, pass the
  891. special value <code class="xref py py-const docutils literal notranslate"><span class="pre">optparse.SUPPRESS_USAGE</span></code>.</p>
  892. </dd>
  893. <dt><code class="docutils literal notranslate"><span class="pre">option_list</span></code> (default: <code class="docutils literal notranslate"><span class="pre">[]</span></code>)</dt><dd><p>A list of Option objects to populate the parser with. The options in
  894. <code class="docutils literal notranslate"><span class="pre">option_list</span></code> are added after any options in <code class="docutils literal notranslate"><span class="pre">standard_option_list</span></code> (a
  895. class attribute that may be set by OptionParser subclasses), but before
  896. any version or help options. Deprecated; use <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_option()</span></code></a> after
  897. creating the parser instead.</p>
  898. </dd>
  899. <dt><code class="docutils literal notranslate"><span class="pre">option_class</span></code> (default: optparse.Option)</dt><dd><p>Class to use when adding options to the parser in <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_option()</span></code></a>.</p>
  900. </dd>
  901. <dt><code class="docutils literal notranslate"><span class="pre">version</span></code> (default: <code class="docutils literal notranslate"><span class="pre">None</span></code>)</dt><dd><p>A version string to print when the user supplies a version option. If you
  902. supply a true value for <code class="docutils literal notranslate"><span class="pre">version</span></code>, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> automatically adds a
  903. version option with the single option string <code class="docutils literal notranslate"><span class="pre">--version</span></code>. The
  904. substring <code class="docutils literal notranslate"><span class="pre">%prog</span></code> is expanded the same as for <code class="docutils literal notranslate"><span class="pre">usage</span></code>.</p>
  905. </dd>
  906. <dt><code class="docutils literal notranslate"><span class="pre">conflict_handler</span></code> (default: <code class="docutils literal notranslate"><span class="pre">&quot;error&quot;</span></code>)</dt><dd><p>Specifies what to do when options with conflicting option strings are
  907. added to the parser; see section
  908. <a class="reference internal" href="#optparse-conflicts-between-options"><span class="std std-ref">Conflicts between options</span></a>.</p>
  909. </dd>
  910. <dt><code class="docutils literal notranslate"><span class="pre">description</span></code> (default: <code class="docutils literal notranslate"><span class="pre">None</span></code>)</dt><dd><p>A paragraph of text giving a brief overview of your program.
  911. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> reformats this paragraph to fit the current terminal width
  912. and prints it when the user requests help (after <code class="docutils literal notranslate"><span class="pre">usage</span></code>, but before the
  913. list of options).</p>
  914. </dd>
  915. <dt><code class="docutils literal notranslate"><span class="pre">formatter</span></code> (default: a new <code class="xref py py-class docutils literal notranslate"><span class="pre">IndentedHelpFormatter</span></code>)</dt><dd><p>An instance of optparse.HelpFormatter that will be used for printing help
  916. text. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> provides two concrete classes for this purpose:
  917. IndentedHelpFormatter and TitledHelpFormatter.</p>
  918. </dd>
  919. <dt><code class="docutils literal notranslate"><span class="pre">add_help_option</span></code> (default: <code class="docutils literal notranslate"><span class="pre">True</span></code>)</dt><dd><p>If true, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> will add a help option (with option strings <code class="docutils literal notranslate"><span class="pre">-h</span></code>
  920. and <code class="docutils literal notranslate"><span class="pre">--help</span></code>) to the parser.</p>
  921. </dd>
  922. <dt><code class="docutils literal notranslate"><span class="pre">prog</span></code></dt><dd><p>The string to use when expanding <code class="docutils literal notranslate"><span class="pre">%prog</span></code> in <code class="docutils literal notranslate"><span class="pre">usage</span></code> and <code class="docutils literal notranslate"><span class="pre">version</span></code>
  923. instead of <code class="docutils literal notranslate"><span class="pre">os.path.basename(sys.argv[0])</span></code>.</p>
  924. </dd>
  925. <dt><code class="docutils literal notranslate"><span class="pre">epilog</span></code> (default: <code class="docutils literal notranslate"><span class="pre">None</span></code>)</dt><dd><p>A paragraph of help text to print after the option help.</p>
  926. </dd>
  927. </dl>
  928. </dd></dl>
  929. </section>
  930. <section id="populating-the-parser">
  931. <span id="optparse-populating-parser"></span><h3>Populating the parser<a class="headerlink" href="#populating-the-parser" title="Link to this heading">¶</a></h3>
  932. <p>There are several ways to populate the parser with options. The preferred way
  933. is by using <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><code class="xref py py-meth docutils literal notranslate"><span class="pre">OptionParser.add_option()</span></code></a>, as shown in section
  934. <a class="reference internal" href="#optparse-tutorial"><span class="std std-ref">Tutorial</span></a>. <code class="xref py py-meth docutils literal notranslate"><span class="pre">add_option()</span></code> can be called in one of two ways:</p>
  935. <ul class="simple">
  936. <li><p>pass it an Option instance (as returned by <code class="xref py py-func docutils literal notranslate"><span class="pre">make_option()</span></code>)</p></li>
  937. <li><p>pass it any combination of positional and keyword arguments that are
  938. acceptable to <code class="xref py py-func docutils literal notranslate"><span class="pre">make_option()</span></code> (i.e., to the Option constructor), and it
  939. will create the Option instance for you</p></li>
  940. </ul>
  941. <p>The other alternative is to pass a list of pre-constructed Option instances to
  942. the OptionParser constructor, as in:</p>
  943. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">option_list</span> <span class="o">=</span> <span class="p">[</span>
  944. <span class="n">make_option</span><span class="p">(</span><span class="s2">&quot;-f&quot;</span><span class="p">,</span> <span class="s2">&quot;--filename&quot;</span><span class="p">,</span>
  945. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s2">&quot;string&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;filename&quot;</span><span class="p">),</span>
  946. <span class="n">make_option</span><span class="p">(</span><span class="s2">&quot;-q&quot;</span><span class="p">,</span> <span class="s2">&quot;--quiet&quot;</span><span class="p">,</span>
  947. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">),</span>
  948. <span class="p">]</span>
  949. <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">option_list</span><span class="o">=</span><span class="n">option_list</span><span class="p">)</span>
  950. </pre></div>
  951. </div>
  952. <p>(<code class="xref py py-func docutils literal notranslate"><span class="pre">make_option()</span></code> is a factory function for creating Option instances;
  953. currently it is an alias for the Option constructor. A future version of
  954. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> may split Option into several classes, and <code class="xref py py-func docutils literal notranslate"><span class="pre">make_option()</span></code>
  955. will pick the right class to instantiate. Do not instantiate Option directly.)</p>
  956. </section>
  957. <section id="defining-options">
  958. <span id="optparse-defining-options"></span><h3>Defining options<a class="headerlink" href="#defining-options" title="Link to this heading">¶</a></h3>
  959. <p>Each Option instance represents a set of synonymous command-line option strings,
  960. e.g. <code class="docutils literal notranslate"><span class="pre">-f</span></code> and <code class="docutils literal notranslate"><span class="pre">--file</span></code>. You can specify any number of short or
  961. long option strings, but you must specify at least one overall option string.</p>
  962. <p>The canonical way to create an <a class="reference internal" href="#optparse.Option" title="optparse.Option"><code class="xref py py-class docutils literal notranslate"><span class="pre">Option</span></code></a> instance is with the
  963. <code class="xref py py-meth docutils literal notranslate"><span class="pre">add_option()</span></code> method of <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionParser</span></code></a>.</p>
  964. <dl class="py method">
  965. <dt class="sig sig-object py" id="optparse.OptionParser.add_option">
  966. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">add_option</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">option</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.add_option" title="Link to this definition">¶</a></dt>
  967. <dt class="sig sig-object py">
  968. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">add_option</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">*opt_str</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attr=value</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span></dt>
  969. <dd><p>To define an option with only a short option string:</p>
  970. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-f&quot;</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="n">value</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
  971. </pre></div>
  972. </div>
  973. <p>And to define an option with only a long option string:</p>
  974. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--foo&quot;</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="n">value</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
  975. </pre></div>
  976. </div>
  977. <p>The keyword arguments define attributes of the new Option object. The most
  978. important option attribute is <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><code class="xref py py-attr docutils literal notranslate"><span class="pre">action</span></code></a>, and it largely
  979. determines which other attributes are relevant or required. If you pass
  980. irrelevant option attributes, or fail to pass required ones, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>
  981. raises an <a class="reference internal" href="#optparse.OptionError" title="optparse.OptionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OptionError</span></code></a> exception explaining your mistake.</p>
  982. <p>An option’s <em>action</em> determines what <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> does when it encounters
  983. this option on the command-line. The standard option actions hard-coded into
  984. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> are:</p>
  985. <dl class="simple">
  986. <dt><code class="docutils literal notranslate"><span class="pre">&quot;store&quot;</span></code></dt><dd><p>store this option’s argument (default)</p>
  987. </dd>
  988. <dt><code class="docutils literal notranslate"><span class="pre">&quot;store_const&quot;</span></code></dt><dd><p>store a constant value, pre-set via <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Option.const</span></code></a></p>
  989. </dd>
  990. <dt><code class="docutils literal notranslate"><span class="pre">&quot;store_true&quot;</span></code></dt><dd><p>store <code class="docutils literal notranslate"><span class="pre">True</span></code></p>
  991. </dd>
  992. <dt><code class="docutils literal notranslate"><span class="pre">&quot;store_false&quot;</span></code></dt><dd><p>store <code class="docutils literal notranslate"><span class="pre">False</span></code></p>
  993. </dd>
  994. <dt><code class="docutils literal notranslate"><span class="pre">&quot;append&quot;</span></code></dt><dd><p>append this option’s argument to a list</p>
  995. </dd>
  996. <dt><code class="docutils literal notranslate"><span class="pre">&quot;append_const&quot;</span></code></dt><dd><p>append a constant value to a list, pre-set via <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Option.const</span></code></a></p>
  997. </dd>
  998. <dt><code class="docutils literal notranslate"><span class="pre">&quot;count&quot;</span></code></dt><dd><p>increment a counter by one</p>
  999. </dd>
  1000. <dt><code class="docutils literal notranslate"><span class="pre">&quot;callback&quot;</span></code></dt><dd><p>call a specified function</p>
  1001. </dd>
  1002. <dt><code class="docutils literal notranslate"><span class="pre">&quot;help&quot;</span></code></dt><dd><p>print a usage message including all options and the documentation for them</p>
  1003. </dd>
  1004. </dl>
  1005. <p>(If you don’t supply an action, the default is <code class="docutils literal notranslate"><span class="pre">&quot;store&quot;</span></code>. For this action,
  1006. you may also supply <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a> and <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a> option
  1007. attributes; see <a class="reference internal" href="#optparse-standard-option-actions"><span class="std std-ref">Standard option actions</span></a>.)</p>
  1008. </dd></dl>
  1009. <p>As you can see, most actions involve storing or updating a value somewhere.
  1010. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> always creates a special object for this, conventionally called
  1011. <code class="docutils literal notranslate"><span class="pre">options</span></code>, which is an instance of <a class="reference internal" href="#optparse.Values" title="optparse.Values"><code class="xref py py-class docutils literal notranslate"><span class="pre">optparse.Values</span></code></a>.</p>
  1012. <dl class="py class">
  1013. <dt class="sig sig-object py" id="optparse.Values">
  1014. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">optparse.</span></span><span class="sig-name descname"><span class="pre">Values</span></span><a class="headerlink" href="#optparse.Values" title="Link to this definition">¶</a></dt>
  1015. <dd><p>An object holding parsed argument names and values as attributes.
  1016. Normally created by calling when calling <a class="reference internal" href="#optparse.OptionParser.parse_args" title="optparse.OptionParser.parse_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">OptionParser.parse_args()</span></code></a>,
  1017. and can be overridden by a custom subclass passed to the <em>values</em> argument of
  1018. <a class="reference internal" href="#optparse.OptionParser.parse_args" title="optparse.OptionParser.parse_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">OptionParser.parse_args()</span></code></a> (as described in <a class="reference internal" href="#optparse-parsing-arguments"><span class="std std-ref">Parsing arguments</span></a>).</p>
  1019. </dd></dl>
  1020. <p>Option
  1021. arguments (and various other values) are stored as attributes of this object,
  1022. according to the <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a> (destination) option attribute.</p>
  1023. <p>For example, when you call</p>
  1024. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
  1025. </pre></div>
  1026. </div>
  1027. <p>one of the first things <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> does is create the <code class="docutils literal notranslate"><span class="pre">options</span></code> object:</p>
  1028. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">options</span> <span class="o">=</span> <span class="n">Values</span><span class="p">()</span>
  1029. </pre></div>
  1030. </div>
  1031. <p>If one of the options in this parser is defined with</p>
  1032. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-f&quot;</span><span class="p">,</span> <span class="s2">&quot;--file&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s2">&quot;string&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;filename&quot;</span><span class="p">)</span>
  1033. </pre></div>
  1034. </div>
  1035. <p>and the command-line being parsed includes any of the following:</p>
  1036. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">-</span><span class="n">ffoo</span>
  1037. <span class="o">-</span><span class="n">f</span> <span class="n">foo</span>
  1038. <span class="o">--</span><span class="n">file</span><span class="o">=</span><span class="n">foo</span>
  1039. <span class="o">--</span><span class="n">file</span> <span class="n">foo</span>
  1040. </pre></div>
  1041. </div>
  1042. <p>then <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>, on seeing this option, will do the equivalent of</p>
  1043. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">options</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="s2">&quot;foo&quot;</span>
  1044. </pre></div>
  1045. </div>
  1046. <p>The <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a> and <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a> option attributes are almost
  1047. as important as <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><code class="xref py py-attr docutils literal notranslate"><span class="pre">action</span></code></a>, but <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><code class="xref py py-attr docutils literal notranslate"><span class="pre">action</span></code></a> is the only
  1048. one that makes sense for <em>all</em> options.</p>
  1049. </section>
  1050. <section id="option-attributes">
  1051. <span id="optparse-option-attributes"></span><h3>Option attributes<a class="headerlink" href="#option-attributes" title="Link to this heading">¶</a></h3>
  1052. <dl class="py class">
  1053. <dt class="sig sig-object py" id="optparse.Option">
  1054. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">optparse.</span></span><span class="sig-name descname"><span class="pre">Option</span></span><a class="headerlink" href="#optparse.Option" title="Link to this definition">¶</a></dt>
  1055. <dd><p>A single command line argument,
  1056. with various attributes passed by keyword to the constructor.
  1057. Normally created with <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><code class="xref py py-meth docutils literal notranslate"><span class="pre">OptionParser.add_option()</span></code></a> rather than directly,
  1058. and can be overridden by a custom class via the <em>option_class</em> argument
  1059. to <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionParser</span></code></a>.</p>
  1060. </dd></dl>
  1061. <p>The following option attributes may be passed as keyword arguments to
  1062. <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><code class="xref py py-meth docutils literal notranslate"><span class="pre">OptionParser.add_option()</span></code></a>. If you pass an option attribute that is not
  1063. relevant to a particular option, or fail to pass a required option attribute,
  1064. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> raises <a class="reference internal" href="#optparse.OptionError" title="optparse.OptionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OptionError</span></code></a>.</p>
  1065. <dl class="py attribute">
  1066. <dt class="sig sig-object py" id="optparse.Option.action">
  1067. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">action</span></span><a class="headerlink" href="#optparse.Option.action" title="Link to this definition">¶</a></dt>
  1068. <dd><p>(default: <code class="docutils literal notranslate"><span class="pre">&quot;store&quot;</span></code>)</p>
  1069. <p>Determines <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>’s behaviour when this option is seen on the
  1070. command line; the available options are documented <a class="reference internal" href="#optparse-standard-option-actions"><span class="std std-ref">here</span></a>.</p>
  1071. </dd></dl>
  1072. <dl class="py attribute">
  1073. <dt class="sig sig-object py" id="optparse.Option.type">
  1074. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">type</span></span><a class="headerlink" href="#optparse.Option.type" title="Link to this definition">¶</a></dt>
  1075. <dd><p>(default: <code class="docutils literal notranslate"><span class="pre">&quot;string&quot;</span></code>)</p>
  1076. <p>The argument type expected by this option (e.g., <code class="docutils literal notranslate"><span class="pre">&quot;string&quot;</span></code> or <code class="docutils literal notranslate"><span class="pre">&quot;int&quot;</span></code>);
  1077. the available option types are documented <a class="reference internal" href="#optparse-standard-option-types"><span class="std std-ref">here</span></a>.</p>
  1078. </dd></dl>
  1079. <dl class="py attribute">
  1080. <dt class="sig sig-object py" id="optparse.Option.dest">
  1081. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">dest</span></span><a class="headerlink" href="#optparse.Option.dest" title="Link to this definition">¶</a></dt>
  1082. <dd><p>(default: derived from option strings)</p>
  1083. <p>If the option’s action implies writing or modifying a value somewhere, this
  1084. tells <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> where to write it: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a> names an
  1085. attribute of the <code class="docutils literal notranslate"><span class="pre">options</span></code> object that <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> builds as it parses
  1086. the command line.</p>
  1087. </dd></dl>
  1088. <dl class="py attribute">
  1089. <dt class="sig sig-object py" id="optparse.Option.default">
  1090. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">default</span></span><a class="headerlink" href="#optparse.Option.default" title="Link to this definition">¶</a></dt>
  1091. <dd><p>The value to use for this option’s destination if the option is not seen on
  1092. the command line. See also <a class="reference internal" href="#optparse.OptionParser.set_defaults" title="optparse.OptionParser.set_defaults"><code class="xref py py-meth docutils literal notranslate"><span class="pre">OptionParser.set_defaults()</span></code></a>.</p>
  1093. </dd></dl>
  1094. <dl class="py attribute">
  1095. <dt class="sig sig-object py" id="optparse.Option.nargs">
  1096. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">nargs</span></span><a class="headerlink" href="#optparse.Option.nargs" title="Link to this definition">¶</a></dt>
  1097. <dd><p>(default: 1)</p>
  1098. <p>How many arguments of type <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a> should be consumed when this
  1099. option is seen. If &gt; 1, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> will store a tuple of values to
  1100. <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>.</p>
  1101. </dd></dl>
  1102. <dl class="py attribute">
  1103. <dt class="sig sig-object py" id="optparse.Option.const">
  1104. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">const</span></span><a class="headerlink" href="#optparse.Option.const" title="Link to this definition">¶</a></dt>
  1105. <dd><p>For actions that store a constant value, the constant value to store.</p>
  1106. </dd></dl>
  1107. <dl class="py attribute">
  1108. <dt class="sig sig-object py" id="optparse.Option.choices">
  1109. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">choices</span></span><a class="headerlink" href="#optparse.Option.choices" title="Link to this definition">¶</a></dt>
  1110. <dd><p>For options of type <code class="docutils literal notranslate"><span class="pre">&quot;choice&quot;</span></code>, the list of strings the user may choose
  1111. from.</p>
  1112. </dd></dl>
  1113. <dl class="py attribute">
  1114. <dt class="sig sig-object py" id="optparse.Option.callback">
  1115. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">callback</span></span><a class="headerlink" href="#optparse.Option.callback" title="Link to this definition">¶</a></dt>
  1116. <dd><p>For options with action <code class="docutils literal notranslate"><span class="pre">&quot;callback&quot;</span></code>, the callable to call when this option
  1117. is seen. See section <a class="reference internal" href="#optparse-option-callbacks"><span class="std std-ref">Option Callbacks</span></a> for detail on the
  1118. arguments passed to the callable.</p>
  1119. </dd></dl>
  1120. <dl class="py attribute">
  1121. <dt class="sig sig-object py" id="optparse.Option.callback_args">
  1122. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">callback_args</span></span><a class="headerlink" href="#optparse.Option.callback_args" title="Link to this definition">¶</a></dt>
  1123. <dt class="sig sig-object py" id="optparse.Option.callback_kwargs">
  1124. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">callback_kwargs</span></span><a class="headerlink" href="#optparse.Option.callback_kwargs" title="Link to this definition">¶</a></dt>
  1125. <dd><p>Additional positional and keyword arguments to pass to <code class="docutils literal notranslate"><span class="pre">callback</span></code> after the
  1126. four standard callback arguments.</p>
  1127. </dd></dl>
  1128. <dl class="py attribute">
  1129. <dt class="sig sig-object py" id="optparse.Option.help">
  1130. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">help</span></span><a class="headerlink" href="#optparse.Option.help" title="Link to this definition">¶</a></dt>
  1131. <dd><p>Help text to print for this option when listing all available options after
  1132. the user supplies a <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><code class="xref py py-attr docutils literal notranslate"><span class="pre">help</span></code></a> option (such as <code class="docutils literal notranslate"><span class="pre">--help</span></code>). If
  1133. no help text is supplied, the option will be listed without help text. To
  1134. hide this option, use the special value <code class="xref py py-const docutils literal notranslate"><span class="pre">optparse.SUPPRESS_HELP</span></code>.</p>
  1135. </dd></dl>
  1136. <dl class="py attribute">
  1137. <dt class="sig sig-object py" id="optparse.Option.metavar">
  1138. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">metavar</span></span><a class="headerlink" href="#optparse.Option.metavar" title="Link to this definition">¶</a></dt>
  1139. <dd><p>(default: derived from option strings)</p>
  1140. <p>Stand-in for the option argument(s) to use when printing help text. See
  1141. section <a class="reference internal" href="#optparse-tutorial"><span class="std std-ref">Tutorial</span></a> for an example.</p>
  1142. </dd></dl>
  1143. </section>
  1144. <section id="standard-option-actions">
  1145. <span id="optparse-standard-option-actions"></span><h3>Standard option actions<a class="headerlink" href="#standard-option-actions" title="Link to this heading">¶</a></h3>
  1146. <p>The various option actions all have slightly different requirements and effects.
  1147. Most actions have several relevant option attributes which you may specify to
  1148. guide <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>’s behaviour; a few have required attributes, which you
  1149. must specify for any option using that action.</p>
  1150. <ul>
  1151. <li><p><code class="docutils literal notranslate"><span class="pre">&quot;store&quot;</span></code> [relevant: <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>,
  1152. <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">nargs</span></code></a>, <a class="reference internal" href="#optparse.Option.choices" title="optparse.Option.choices"><code class="xref py py-attr docutils literal notranslate"><span class="pre">choices</span></code></a>]</p>
  1153. <p>The option must be followed by an argument, which is converted to a value
  1154. according to <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a> and stored in <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>. If
  1155. <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">nargs</span></code></a> &gt; 1, multiple arguments will be consumed from the
  1156. command line; all will be converted according to <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a> and
  1157. stored to <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a> as a tuple. See the
  1158. <a class="reference internal" href="#optparse-standard-option-types"><span class="std std-ref">Standard option types</span></a> section.</p>
  1159. <p>If <a class="reference internal" href="#optparse.Option.choices" title="optparse.Option.choices"><code class="xref py py-attr docutils literal notranslate"><span class="pre">choices</span></code></a> is supplied (a list or tuple of strings), the type
  1160. defaults to <code class="docutils literal notranslate"><span class="pre">&quot;choice&quot;</span></code>.</p>
  1161. <p>If <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a> is not supplied, it defaults to <code class="docutils literal notranslate"><span class="pre">&quot;string&quot;</span></code>.</p>
  1162. <p>If <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a> is not supplied, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> derives a destination
  1163. from the first long option string (e.g., <code class="docutils literal notranslate"><span class="pre">--foo-bar</span></code> implies
  1164. <code class="docutils literal notranslate"><span class="pre">foo_bar</span></code>). If there are no long option strings, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> derives a
  1165. destination from the first short option string (e.g., <code class="docutils literal notranslate"><span class="pre">-f</span></code> implies <code class="docutils literal notranslate"><span class="pre">f</span></code>).</p>
  1166. <p>Example:</p>
  1167. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-f&quot;</span><span class="p">)</span>
  1168. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-p&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s2">&quot;float&quot;</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;point&quot;</span><span class="p">)</span>
  1169. </pre></div>
  1170. </div>
  1171. <p>As it parses the command line</p>
  1172. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">-</span><span class="n">f</span> <span class="n">foo</span><span class="o">.</span><span class="n">txt</span> <span class="o">-</span><span class="n">p</span> <span class="mi">1</span> <span class="o">-</span><span class="mf">3.5</span> <span class="mi">4</span> <span class="o">-</span><span class="n">fbar</span><span class="o">.</span><span class="n">txt</span>
  1173. </pre></div>
  1174. </div>
  1175. <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> will set</p>
  1176. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">options</span><span class="o">.</span><span class="n">f</span> <span class="o">=</span> <span class="s2">&quot;foo.txt&quot;</span>
  1177. <span class="n">options</span><span class="o">.</span><span class="n">point</span> <span class="o">=</span> <span class="p">(</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.5</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">)</span>
  1178. <span class="n">options</span><span class="o">.</span><span class="n">f</span> <span class="o">=</span> <span class="s2">&quot;bar.txt&quot;</span>
  1179. </pre></div>
  1180. </div>
  1181. </li>
  1182. <li><p><code class="docutils literal notranslate"><span class="pre">&quot;store_const&quot;</span></code> [required: <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><code class="xref py py-attr docutils literal notranslate"><span class="pre">const</span></code></a>; relevant:
  1183. <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>]</p>
  1184. <p>The value <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><code class="xref py py-attr docutils literal notranslate"><span class="pre">const</span></code></a> is stored in <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>.</p>
  1185. <p>Example:</p>
  1186. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-q&quot;</span><span class="p">,</span> <span class="s2">&quot;--quiet&quot;</span><span class="p">,</span>
  1187. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_const&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">)</span>
  1188. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-v&quot;</span><span class="p">,</span> <span class="s2">&quot;--verbose&quot;</span><span class="p">,</span>
  1189. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_const&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">)</span>
  1190. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--noisy&quot;</span><span class="p">,</span>
  1191. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_const&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">)</span>
  1192. </pre></div>
  1193. </div>
  1194. <p>If <code class="docutils literal notranslate"><span class="pre">--noisy</span></code> is seen, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> will set</p>
  1195. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">options</span><span class="o">.</span><span class="n">verbose</span> <span class="o">=</span> <span class="mi">2</span>
  1196. </pre></div>
  1197. </div>
  1198. </li>
  1199. <li><p><code class="docutils literal notranslate"><span class="pre">&quot;store_true&quot;</span></code> [relevant: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>]</p>
  1200. <p>A special case of <code class="docutils literal notranslate"><span class="pre">&quot;store_const&quot;</span></code> that stores <code class="docutils literal notranslate"><span class="pre">True</span></code> to
  1201. <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>.</p>
  1202. </li>
  1203. <li><p><code class="docutils literal notranslate"><span class="pre">&quot;store_false&quot;</span></code> [relevant: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>]</p>
  1204. <p>Like <code class="docutils literal notranslate"><span class="pre">&quot;store_true&quot;</span></code>, but stores <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  1205. <p>Example:</p>
  1206. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--clobber&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;clobber&quot;</span><span class="p">)</span>
  1207. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--no-clobber&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_false&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;clobber&quot;</span><span class="p">)</span>
  1208. </pre></div>
  1209. </div>
  1210. </li>
  1211. <li><p><code class="docutils literal notranslate"><span class="pre">&quot;append&quot;</span></code> [relevant: <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>,
  1212. <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">nargs</span></code></a>, <a class="reference internal" href="#optparse.Option.choices" title="optparse.Option.choices"><code class="xref py py-attr docutils literal notranslate"><span class="pre">choices</span></code></a>]</p>
  1213. <p>The option must be followed by an argument, which is appended to the list in
  1214. <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>. If no default value for <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a> is
  1215. supplied, an empty list is automatically created when <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> first
  1216. encounters this option on the command-line. If <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">nargs</span></code></a> &gt; 1,
  1217. multiple arguments are consumed, and a tuple of length <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">nargs</span></code></a>
  1218. is appended to <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>.</p>
  1219. <p>The defaults for <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a> and <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a> are the same as
  1220. for the <code class="docutils literal notranslate"><span class="pre">&quot;store&quot;</span></code> action.</p>
  1221. <p>Example:</p>
  1222. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-t&quot;</span><span class="p">,</span> <span class="s2">&quot;--tracks&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;append&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s2">&quot;int&quot;</span><span class="p">)</span>
  1223. </pre></div>
  1224. </div>
  1225. <p>If <code class="docutils literal notranslate"><span class="pre">-t3</span></code> is seen on the command-line, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> does the equivalent
  1226. of:</p>
  1227. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">options</span><span class="o">.</span><span class="n">tracks</span> <span class="o">=</span> <span class="p">[]</span>
  1228. <span class="n">options</span><span class="o">.</span><span class="n">tracks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="s2">&quot;3&quot;</span><span class="p">))</span>
  1229. </pre></div>
  1230. </div>
  1231. <p>If, a little later on, <code class="docutils literal notranslate"><span class="pre">--tracks=4</span></code> is seen, it does:</p>
  1232. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">options</span><span class="o">.</span><span class="n">tracks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="s2">&quot;4&quot;</span><span class="p">))</span>
  1233. </pre></div>
  1234. </div>
  1235. <p>The <code class="docutils literal notranslate"><span class="pre">append</span></code> action calls the <code class="docutils literal notranslate"><span class="pre">append</span></code> method on the current value of the
  1236. option. This means that any default value specified must have an <code class="docutils literal notranslate"><span class="pre">append</span></code>
  1237. method. It also means that if the default value is non-empty, the default
  1238. elements will be present in the parsed value for the option, with any values
  1239. from the command line appended after those default values:</p>
  1240. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--files&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;append&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;~/.mypkg/defaults&#39;</span><span class="p">])</span>
  1241. <span class="gp">&gt;&gt;&gt; </span><span class="n">opts</span><span class="p">,</span> <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><span class="s1">&#39;--files&#39;</span><span class="p">,</span> <span class="s1">&#39;overrides.mypkg&#39;</span><span class="p">])</span>
  1242. <span class="gp">&gt;&gt;&gt; </span><span class="n">opts</span><span class="o">.</span><span class="n">files</span>
  1243. <span class="go">[&#39;~/.mypkg/defaults&#39;, &#39;overrides.mypkg&#39;]</span>
  1244. </pre></div>
  1245. </div>
  1246. </li>
  1247. <li><p><code class="docutils literal notranslate"><span class="pre">&quot;append_const&quot;</span></code> [required: <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><code class="xref py py-attr docutils literal notranslate"><span class="pre">const</span></code></a>; relevant:
  1248. <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>]</p>
  1249. <p>Like <code class="docutils literal notranslate"><span class="pre">&quot;store_const&quot;</span></code>, but the value <a class="reference internal" href="#optparse.Option.const" title="optparse.Option.const"><code class="xref py py-attr docutils literal notranslate"><span class="pre">const</span></code></a> is appended to
  1250. <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>; as with <code class="docutils literal notranslate"><span class="pre">&quot;append&quot;</span></code>, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a> defaults to
  1251. <code class="docutils literal notranslate"><span class="pre">None</span></code>, and an empty list is automatically created the first time the option
  1252. is encountered.</p>
  1253. </li>
  1254. <li><p><code class="docutils literal notranslate"><span class="pre">&quot;count&quot;</span></code> [relevant: <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>]</p>
  1255. <p>Increment the integer stored at <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>. If no default value is
  1256. supplied, <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a> is set to zero before being incremented the
  1257. first time.</p>
  1258. <p>Example:</p>
  1259. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-v&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;count&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbosity&quot;</span><span class="p">)</span>
  1260. </pre></div>
  1261. </div>
  1262. <p>The first time <code class="docutils literal notranslate"><span class="pre">-v</span></code> is seen on the command line, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> does the
  1263. equivalent of:</p>
  1264. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">options</span><span class="o">.</span><span class="n">verbosity</span> <span class="o">=</span> <span class="mi">0</span>
  1265. <span class="n">options</span><span class="o">.</span><span class="n">verbosity</span> <span class="o">+=</span> <span class="mi">1</span>
  1266. </pre></div>
  1267. </div>
  1268. <p>Every subsequent occurrence of <code class="docutils literal notranslate"><span class="pre">-v</span></code> results in</p>
  1269. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">options</span><span class="o">.</span><span class="n">verbosity</span> <span class="o">+=</span> <span class="mi">1</span>
  1270. </pre></div>
  1271. </div>
  1272. </li>
  1273. <li><p><code class="docutils literal notranslate"><span class="pre">&quot;callback&quot;</span></code> [required: <a class="reference internal" href="#optparse.Option.callback" title="optparse.Option.callback"><code class="xref py py-attr docutils literal notranslate"><span class="pre">callback</span></code></a>; relevant:
  1274. <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>, <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">nargs</span></code></a>, <a class="reference internal" href="#optparse.Option.callback_args" title="optparse.Option.callback_args"><code class="xref py py-attr docutils literal notranslate"><span class="pre">callback_args</span></code></a>,
  1275. <a class="reference internal" href="#optparse.Option.callback_kwargs" title="optparse.Option.callback_kwargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">callback_kwargs</span></code></a>]</p>
  1276. <p>Call the function specified by <a class="reference internal" href="#optparse.Option.callback" title="optparse.Option.callback"><code class="xref py py-attr docutils literal notranslate"><span class="pre">callback</span></code></a>, which is called as</p>
  1277. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">func</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
  1278. </pre></div>
  1279. </div>
  1280. <p>See section <a class="reference internal" href="#optparse-option-callbacks"><span class="std std-ref">Option Callbacks</span></a> for more detail.</p>
  1281. </li>
  1282. <li><p><code class="docutils literal notranslate"><span class="pre">&quot;help&quot;</span></code></p>
  1283. <p>Prints a complete help message for all the options in the current option
  1284. parser. The help message is constructed from the <code class="docutils literal notranslate"><span class="pre">usage</span></code> string passed to
  1285. OptionParser’s constructor and the <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><code class="xref py py-attr docutils literal notranslate"><span class="pre">help</span></code></a> string passed to every
  1286. option.</p>
  1287. <p>If no <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><code class="xref py py-attr docutils literal notranslate"><span class="pre">help</span></code></a> string is supplied for an option, it will still be
  1288. listed in the help message. To omit an option entirely, use the special value
  1289. <code class="xref py py-const docutils literal notranslate"><span class="pre">optparse.SUPPRESS_HELP</span></code>.</p>
  1290. <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> automatically adds a <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><code class="xref py py-attr docutils literal notranslate"><span class="pre">help</span></code></a> option to all
  1291. OptionParsers, so you do not normally need to create one.</p>
  1292. <p>Example:</p>
  1293. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">OptionParser</span><span class="p">,</span> <span class="n">SUPPRESS_HELP</span>
  1294. <span class="c1"># usually, a help option is added automatically, but that can</span>
  1295. <span class="c1"># be suppressed using the add_help_option argument</span>
  1296. <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">add_help_option</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
  1297. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</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> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;help&quot;</span><span class="p">)</span>
  1298. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-v&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;verbose&quot;</span><span class="p">,</span>
  1299. <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Be moderately verbose&quot;</span><span class="p">)</span>
  1300. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--file&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;filename&quot;</span><span class="p">,</span>
  1301. <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Input file to read data from&quot;</span><span class="p">)</span>
  1302. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--secret&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="n">SUPPRESS_HELP</span><span class="p">)</span>
  1303. </pre></div>
  1304. </div>
  1305. <p>If <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> sees either <code class="docutils literal notranslate"><span class="pre">-h</span></code> or <code class="docutils literal notranslate"><span class="pre">--help</span></code> on the command line,
  1306. it will print something like the following help message to stdout (assuming
  1307. <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> is <code class="docutils literal notranslate"><span class="pre">&quot;foo.py&quot;</span></code>):</p>
  1308. <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>Usage: foo.py [options]
  1309. Options:
  1310. -h, --help Show this help message and exit
  1311. -v Be moderately verbose
  1312. --file=FILENAME Input file to read data from
  1313. </pre></div>
  1314. </div>
  1315. <p>After printing the help message, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> terminates your process with
  1316. <code class="docutils literal notranslate"><span class="pre">sys.exit(0)</span></code>.</p>
  1317. </li>
  1318. <li><p><code class="docutils literal notranslate"><span class="pre">&quot;version&quot;</span></code></p>
  1319. <p>Prints the version number supplied to the OptionParser to stdout and exits.
  1320. The version number is actually formatted and printed by the
  1321. <code class="docutils literal notranslate"><span class="pre">print_version()</span></code> method of OptionParser. Generally only relevant if the
  1322. <code class="docutils literal notranslate"><span class="pre">version</span></code> argument is supplied to the OptionParser constructor. As with
  1323. <a class="reference internal" href="#optparse.Option.help" title="optparse.Option.help"><code class="xref py py-attr docutils literal notranslate"><span class="pre">help</span></code></a> options, you will rarely create <code class="docutils literal notranslate"><span class="pre">version</span></code> options,
  1324. since <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> automatically adds them when needed.</p>
  1325. </li>
  1326. </ul>
  1327. </section>
  1328. <section id="standard-option-types">
  1329. <span id="optparse-standard-option-types"></span><h3>Standard option types<a class="headerlink" href="#standard-option-types" title="Link to this heading">¶</a></h3>
  1330. <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> has five built-in option types: <code class="docutils literal notranslate"><span class="pre">&quot;string&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;int&quot;</span></code>,
  1331. <code class="docutils literal notranslate"><span class="pre">&quot;choice&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;float&quot;</span></code> and <code class="docutils literal notranslate"><span class="pre">&quot;complex&quot;</span></code>. If you need to add new
  1332. option types, see section <a class="reference internal" href="#optparse-extending-optparse"><span class="std std-ref">Extending optparse</span></a>.</p>
  1333. <p>Arguments to string options are not checked or converted in any way: the text on
  1334. the command line is stored in the destination (or passed to the callback) as-is.</p>
  1335. <p>Integer arguments (type <code class="docutils literal notranslate"><span class="pre">&quot;int&quot;</span></code>) are parsed as follows:</p>
  1336. <ul class="simple">
  1337. <li><p>if the number starts with <code class="docutils literal notranslate"><span class="pre">0x</span></code>, it is parsed as a hexadecimal number</p></li>
  1338. <li><p>if the number starts with <code class="docutils literal notranslate"><span class="pre">0</span></code>, it is parsed as an octal number</p></li>
  1339. <li><p>if the number starts with <code class="docutils literal notranslate"><span class="pre">0b</span></code>, it is parsed as a binary number</p></li>
  1340. <li><p>otherwise, the number is parsed as a decimal number</p></li>
  1341. </ul>
  1342. <p>The conversion is done by calling <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> with the appropriate base (2, 8,
  1343. 10, or 16). If this fails, so will <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>, although with a more useful
  1344. error message.</p>
  1345. <p><code class="docutils literal notranslate"><span class="pre">&quot;float&quot;</span></code> and <code class="docutils literal notranslate"><span class="pre">&quot;complex&quot;</span></code> option arguments are converted directly with
  1346. <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-func docutils literal notranslate"><span class="pre">float()</span></code></a> and <a class="reference internal" href="functions.html#complex" title="complex"><code class="xref py py-func docutils literal notranslate"><span class="pre">complex()</span></code></a>, with similar error-handling.</p>
  1347. <p><code class="docutils literal notranslate"><span class="pre">&quot;choice&quot;</span></code> options are a subtype of <code class="docutils literal notranslate"><span class="pre">&quot;string&quot;</span></code> options. The
  1348. <a class="reference internal" href="#optparse.Option.choices" title="optparse.Option.choices"><code class="xref py py-attr docutils literal notranslate"><span class="pre">choices</span></code></a> option attribute (a sequence of strings) defines the
  1349. set of allowed option arguments. <code class="xref py py-func docutils literal notranslate"><span class="pre">optparse.check_choice()</span></code> compares
  1350. user-supplied option arguments against this master list and raises
  1351. <a class="reference internal" href="#optparse.OptionValueError" title="optparse.OptionValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OptionValueError</span></code></a> if an invalid string is given.</p>
  1352. </section>
  1353. <section id="parsing-arguments">
  1354. <span id="optparse-parsing-arguments"></span><h3>Parsing arguments<a class="headerlink" href="#parsing-arguments" title="Link to this heading">¶</a></h3>
  1355. <p>The whole point of creating and populating an OptionParser is to call its
  1356. <a class="reference internal" href="#optparse.OptionParser.parse_args" title="optparse.OptionParser.parse_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse_args()</span></code></a> method.</p>
  1357. <dl class="py method">
  1358. <dt class="sig sig-object py" id="optparse.OptionParser.parse_args">
  1359. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">parse_args</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">values</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.parse_args" title="Link to this definition">¶</a></dt>
  1360. <dd><p>Parse the command-line options found in <em>args</em>.</p>
  1361. <p>The input parameters are</p>
  1362. <dl class="simple">
  1363. <dt><code class="docutils literal notranslate"><span class="pre">args</span></code></dt><dd><p>the list of arguments to process (default: <code class="docutils literal notranslate"><span class="pre">sys.argv[1:]</span></code>)</p>
  1364. </dd>
  1365. <dt><code class="docutils literal notranslate"><span class="pre">values</span></code></dt><dd><p>an <a class="reference internal" href="#optparse.Values" title="optparse.Values"><code class="xref py py-class docutils literal notranslate"><span class="pre">Values</span></code></a> object to store option arguments in (default: a
  1366. new instance of <a class="reference internal" href="#optparse.Values" title="optparse.Values"><code class="xref py py-class docutils literal notranslate"><span class="pre">Values</span></code></a>) – if you give an existing object, the
  1367. option defaults will not be initialized on it</p>
  1368. </dd>
  1369. </dl>
  1370. <p>and the return value is a pair <code class="docutils literal notranslate"><span class="pre">(options,</span> <span class="pre">args)</span></code> where</p>
  1371. <dl class="simple">
  1372. <dt><code class="docutils literal notranslate"><span class="pre">options</span></code></dt><dd><p>the same object that was passed in as <em>values</em>, or the <code class="docutils literal notranslate"><span class="pre">optparse.Values</span></code>
  1373. instance created by <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a></p>
  1374. </dd>
  1375. <dt><code class="docutils literal notranslate"><span class="pre">args</span></code></dt><dd><p>the leftover positional arguments after all options have been processed</p>
  1376. </dd>
  1377. </dl>
  1378. </dd></dl>
  1379. <p>The most common usage is to supply neither keyword argument. If you supply
  1380. <code class="docutils literal notranslate"><span class="pre">values</span></code>, it will be modified with repeated <a class="reference internal" href="functions.html#setattr" title="setattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">setattr()</span></code></a> calls (roughly one
  1381. for every option argument stored to an option destination) and returned by
  1382. <a class="reference internal" href="#optparse.OptionParser.parse_args" title="optparse.OptionParser.parse_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse_args()</span></code></a>.</p>
  1383. <p>If <a class="reference internal" href="#optparse.OptionParser.parse_args" title="optparse.OptionParser.parse_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse_args()</span></code></a> encounters any errors in the argument list, it calls the
  1384. OptionParser’s <code class="xref py py-meth docutils literal notranslate"><span class="pre">error()</span></code> method with an appropriate end-user error message.
  1385. This ultimately terminates your process with an exit status of 2 (the
  1386. traditional Unix exit status for command-line errors).</p>
  1387. </section>
  1388. <section id="querying-and-manipulating-your-option-parser">
  1389. <span id="optparse-querying-manipulating-option-parser"></span><h3>Querying and manipulating your option parser<a class="headerlink" href="#querying-and-manipulating-your-option-parser" title="Link to this heading">¶</a></h3>
  1390. <p>The default behavior of the option parser can be customized slightly, and you
  1391. can also poke around your option parser and see what’s there. OptionParser
  1392. provides several methods to help you out:</p>
  1393. <dl class="py method">
  1394. <dt class="sig sig-object py" id="optparse.OptionParser.disable_interspersed_args">
  1395. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">disable_interspersed_args</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.disable_interspersed_args" title="Link to this definition">¶</a></dt>
  1396. <dd><p>Set parsing to stop on the first non-option. For example, if <code class="docutils literal notranslate"><span class="pre">-a</span></code> and
  1397. <code class="docutils literal notranslate"><span class="pre">-b</span></code> are both simple options that take no arguments, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>
  1398. normally accepts this syntax:</p>
  1399. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">prog</span> <span class="o">-</span><span class="n">a</span> <span class="n">arg1</span> <span class="o">-</span><span class="n">b</span> <span class="n">arg2</span>
  1400. </pre></div>
  1401. </div>
  1402. <p>and treats it as equivalent to</p>
  1403. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">prog</span> <span class="o">-</span><span class="n">a</span> <span class="o">-</span><span class="n">b</span> <span class="n">arg1</span> <span class="n">arg2</span>
  1404. </pre></div>
  1405. </div>
  1406. <p>To disable this feature, call <a class="reference internal" href="#optparse.OptionParser.disable_interspersed_args" title="optparse.OptionParser.disable_interspersed_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">disable_interspersed_args()</span></code></a>. This
  1407. restores traditional Unix syntax, where option parsing stops with the first
  1408. non-option argument.</p>
  1409. <p>Use this if you have a command processor which runs another command which has
  1410. options of its own and you want to make sure these options don’t get
  1411. confused. For example, each command might have a different set of options.</p>
  1412. </dd></dl>
  1413. <dl class="py method">
  1414. <dt class="sig sig-object py" id="optparse.OptionParser.enable_interspersed_args">
  1415. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">enable_interspersed_args</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.enable_interspersed_args" title="Link to this definition">¶</a></dt>
  1416. <dd><p>Set parsing to not stop on the first non-option, allowing interspersing
  1417. switches with command arguments. This is the default behavior.</p>
  1418. </dd></dl>
  1419. <dl class="py method">
  1420. <dt class="sig sig-object py" id="optparse.OptionParser.get_option">
  1421. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">get_option</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">opt_str</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.get_option" title="Link to this definition">¶</a></dt>
  1422. <dd><p>Returns the Option instance with the option string <em>opt_str</em>, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if
  1423. no options have that option string.</p>
  1424. </dd></dl>
  1425. <dl class="py method">
  1426. <dt class="sig sig-object py" id="optparse.OptionParser.has_option">
  1427. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">has_option</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">opt_str</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.has_option" title="Link to this definition">¶</a></dt>
  1428. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the OptionParser has an option with option string <em>opt_str</em>
  1429. (e.g., <code class="docutils literal notranslate"><span class="pre">-q</span></code> or <code class="docutils literal notranslate"><span class="pre">--verbose</span></code>).</p>
  1430. </dd></dl>
  1431. <dl class="py method">
  1432. <dt class="sig sig-object py" id="optparse.OptionParser.remove_option">
  1433. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">remove_option</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">opt_str</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.remove_option" title="Link to this definition">¶</a></dt>
  1434. <dd><p>If the <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionParser</span></code></a> has an option corresponding to <em>opt_str</em>, that
  1435. option is removed. If that option provided any other option strings, all of
  1436. those option strings become invalid. If <em>opt_str</em> does not occur in any
  1437. option belonging to this <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionParser</span></code></a>, raises <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
  1438. </dd></dl>
  1439. </section>
  1440. <section id="conflicts-between-options">
  1441. <span id="optparse-conflicts-between-options"></span><h3>Conflicts between options<a class="headerlink" href="#conflicts-between-options" title="Link to this heading">¶</a></h3>
  1442. <p>If you’re not careful, it’s easy to define options with conflicting option
  1443. strings:</p>
  1444. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-n&quot;</span><span class="p">,</span> <span class="s2">&quot;--dry-run&quot;</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
  1445. <span class="o">...</span>
  1446. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-n&quot;</span><span class="p">,</span> <span class="s2">&quot;--noisy&quot;</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
  1447. </pre></div>
  1448. </div>
  1449. <p>(This is particularly true if you’ve defined your own OptionParser subclass with
  1450. some standard options.)</p>
  1451. <p>Every time you add an option, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> checks for conflicts with existing
  1452. options. If it finds any, it invokes the current conflict-handling mechanism.
  1453. You can set the conflict-handling mechanism either in the constructor:</p>
  1454. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="o">...</span><span class="p">,</span> <span class="n">conflict_handler</span><span class="o">=</span><span class="n">handler</span><span class="p">)</span>
  1455. </pre></div>
  1456. </div>
  1457. <p>or with a separate call:</p>
  1458. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">set_conflict_handler</span><span class="p">(</span><span class="n">handler</span><span class="p">)</span>
  1459. </pre></div>
  1460. </div>
  1461. <p>The available conflict handlers are:</p>
  1462. <blockquote>
  1463. <div><dl class="simple">
  1464. <dt><code class="docutils literal notranslate"><span class="pre">&quot;error&quot;</span></code> (default)</dt><dd><p>assume option conflicts are a programming error and raise
  1465. <a class="reference internal" href="#optparse.OptionConflictError" title="optparse.OptionConflictError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OptionConflictError</span></code></a></p>
  1466. </dd>
  1467. <dt><code class="docutils literal notranslate"><span class="pre">&quot;resolve&quot;</span></code></dt><dd><p>resolve option conflicts intelligently (see below)</p>
  1468. </dd>
  1469. </dl>
  1470. </div></blockquote>
  1471. <p>As an example, let’s define an <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionParser</span></code></a> that resolves conflicts
  1472. intelligently and add conflicting options to it:</p>
  1473. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">conflict_handler</span><span class="o">=</span><span class="s2">&quot;resolve&quot;</span><span class="p">)</span>
  1474. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-n&quot;</span><span class="p">,</span> <span class="s2">&quot;--dry-run&quot;</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;do no harm&quot;</span><span class="p">)</span>
  1475. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-n&quot;</span><span class="p">,</span> <span class="s2">&quot;--noisy&quot;</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;be noisy&quot;</span><span class="p">)</span>
  1476. </pre></div>
  1477. </div>
  1478. <p>At this point, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> detects that a previously added option is already
  1479. using the <code class="docutils literal notranslate"><span class="pre">-n</span></code> option string. Since <code class="docutils literal notranslate"><span class="pre">conflict_handler</span></code> is <code class="docutils literal notranslate"><span class="pre">&quot;resolve&quot;</span></code>,
  1480. it resolves the situation by removing <code class="docutils literal notranslate"><span class="pre">-n</span></code> from the earlier option’s list of
  1481. option strings. Now <code class="docutils literal notranslate"><span class="pre">--dry-run</span></code> is the only way for the user to activate
  1482. that option. If the user asks for help, the help message will reflect that:</p>
  1483. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Options</span><span class="p">:</span>
  1484. <span class="o">--</span><span class="n">dry</span><span class="o">-</span><span class="n">run</span> <span class="n">do</span> <span class="n">no</span> <span class="n">harm</span>
  1485. <span class="o">...</span>
  1486. <span class="o">-</span><span class="n">n</span><span class="p">,</span> <span class="o">--</span><span class="n">noisy</span> <span class="n">be</span> <span class="n">noisy</span>
  1487. </pre></div>
  1488. </div>
  1489. <p>It’s possible to whittle away the option strings for a previously added option
  1490. until there are none left, and the user has no way of invoking that option from
  1491. the command-line. In that case, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> removes that option completely,
  1492. so it doesn’t show up in help text or anywhere else. Carrying on with our
  1493. existing OptionParser:</p>
  1494. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--dry-run&quot;</span><span class="p">,</span> <span class="o">...</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s2">&quot;new dry-run option&quot;</span><span class="p">)</span>
  1495. </pre></div>
  1496. </div>
  1497. <p>At this point, the original <code class="docutils literal notranslate"><span class="pre">-n</span></code>/<code class="docutils literal notranslate"><span class="pre">--dry-run</span></code> option is no longer
  1498. accessible, so <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> removes it, leaving this help text:</p>
  1499. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Options</span><span class="p">:</span>
  1500. <span class="o">...</span>
  1501. <span class="o">-</span><span class="n">n</span><span class="p">,</span> <span class="o">--</span><span class="n">noisy</span> <span class="n">be</span> <span class="n">noisy</span>
  1502. <span class="o">--</span><span class="n">dry</span><span class="o">-</span><span class="n">run</span> <span class="n">new</span> <span class="n">dry</span><span class="o">-</span><span class="n">run</span> <span class="n">option</span>
  1503. </pre></div>
  1504. </div>
  1505. </section>
  1506. <section id="cleanup">
  1507. <span id="optparse-cleanup"></span><h3>Cleanup<a class="headerlink" href="#cleanup" title="Link to this heading">¶</a></h3>
  1508. <p>OptionParser instances have several cyclic references. This should not be a
  1509. problem for Python’s garbage collector, but you may wish to break the cyclic
  1510. references explicitly by calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">destroy()</span></code> on your
  1511. OptionParser once you are done with it. This is particularly useful in
  1512. long-running applications where large object graphs are reachable from your
  1513. OptionParser.</p>
  1514. </section>
  1515. <section id="other-methods">
  1516. <span id="optparse-other-methods"></span><h3>Other methods<a class="headerlink" href="#other-methods" title="Link to this heading">¶</a></h3>
  1517. <p>OptionParser supports several other public methods:</p>
  1518. <dl class="py method">
  1519. <dt class="sig sig-object py" id="optparse.OptionParser.set_usage">
  1520. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">set_usage</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">usage</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.set_usage" title="Link to this definition">¶</a></dt>
  1521. <dd><p>Set the usage string according to the rules described above for the <code class="docutils literal notranslate"><span class="pre">usage</span></code>
  1522. constructor keyword argument. Passing <code class="docutils literal notranslate"><span class="pre">None</span></code> sets the default usage
  1523. string; use <code class="xref py py-const docutils literal notranslate"><span class="pre">optparse.SUPPRESS_USAGE</span></code> to suppress a usage message.</p>
  1524. </dd></dl>
  1525. <dl class="py method">
  1526. <dt class="sig sig-object py" id="optparse.OptionParser.print_usage">
  1527. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">print_usage</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.print_usage" title="Link to this definition">¶</a></dt>
  1528. <dd><p>Print the usage message for the current program (<code class="docutils literal notranslate"><span class="pre">self.usage</span></code>) to <em>file</em>
  1529. (default stdout). Any occurrence of the string <code class="docutils literal notranslate"><span class="pre">%prog</span></code> in <code class="docutils literal notranslate"><span class="pre">self.usage</span></code>
  1530. is replaced with the name of the current program. Does nothing if
  1531. <code class="docutils literal notranslate"><span class="pre">self.usage</span></code> is empty or not defined.</p>
  1532. </dd></dl>
  1533. <dl class="py method">
  1534. <dt class="sig sig-object py" id="optparse.OptionParser.get_usage">
  1535. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">get_usage</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.get_usage" title="Link to this definition">¶</a></dt>
  1536. <dd><p>Same as <a class="reference internal" href="#optparse.OptionParser.print_usage" title="optparse.OptionParser.print_usage"><code class="xref py py-meth docutils literal notranslate"><span class="pre">print_usage()</span></code></a> but returns the usage string instead of
  1537. printing it.</p>
  1538. </dd></dl>
  1539. <dl class="py method">
  1540. <dt class="sig sig-object py" id="optparse.OptionParser.set_defaults">
  1541. <span class="sig-prename descclassname"><span class="pre">OptionParser.</span></span><span class="sig-name descname"><span class="pre">set_defaults</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dest=value</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#optparse.OptionParser.set_defaults" title="Link to this definition">¶</a></dt>
  1542. <dd><p>Set default values for several option destinations at once. Using
  1543. <a class="reference internal" href="#optparse.OptionParser.set_defaults" title="optparse.OptionParser.set_defaults"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_defaults()</span></code></a> is the preferred way to set default values for options,
  1544. since multiple options can share the same destination. For example, if
  1545. several “mode” options all set the same destination, any one of them can set
  1546. the default, and the last one wins:</p>
  1547. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--advanced&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_const&quot;</span><span class="p">,</span>
  1548. <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;mode&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="s2">&quot;advanced&quot;</span><span class="p">,</span>
  1549. <span class="n">default</span><span class="o">=</span><span class="s2">&quot;novice&quot;</span><span class="p">)</span> <span class="c1"># overridden below</span>
  1550. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--novice&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_const&quot;</span><span class="p">,</span>
  1551. <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;mode&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="s2">&quot;novice&quot;</span><span class="p">,</span>
  1552. <span class="n">default</span><span class="o">=</span><span class="s2">&quot;advanced&quot;</span><span class="p">)</span> <span class="c1"># overrides above setting</span>
  1553. </pre></div>
  1554. </div>
  1555. <p>To avoid this confusion, use <a class="reference internal" href="#optparse.OptionParser.set_defaults" title="optparse.OptionParser.set_defaults"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_defaults()</span></code></a>:</p>
  1556. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">set_defaults</span><span class="p">(</span><span class="n">mode</span><span class="o">=</span><span class="s2">&quot;advanced&quot;</span><span class="p">)</span>
  1557. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--advanced&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_const&quot;</span><span class="p">,</span>
  1558. <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;mode&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="s2">&quot;advanced&quot;</span><span class="p">)</span>
  1559. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--novice&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_const&quot;</span><span class="p">,</span>
  1560. <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;mode&quot;</span><span class="p">,</span> <span class="n">const</span><span class="o">=</span><span class="s2">&quot;novice&quot;</span><span class="p">)</span>
  1561. </pre></div>
  1562. </div>
  1563. </dd></dl>
  1564. </section>
  1565. </section>
  1566. <section id="option-callbacks">
  1567. <span id="optparse-option-callbacks"></span><h2>Option Callbacks<a class="headerlink" href="#option-callbacks" title="Link to this heading">¶</a></h2>
  1568. <p>When <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>’s built-in actions and types aren’t quite enough for your
  1569. needs, you have two choices: extend <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> or define a callback option.
  1570. Extending <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> is more general, but overkill for a lot of simple
  1571. cases. Quite often a simple callback is all you need.</p>
  1572. <p>There are two steps to defining a callback option:</p>
  1573. <ul class="simple">
  1574. <li><p>define the option itself using the <code class="docutils literal notranslate"><span class="pre">&quot;callback&quot;</span></code> action</p></li>
  1575. <li><p>write the callback; this is a function (or method) that takes at least four
  1576. arguments, as described below</p></li>
  1577. </ul>
  1578. <section id="defining-a-callback-option">
  1579. <span id="optparse-defining-callback-option"></span><h3>Defining a callback option<a class="headerlink" href="#defining-a-callback-option" title="Link to this heading">¶</a></h3>
  1580. <p>As always, the easiest way to define a callback option is by using the
  1581. <a class="reference internal" href="#optparse.OptionParser.add_option" title="optparse.OptionParser.add_option"><code class="xref py py-meth docutils literal notranslate"><span class="pre">OptionParser.add_option()</span></code></a> method. Apart from <a class="reference internal" href="#optparse.Option.action" title="optparse.Option.action"><code class="xref py py-attr docutils literal notranslate"><span class="pre">action</span></code></a>, the
  1582. only option attribute you must specify is <code class="docutils literal notranslate"><span class="pre">callback</span></code>, the function to call:</p>
  1583. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-c&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">my_callback</span><span class="p">)</span>
  1584. </pre></div>
  1585. </div>
  1586. <p><code class="docutils literal notranslate"><span class="pre">callback</span></code> is a function (or other callable object), so you must have already
  1587. defined <code class="docutils literal notranslate"><span class="pre">my_callback()</span></code> when you create this callback option. In this simple
  1588. case, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> doesn’t even know if <code class="docutils literal notranslate"><span class="pre">-c</span></code> takes any arguments,
  1589. which usually means that the option takes no arguments—the mere presence of
  1590. <code class="docutils literal notranslate"><span class="pre">-c</span></code> on the command-line is all it needs to know. In some
  1591. circumstances, though, you might want your callback to consume an arbitrary
  1592. number of command-line arguments. This is where writing callbacks gets tricky;
  1593. it’s covered later in this section.</p>
  1594. <p><a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> always passes four particular arguments to your callback, and it
  1595. will only pass additional arguments if you specify them via
  1596. <a class="reference internal" href="#optparse.Option.callback_args" title="optparse.Option.callback_args"><code class="xref py py-attr docutils literal notranslate"><span class="pre">callback_args</span></code></a> and <a class="reference internal" href="#optparse.Option.callback_kwargs" title="optparse.Option.callback_kwargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">callback_kwargs</span></code></a>. Thus, the
  1597. minimal callback function signature is:</p>
  1598. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">my_callback</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
  1599. </pre></div>
  1600. </div>
  1601. <p>The four arguments to a callback are described below.</p>
  1602. <p>There are several other option attributes that you can supply when you define a
  1603. callback option:</p>
  1604. <dl class="simple">
  1605. <dt><a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a></dt><dd><p>has its usual meaning: as with the <code class="docutils literal notranslate"><span class="pre">&quot;store&quot;</span></code> or <code class="docutils literal notranslate"><span class="pre">&quot;append&quot;</span></code> actions, it
  1606. instructs <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> to consume one argument and convert it to
  1607. <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>. Rather than storing the converted value(s) anywhere,
  1608. though, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> passes it to your callback function.</p>
  1609. </dd>
  1610. <dt><a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">nargs</span></code></a></dt><dd><p>also has its usual meaning: if it is supplied and &gt; 1, <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> will
  1611. consume <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">nargs</span></code></a> arguments, each of which must be convertible to
  1612. <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>. It then passes a tuple of converted values to your
  1613. callback.</p>
  1614. </dd>
  1615. <dt><a class="reference internal" href="#optparse.Option.callback_args" title="optparse.Option.callback_args"><code class="xref py py-attr docutils literal notranslate"><span class="pre">callback_args</span></code></a></dt><dd><p>a tuple of extra positional arguments to pass to the callback</p>
  1616. </dd>
  1617. <dt><a class="reference internal" href="#optparse.Option.callback_kwargs" title="optparse.Option.callback_kwargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">callback_kwargs</span></code></a></dt><dd><p>a dictionary of extra keyword arguments to pass to the callback</p>
  1618. </dd>
  1619. </dl>
  1620. </section>
  1621. <section id="how-callbacks-are-called">
  1622. <span id="optparse-how-callbacks-called"></span><h3>How callbacks are called<a class="headerlink" href="#how-callbacks-are-called" title="Link to this heading">¶</a></h3>
  1623. <p>All callbacks are called as follows:</p>
  1624. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">func</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
  1625. </pre></div>
  1626. </div>
  1627. <p>where</p>
  1628. <dl>
  1629. <dt><code class="docutils literal notranslate"><span class="pre">option</span></code></dt><dd><p>is the Option instance that’s calling the callback</p>
  1630. </dd>
  1631. <dt><code class="docutils literal notranslate"><span class="pre">opt_str</span></code></dt><dd><p>is the option string seen on the command-line that’s triggering the callback.
  1632. (If an abbreviated long option was used, <code class="docutils literal notranslate"><span class="pre">opt_str</span></code> will be the full,
  1633. canonical option string—e.g. if the user puts <code class="docutils literal notranslate"><span class="pre">--foo</span></code> on the
  1634. command-line as an abbreviation for <code class="docutils literal notranslate"><span class="pre">--foobar</span></code>, then <code class="docutils literal notranslate"><span class="pre">opt_str</span></code> will be
  1635. <code class="docutils literal notranslate"><span class="pre">&quot;--foobar&quot;</span></code>.)</p>
  1636. </dd>
  1637. <dt><code class="docutils literal notranslate"><span class="pre">value</span></code></dt><dd><p>is the argument to this option seen on the command-line. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> will
  1638. only expect an argument if <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a> is set; the type of <code class="docutils literal notranslate"><span class="pre">value</span></code> will be
  1639. the type implied by the option’s type. If <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a> for this option is
  1640. <code class="docutils literal notranslate"><span class="pre">None</span></code> (no argument expected), then <code class="docutils literal notranslate"><span class="pre">value</span></code> will be <code class="docutils literal notranslate"><span class="pre">None</span></code>. If <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">nargs</span></code></a>
  1641. &gt; 1, <code class="docutils literal notranslate"><span class="pre">value</span></code> will be a tuple of values of the appropriate type.</p>
  1642. </dd>
  1643. <dt><code class="docutils literal notranslate"><span class="pre">parser</span></code></dt><dd><p>is the OptionParser instance driving the whole thing, mainly useful because
  1644. you can access some other interesting data through its instance attributes:</p>
  1645. <dl class="simple">
  1646. <dt><code class="docutils literal notranslate"><span class="pre">parser.largs</span></code></dt><dd><p>the current list of leftover arguments, ie. arguments that have been
  1647. consumed but are neither options nor option arguments. Feel free to modify
  1648. <code class="docutils literal notranslate"><span class="pre">parser.largs</span></code>, e.g. by adding more arguments to it. (This list will
  1649. become <code class="docutils literal notranslate"><span class="pre">args</span></code>, the second return value of <a class="reference internal" href="#optparse.OptionParser.parse_args" title="optparse.OptionParser.parse_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse_args()</span></code></a>.)</p>
  1650. </dd>
  1651. <dt><code class="docutils literal notranslate"><span class="pre">parser.rargs</span></code></dt><dd><p>the current list of remaining arguments, ie. with <code class="docutils literal notranslate"><span class="pre">opt_str</span></code> and
  1652. <code class="docutils literal notranslate"><span class="pre">value</span></code> (if applicable) removed, and only the arguments following them
  1653. still there. Feel free to modify <code class="docutils literal notranslate"><span class="pre">parser.rargs</span></code>, e.g. by consuming more
  1654. arguments.</p>
  1655. </dd>
  1656. <dt><code class="docutils literal notranslate"><span class="pre">parser.values</span></code></dt><dd><p>the object where option values are by default stored (an instance of
  1657. optparse.OptionValues). This lets callbacks use the same mechanism as the
  1658. rest of <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> for storing option values; you don’t need to mess
  1659. around with globals or closures. You can also access or modify the
  1660. value(s) of any options already encountered on the command-line.</p>
  1661. </dd>
  1662. </dl>
  1663. </dd>
  1664. <dt><code class="docutils literal notranslate"><span class="pre">args</span></code></dt><dd><p>is a tuple of arbitrary positional arguments supplied via the
  1665. <a class="reference internal" href="#optparse.Option.callback_args" title="optparse.Option.callback_args"><code class="xref py py-attr docutils literal notranslate"><span class="pre">callback_args</span></code></a> option attribute.</p>
  1666. </dd>
  1667. <dt><code class="docutils literal notranslate"><span class="pre">kwargs</span></code></dt><dd><p>is a dictionary of arbitrary keyword arguments supplied via
  1668. <a class="reference internal" href="#optparse.Option.callback_kwargs" title="optparse.Option.callback_kwargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">callback_kwargs</span></code></a>.</p>
  1669. </dd>
  1670. </dl>
  1671. </section>
  1672. <section id="raising-errors-in-a-callback">
  1673. <span id="optparse-raising-errors-in-callback"></span><h3>Raising errors in a callback<a class="headerlink" href="#raising-errors-in-a-callback" title="Link to this heading">¶</a></h3>
  1674. <p>The callback function should raise <a class="reference internal" href="#optparse.OptionValueError" title="optparse.OptionValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OptionValueError</span></code></a> if there are any
  1675. problems with the option or its argument(s). <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> catches this and
  1676. terminates the program, printing the error message you supply to stderr. Your
  1677. message should be clear, concise, accurate, and mention the option at fault.
  1678. Otherwise, the user will have a hard time figuring out what they did wrong.</p>
  1679. </section>
  1680. <section id="callback-example-1-trivial-callback">
  1681. <span id="optparse-callback-example-1"></span><h3>Callback example 1: trivial callback<a class="headerlink" href="#callback-example-1-trivial-callback" title="Link to this heading">¶</a></h3>
  1682. <p>Here’s an example of a callback option that takes no arguments, and simply
  1683. records that the option was seen:</p>
  1684. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">record_foo_seen</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
  1685. <span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">saw_foo</span> <span class="o">=</span> <span class="kc">True</span>
  1686. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--foo&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">record_foo_seen</span><span class="p">)</span>
  1687. </pre></div>
  1688. </div>
  1689. <p>Of course, you could do that with the <code class="docutils literal notranslate"><span class="pre">&quot;store_true&quot;</span></code> action.</p>
  1690. </section>
  1691. <section id="callback-example-2-check-option-order">
  1692. <span id="optparse-callback-example-2"></span><h3>Callback example 2: check option order<a class="headerlink" href="#callback-example-2-check-option-order" title="Link to this heading">¶</a></h3>
  1693. <p>Here’s a slightly more interesting example: record the fact that <code class="docutils literal notranslate"><span class="pre">-a</span></code> is
  1694. seen, but blow up if it comes after <code class="docutils literal notranslate"><span class="pre">-b</span></code> in the command-line.</p>
  1695. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">check_order</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
  1696. <span class="k">if</span> <span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">b</span><span class="p">:</span>
  1697. <span class="k">raise</span> <span class="n">OptionValueError</span><span class="p">(</span><span class="s2">&quot;can&#39;t use -a after -b&quot;</span><span class="p">)</span>
  1698. <span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
  1699. <span class="o">...</span>
  1700. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-a&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">check_order</span><span class="p">)</span>
  1701. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-b&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;b&quot;</span><span class="p">)</span>
  1702. </pre></div>
  1703. </div>
  1704. </section>
  1705. <section id="callback-example-3-check-option-order-generalized">
  1706. <span id="optparse-callback-example-3"></span><h3>Callback example 3: check option order (generalized)<a class="headerlink" href="#callback-example-3-check-option-order-generalized" title="Link to this heading">¶</a></h3>
  1707. <p>If you want to re-use this callback for several similar options (set a flag, but
  1708. blow up if <code class="docutils literal notranslate"><span class="pre">-b</span></code> has already been seen), it needs a bit of work: the error
  1709. message and the flag that it sets must be generalized.</p>
  1710. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">check_order</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
  1711. <span class="k">if</span> <span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">b</span><span class="p">:</span>
  1712. <span class="k">raise</span> <span class="n">OptionValueError</span><span class="p">(</span><span class="s2">&quot;can&#39;t use </span><span class="si">%s</span><span class="s2"> after -b&quot;</span> <span class="o">%</span> <span class="n">opt_str</span><span class="p">)</span>
  1713. <span class="nb">setattr</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">option</span><span class="o">.</span><span class="n">dest</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
  1714. <span class="o">...</span>
  1715. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-a&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">check_order</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;a&#39;</span><span class="p">)</span>
  1716. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-b&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;b&quot;</span><span class="p">)</span>
  1717. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-c&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">check_order</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;c&#39;</span><span class="p">)</span>
  1718. </pre></div>
  1719. </div>
  1720. </section>
  1721. <section id="callback-example-4-check-arbitrary-condition">
  1722. <span id="optparse-callback-example-4"></span><h3>Callback example 4: check arbitrary condition<a class="headerlink" href="#callback-example-4-check-arbitrary-condition" title="Link to this heading">¶</a></h3>
  1723. <p>Of course, you could put any condition in there—you’re not limited to checking
  1724. the values of already-defined options. For example, if you have options that
  1725. should not be called when the moon is full, all you have to do is this:</p>
  1726. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">check_moon</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
  1727. <span class="k">if</span> <span class="n">is_moon_full</span><span class="p">():</span>
  1728. <span class="k">raise</span> <span class="n">OptionValueError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> option invalid when moon is full&quot;</span>
  1729. <span class="o">%</span> <span class="n">opt_str</span><span class="p">)</span>
  1730. <span class="nb">setattr</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">option</span><span class="o">.</span><span class="n">dest</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
  1731. <span class="o">...</span>
  1732. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--foo&quot;</span><span class="p">,</span>
  1733. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">check_moon</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;foo&quot;</span><span class="p">)</span>
  1734. </pre></div>
  1735. </div>
  1736. <p>(The definition of <code class="docutils literal notranslate"><span class="pre">is_moon_full()</span></code> is left as an exercise for the reader.)</p>
  1737. </section>
  1738. <section id="callback-example-5-fixed-arguments">
  1739. <span id="optparse-callback-example-5"></span><h3>Callback example 5: fixed arguments<a class="headerlink" href="#callback-example-5-fixed-arguments" title="Link to this heading">¶</a></h3>
  1740. <p>Things get slightly more interesting when you define callback options that take
  1741. a fixed number of arguments. Specifying that a callback option takes arguments
  1742. is similar to defining a <code class="docutils literal notranslate"><span class="pre">&quot;store&quot;</span></code> or <code class="docutils literal notranslate"><span class="pre">&quot;append&quot;</span></code> option: if you define
  1743. <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a>, then the option takes one argument that must be
  1744. convertible to that type; if you further define <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">nargs</span></code></a>, then the
  1745. option takes <a class="reference internal" href="#optparse.Option.nargs" title="optparse.Option.nargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">nargs</span></code></a> arguments.</p>
  1746. <p>Here’s an example that just emulates the standard <code class="docutils literal notranslate"><span class="pre">&quot;store&quot;</span></code> action:</p>
  1747. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">store_value</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
  1748. <span class="nb">setattr</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">option</span><span class="o">.</span><span class="n">dest</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
  1749. <span class="o">...</span>
  1750. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;--foo&quot;</span><span class="p">,</span>
  1751. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">store_value</span><span class="p">,</span>
  1752. <span class="nb">type</span><span class="o">=</span><span class="s2">&quot;int&quot;</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;foo&quot;</span><span class="p">)</span>
  1753. </pre></div>
  1754. </div>
  1755. <p>Note that <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> takes care of consuming 3 arguments and converting
  1756. them to integers for you; all you have to do is store them. (Or whatever;
  1757. obviously you don’t need a callback for this example.)</p>
  1758. </section>
  1759. <section id="callback-example-6-variable-arguments">
  1760. <span id="optparse-callback-example-6"></span><h3>Callback example 6: variable arguments<a class="headerlink" href="#callback-example-6-variable-arguments" title="Link to this heading">¶</a></h3>
  1761. <p>Things get hairy when you want an option to take a variable number of arguments.
  1762. For this case, you must write a callback, as <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> doesn’t provide any
  1763. built-in capabilities for it. And you have to deal with certain intricacies of
  1764. conventional Unix command-line parsing that <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> normally handles for
  1765. you. In particular, callbacks should implement the conventional rules for bare
  1766. <code class="docutils literal notranslate"><span class="pre">--</span></code> and <code class="docutils literal notranslate"><span class="pre">-</span></code> arguments:</p>
  1767. <ul class="simple">
  1768. <li><p>either <code class="docutils literal notranslate"><span class="pre">--</span></code> or <code class="docutils literal notranslate"><span class="pre">-</span></code> can be option arguments</p></li>
  1769. <li><p>bare <code class="docutils literal notranslate"><span class="pre">--</span></code> (if not the argument to some option): halt command-line
  1770. processing and discard the <code class="docutils literal notranslate"><span class="pre">--</span></code></p></li>
  1771. <li><p>bare <code class="docutils literal notranslate"><span class="pre">-</span></code> (if not the argument to some option): halt command-line
  1772. processing but keep the <code class="docutils literal notranslate"><span class="pre">-</span></code> (append it to <code class="docutils literal notranslate"><span class="pre">parser.largs</span></code>)</p></li>
  1773. </ul>
  1774. <p>If you want an option that takes a variable number of arguments, there are
  1775. several subtle, tricky issues to worry about. The exact implementation you
  1776. choose will be based on which trade-offs you’re willing to make for your
  1777. application (which is why <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> doesn’t support this sort of thing
  1778. directly).</p>
  1779. <p>Nevertheless, here’s a stab at a callback for an option with variable
  1780. arguments:</p>
  1781. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">vararg_callback</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
  1782. <span class="k">assert</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span>
  1783. <span class="n">value</span> <span class="o">=</span> <span class="p">[]</span>
  1784. <span class="k">def</span> <span class="nf">floatable</span><span class="p">(</span><span class="nb">str</span><span class="p">):</span>
  1785. <span class="k">try</span><span class="p">:</span>
  1786. <span class="nb">float</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>
  1787. <span class="k">return</span> <span class="kc">True</span>
  1788. <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
  1789. <span class="k">return</span> <span class="kc">False</span>
  1790. <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">parser</span><span class="o">.</span><span class="n">rargs</span><span class="p">:</span>
  1791. <span class="c1"># stop on --foo like options</span>
  1792. <span class="k">if</span> <span class="n">arg</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;--&quot;</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
  1793. <span class="k">break</span>
  1794. <span class="c1"># stop on -a, but not on -3 or -3.0</span>
  1795. <span class="k">if</span> <span class="n">arg</span><span class="p">[:</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;-&quot;</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">floatable</span><span class="p">(</span><span class="n">arg</span><span class="p">):</span>
  1796. <span class="k">break</span>
  1797. <span class="n">value</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
  1798. <span class="k">del</span> <span class="n">parser</span><span class="o">.</span><span class="n">rargs</span><span class="p">[:</span><span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="p">)]</span>
  1799. <span class="nb">setattr</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">option</span><span class="o">.</span><span class="n">dest</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
  1800. <span class="o">...</span>
  1801. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-c&quot;</span><span class="p">,</span> <span class="s2">&quot;--callback&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;vararg_attr&quot;</span><span class="p">,</span>
  1802. <span class="n">action</span><span class="o">=</span><span class="s2">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">vararg_callback</span><span class="p">)</span>
  1803. </pre></div>
  1804. </div>
  1805. </section>
  1806. </section>
  1807. <section id="extending-optparse">
  1808. <span id="optparse-extending-optparse"></span><h2>Extending <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a><a class="headerlink" href="#extending-optparse" title="Link to this heading">¶</a></h2>
  1809. <p>Since the two major controlling factors in how <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> interprets
  1810. command-line options are the action and type of each option, the most likely
  1811. direction of extension is to add new actions and new types.</p>
  1812. <section id="adding-new-types">
  1813. <span id="optparse-adding-new-types"></span><h3>Adding new types<a class="headerlink" href="#adding-new-types" title="Link to this heading">¶</a></h3>
  1814. <p>To add new types, you need to define your own subclass of <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>’s
  1815. <a class="reference internal" href="#optparse.Option" title="optparse.Option"><code class="xref py py-class docutils literal notranslate"><span class="pre">Option</span></code></a> class. This class has a couple of attributes that define
  1816. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>’s types: <a class="reference internal" href="#optparse.Option.TYPES" title="optparse.Option.TYPES"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TYPES</span></code></a> and <a class="reference internal" href="#optparse.Option.TYPE_CHECKER" title="optparse.Option.TYPE_CHECKER"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TYPE_CHECKER</span></code></a>.</p>
  1817. <dl class="py attribute">
  1818. <dt class="sig sig-object py" id="optparse.Option.TYPES">
  1819. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">TYPES</span></span><a class="headerlink" href="#optparse.Option.TYPES" title="Link to this definition">¶</a></dt>
  1820. <dd><p>A tuple of type names; in your subclass, simply define a new tuple
  1821. <a class="reference internal" href="#optparse.Option.TYPES" title="optparse.Option.TYPES"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TYPES</span></code></a> that builds on the standard one.</p>
  1822. </dd></dl>
  1823. <dl class="py attribute">
  1824. <dt class="sig sig-object py" id="optparse.Option.TYPE_CHECKER">
  1825. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">TYPE_CHECKER</span></span><a class="headerlink" href="#optparse.Option.TYPE_CHECKER" title="Link to this definition">¶</a></dt>
  1826. <dd><p>A dictionary mapping type names to type-checking functions. A type-checking
  1827. function has the following signature:</p>
  1828. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">check_mytype</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
  1829. </pre></div>
  1830. </div>
  1831. <p>where <code class="docutils literal notranslate"><span class="pre">option</span></code> is an <a class="reference internal" href="#optparse.Option" title="optparse.Option"><code class="xref py py-class docutils literal notranslate"><span class="pre">Option</span></code></a> instance, <code class="docutils literal notranslate"><span class="pre">opt</span></code> is an option string
  1832. (e.g., <code class="docutils literal notranslate"><span class="pre">-f</span></code>), and <code class="docutils literal notranslate"><span class="pre">value</span></code> is the string from the command line that must
  1833. be checked and converted to your desired type. <code class="docutils literal notranslate"><span class="pre">check_mytype()</span></code> should
  1834. return an object of the hypothetical type <code class="docutils literal notranslate"><span class="pre">mytype</span></code>. The value returned by
  1835. a type-checking function will wind up in the OptionValues instance returned
  1836. by <a class="reference internal" href="#optparse.OptionParser.parse_args" title="optparse.OptionParser.parse_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">OptionParser.parse_args()</span></code></a>, or be passed to a callback as the
  1837. <code class="docutils literal notranslate"><span class="pre">value</span></code> parameter.</p>
  1838. <p>Your type-checking function should raise <a class="reference internal" href="#optparse.OptionValueError" title="optparse.OptionValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OptionValueError</span></code></a> if it
  1839. encounters any problems. <a class="reference internal" href="#optparse.OptionValueError" title="optparse.OptionValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OptionValueError</span></code></a> takes a single string
  1840. argument, which is passed as-is to <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionParser</span></code></a>’s <code class="xref py py-meth docutils literal notranslate"><span class="pre">error()</span></code>
  1841. method, which in turn prepends the program name and the string <code class="docutils literal notranslate"><span class="pre">&quot;error:&quot;</span></code>
  1842. and prints everything to stderr before terminating the process.</p>
  1843. </dd></dl>
  1844. <p>Here’s a silly example that demonstrates adding a <code class="docutils literal notranslate"><span class="pre">&quot;complex&quot;</span></code> option type to
  1845. parse Python-style complex numbers on the command line. (This is even sillier
  1846. than it used to be, because <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> 1.3 added built-in support for
  1847. complex numbers, but never mind.)</p>
  1848. <p>First, the necessary imports:</p>
  1849. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">copy</span> <span class="kn">import</span> <span class="n">copy</span>
  1850. <span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">Option</span><span class="p">,</span> <span class="n">OptionValueError</span>
  1851. </pre></div>
  1852. </div>
  1853. <p>You need to define your type-checker first, since it’s referred to later (in the
  1854. <a class="reference internal" href="#optparse.Option.TYPE_CHECKER" title="optparse.Option.TYPE_CHECKER"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TYPE_CHECKER</span></code></a> class attribute of your Option subclass):</p>
  1855. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">check_complex</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
  1856. <span class="k">try</span><span class="p">:</span>
  1857. <span class="k">return</span> <span class="nb">complex</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
  1858. <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
  1859. <span class="k">raise</span> <span class="n">OptionValueError</span><span class="p">(</span>
  1860. <span class="s2">&quot;option </span><span class="si">%s</span><span class="s2">: invalid complex value: </span><span class="si">%r</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
  1861. </pre></div>
  1862. </div>
  1863. <p>Finally, the Option subclass:</p>
  1864. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyOption</span> <span class="p">(</span><span class="n">Option</span><span class="p">):</span>
  1865. <span class="n">TYPES</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">TYPES</span> <span class="o">+</span> <span class="p">(</span><span class="s2">&quot;complex&quot;</span><span class="p">,)</span>
  1866. <span class="n">TYPE_CHECKER</span> <span class="o">=</span> <span class="n">copy</span><span class="p">(</span><span class="n">Option</span><span class="o">.</span><span class="n">TYPE_CHECKER</span><span class="p">)</span>
  1867. <span class="n">TYPE_CHECKER</span><span class="p">[</span><span class="s2">&quot;complex&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">check_complex</span>
  1868. </pre></div>
  1869. </div>
  1870. <p>(If we didn’t make a <a class="reference internal" href="copy.html#module-copy" title="copy: Shallow and deep copy operations."><code class="xref py py-func docutils literal notranslate"><span class="pre">copy()</span></code></a> of <a class="reference internal" href="#optparse.Option.TYPE_CHECKER" title="optparse.Option.TYPE_CHECKER"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Option.TYPE_CHECKER</span></code></a>, we would end
  1871. up modifying the <a class="reference internal" href="#optparse.Option.TYPE_CHECKER" title="optparse.Option.TYPE_CHECKER"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TYPE_CHECKER</span></code></a> attribute of <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>’s
  1872. Option class. This being Python, nothing stops you from doing that except good
  1873. manners and common sense.)</p>
  1874. <p>That’s it! Now you can write a script that uses the new option type just like
  1875. any other <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>-based script, except you have to instruct your
  1876. OptionParser to use MyOption instead of Option:</p>
  1877. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">option_class</span><span class="o">=</span><span class="n">MyOption</span><span class="p">)</span>
  1878. <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s2">&quot;-c&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s2">&quot;complex&quot;</span><span class="p">)</span>
  1879. </pre></div>
  1880. </div>
  1881. <p>Alternately, you can build your own option list and pass it to OptionParser; if
  1882. you don’t use <code class="xref py py-meth docutils literal notranslate"><span class="pre">add_option()</span></code> in the above way, you don’t need to tell
  1883. OptionParser which option class to use:</p>
  1884. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">option_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">MyOption</span><span class="p">(</span><span class="s2">&quot;-c&quot;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s2">&quot;store&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s2">&quot;complex&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s2">&quot;c&quot;</span><span class="p">)]</span>
  1885. <span class="n">parser</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">(</span><span class="n">option_list</span><span class="o">=</span><span class="n">option_list</span><span class="p">)</span>
  1886. </pre></div>
  1887. </div>
  1888. </section>
  1889. <section id="adding-new-actions">
  1890. <span id="optparse-adding-new-actions"></span><h3>Adding new actions<a class="headerlink" href="#adding-new-actions" title="Link to this heading">¶</a></h3>
  1891. <p>Adding new actions is a bit trickier, because you have to understand that
  1892. <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> has a couple of classifications for actions:</p>
  1893. <dl class="simple">
  1894. <dt>“store” actions</dt><dd><p>actions that result in <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> storing a value to an attribute of the
  1895. current OptionValues instance; these options require a <a class="reference internal" href="#optparse.Option.dest" title="optparse.Option.dest"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dest</span></code></a>
  1896. attribute to be supplied to the Option constructor.</p>
  1897. </dd>
  1898. <dt>“typed” actions</dt><dd><p>actions that take a value from the command line and expect it to be of a
  1899. certain type; or rather, a string that can be converted to a certain type.
  1900. These options require a <a class="reference internal" href="#optparse.Option.type" title="optparse.Option.type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code></a> attribute to the Option
  1901. constructor.</p>
  1902. </dd>
  1903. </dl>
  1904. <p>These are overlapping sets: some default “store” actions are <code class="docutils literal notranslate"><span class="pre">&quot;store&quot;</span></code>,
  1905. <code class="docutils literal notranslate"><span class="pre">&quot;store_const&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;append&quot;</span></code>, and <code class="docutils literal notranslate"><span class="pre">&quot;count&quot;</span></code>, while the default “typed”
  1906. actions are <code class="docutils literal notranslate"><span class="pre">&quot;store&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;append&quot;</span></code>, and <code class="docutils literal notranslate"><span class="pre">&quot;callback&quot;</span></code>.</p>
  1907. <p>When you add an action, you need to categorize it by listing it in at least one
  1908. of the following class attributes of Option (all are lists of strings):</p>
  1909. <dl class="py attribute">
  1910. <dt class="sig sig-object py" id="optparse.Option.ACTIONS">
  1911. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">ACTIONS</span></span><a class="headerlink" href="#optparse.Option.ACTIONS" title="Link to this definition">¶</a></dt>
  1912. <dd><p>All actions must be listed in ACTIONS.</p>
  1913. </dd></dl>
  1914. <dl class="py attribute">
  1915. <dt class="sig sig-object py" id="optparse.Option.STORE_ACTIONS">
  1916. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">STORE_ACTIONS</span></span><a class="headerlink" href="#optparse.Option.STORE_ACTIONS" title="Link to this definition">¶</a></dt>
  1917. <dd><p>“store” actions are additionally listed here.</p>
  1918. </dd></dl>
  1919. <dl class="py attribute">
  1920. <dt class="sig sig-object py" id="optparse.Option.TYPED_ACTIONS">
  1921. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">TYPED_ACTIONS</span></span><a class="headerlink" href="#optparse.Option.TYPED_ACTIONS" title="Link to this definition">¶</a></dt>
  1922. <dd><p>“typed” actions are additionally listed here.</p>
  1923. </dd></dl>
  1924. <dl class="py attribute">
  1925. <dt class="sig sig-object py" id="optparse.Option.ALWAYS_TYPED_ACTIONS">
  1926. <span class="sig-prename descclassname"><span class="pre">Option.</span></span><span class="sig-name descname"><span class="pre">ALWAYS_TYPED_ACTIONS</span></span><a class="headerlink" href="#optparse.Option.ALWAYS_TYPED_ACTIONS" title="Link to this definition">¶</a></dt>
  1927. <dd><p>Actions that always take a type (i.e. whose options always take a value) are
  1928. additionally listed here. The only effect of this is that <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>
  1929. assigns the default type, <code class="docutils literal notranslate"><span class="pre">&quot;string&quot;</span></code>, to options with no explicit type
  1930. whose action is listed in <a class="reference internal" href="#optparse.Option.ALWAYS_TYPED_ACTIONS" title="optparse.Option.ALWAYS_TYPED_ACTIONS"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ALWAYS_TYPED_ACTIONS</span></code></a>.</p>
  1931. </dd></dl>
  1932. <p>In order to actually implement your new action, you must override Option’s
  1933. <code class="xref py py-meth docutils literal notranslate"><span class="pre">take_action()</span></code> method and add a case that recognizes your action.</p>
  1934. <p>For example, let’s add an <code class="docutils literal notranslate"><span class="pre">&quot;extend&quot;</span></code> action. This is similar to the standard
  1935. <code class="docutils literal notranslate"><span class="pre">&quot;append&quot;</span></code> action, but instead of taking a single value from the command-line
  1936. and appending it to an existing list, <code class="docutils literal notranslate"><span class="pre">&quot;extend&quot;</span></code> will take multiple values in
  1937. a single comma-delimited string, and extend an existing list with them. That
  1938. is, if <code class="docutils literal notranslate"><span class="pre">--names</span></code> is an <code class="docutils literal notranslate"><span class="pre">&quot;extend&quot;</span></code> option of type <code class="docutils literal notranslate"><span class="pre">&quot;string&quot;</span></code>, the command
  1939. line</p>
  1940. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">--</span><span class="n">names</span><span class="o">=</span><span class="n">foo</span><span class="p">,</span><span class="n">bar</span> <span class="o">--</span><span class="n">names</span> <span class="n">blah</span> <span class="o">--</span><span class="n">names</span> <span class="n">ding</span><span class="p">,</span><span class="n">dong</span>
  1941. </pre></div>
  1942. </div>
  1943. <p>would result in a list</p>
  1944. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="s2">&quot;foo&quot;</span><span class="p">,</span> <span class="s2">&quot;bar&quot;</span><span class="p">,</span> <span class="s2">&quot;blah&quot;</span><span class="p">,</span> <span class="s2">&quot;ding&quot;</span><span class="p">,</span> <span class="s2">&quot;dong&quot;</span><span class="p">]</span>
  1945. </pre></div>
  1946. </div>
  1947. <p>Again we define a subclass of Option:</p>
  1948. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyOption</span><span class="p">(</span><span class="n">Option</span><span class="p">):</span>
  1949. <span class="n">ACTIONS</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">ACTIONS</span> <span class="o">+</span> <span class="p">(</span><span class="s2">&quot;extend&quot;</span><span class="p">,)</span>
  1950. <span class="n">STORE_ACTIONS</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">STORE_ACTIONS</span> <span class="o">+</span> <span class="p">(</span><span class="s2">&quot;extend&quot;</span><span class="p">,)</span>
  1951. <span class="n">TYPED_ACTIONS</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">TYPED_ACTIONS</span> <span class="o">+</span> <span class="p">(</span><span class="s2">&quot;extend&quot;</span><span class="p">,)</span>
  1952. <span class="n">ALWAYS_TYPED_ACTIONS</span> <span class="o">=</span> <span class="n">Option</span><span class="o">.</span><span class="n">ALWAYS_TYPED_ACTIONS</span> <span class="o">+</span> <span class="p">(</span><span class="s2">&quot;extend&quot;</span><span class="p">,)</span>
  1953. <span class="k">def</span> <span class="nf">take_action</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
  1954. <span class="k">if</span> <span class="n">action</span> <span class="o">==</span> <span class="s2">&quot;extend&quot;</span><span class="p">:</span>
  1955. <span class="n">lvalue</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;,&quot;</span><span class="p">)</span>
  1956. <span class="n">values</span><span class="o">.</span><span class="n">ensure_value</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="p">[])</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">lvalue</span><span class="p">)</span>
  1957. <span class="k">else</span><span class="p">:</span>
  1958. <span class="n">Option</span><span class="o">.</span><span class="n">take_action</span><span class="p">(</span>
  1959. <span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">opt</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">values</span><span class="p">,</span> <span class="n">parser</span><span class="p">)</span>
  1960. </pre></div>
  1961. </div>
  1962. <p>Features of note:</p>
  1963. <ul>
  1964. <li><p><code class="docutils literal notranslate"><span class="pre">&quot;extend&quot;</span></code> both expects a value on the command-line and stores that value
  1965. somewhere, so it goes in both <a class="reference internal" href="#optparse.Option.STORE_ACTIONS" title="optparse.Option.STORE_ACTIONS"><code class="xref py py-attr docutils literal notranslate"><span class="pre">STORE_ACTIONS</span></code></a> and
  1966. <a class="reference internal" href="#optparse.Option.TYPED_ACTIONS" title="optparse.Option.TYPED_ACTIONS"><code class="xref py py-attr docutils literal notranslate"><span class="pre">TYPED_ACTIONS</span></code></a>.</p></li>
  1967. <li><p>to ensure that <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> assigns the default type of <code class="docutils literal notranslate"><span class="pre">&quot;string&quot;</span></code> to
  1968. <code class="docutils literal notranslate"><span class="pre">&quot;extend&quot;</span></code> actions, we put the <code class="docutils literal notranslate"><span class="pre">&quot;extend&quot;</span></code> action in
  1969. <a class="reference internal" href="#optparse.Option.ALWAYS_TYPED_ACTIONS" title="optparse.Option.ALWAYS_TYPED_ACTIONS"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ALWAYS_TYPED_ACTIONS</span></code></a> as well.</p></li>
  1970. <li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">MyOption.take_action()</span></code> implements just this one new action, and passes
  1971. control back to <code class="xref py py-meth docutils literal notranslate"><span class="pre">Option.take_action()</span></code> for the standard <a class="reference internal" href="#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>
  1972. actions.</p></li>
  1973. <li><p><code class="docutils literal notranslate"><span class="pre">values</span></code> is an instance of the optparse_parser.Values class, which provides
  1974. the very useful <code class="xref py py-meth docutils literal notranslate"><span class="pre">ensure_value()</span></code> method. <code class="xref py py-meth docutils literal notranslate"><span class="pre">ensure_value()</span></code> is
  1975. essentially <a class="reference internal" href="functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a> with a safety valve; it is called as</p>
  1976. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">values</span><span class="o">.</span><span class="n">ensure_value</span><span class="p">(</span><span class="n">attr</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
  1977. </pre></div>
  1978. </div>
  1979. <p>If the <code class="docutils literal notranslate"><span class="pre">attr</span></code> attribute of <code class="docutils literal notranslate"><span class="pre">values</span></code> doesn’t exist or is <code class="docutils literal notranslate"><span class="pre">None</span></code>, then
  1980. ensure_value() first sets it to <code class="docutils literal notranslate"><span class="pre">value</span></code>, and then returns <code class="docutils literal notranslate"><span class="pre">value</span></code>. This is
  1981. very handy for actions like <code class="docutils literal notranslate"><span class="pre">&quot;extend&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;append&quot;</span></code>, and <code class="docutils literal notranslate"><span class="pre">&quot;count&quot;</span></code>, all
  1982. of which accumulate data in a variable and expect that variable to be of a
  1983. certain type (a list for the first two, an integer for the latter). Using
  1984. <code class="xref py py-meth docutils literal notranslate"><span class="pre">ensure_value()</span></code> means that scripts using your action don’t have to worry
  1985. about setting a default value for the option destinations in question; they
  1986. can just leave the default as <code class="docutils literal notranslate"><span class="pre">None</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">ensure_value()</span></code> will take care of
  1987. getting it right when it’s needed.</p>
  1988. </li>
  1989. </ul>
  1990. </section>
  1991. </section>
  1992. <section id="exceptions">
  1993. <h2>Exceptions<a class="headerlink" href="#exceptions" title="Link to this heading">¶</a></h2>
  1994. <dl class="py exception">
  1995. <dt class="sig sig-object py" id="optparse.OptionError">
  1996. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">optparse.</span></span><span class="sig-name descname"><span class="pre">OptionError</span></span><a class="headerlink" href="#optparse.OptionError" title="Link to this definition">¶</a></dt>
  1997. <dd><p>Raised if an <a class="reference internal" href="#optparse.Option" title="optparse.Option"><code class="xref py py-class docutils literal notranslate"><span class="pre">Option</span></code></a> instance is created with invalid or
  1998. inconsistent arguments.</p>
  1999. </dd></dl>
  2000. <dl class="py exception">
  2001. <dt class="sig sig-object py" id="optparse.OptionConflictError">
  2002. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">optparse.</span></span><span class="sig-name descname"><span class="pre">OptionConflictError</span></span><a class="headerlink" href="#optparse.OptionConflictError" title="Link to this definition">¶</a></dt>
  2003. <dd><p>Raised if conflicting options are added to an <a class="reference internal" href="#optparse.OptionParser" title="optparse.OptionParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">OptionParser</span></code></a>.</p>
  2004. </dd></dl>
  2005. <dl class="py exception">
  2006. <dt class="sig sig-object py" id="optparse.OptionValueError">
  2007. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">optparse.</span></span><span class="sig-name descname"><span class="pre">OptionValueError</span></span><a class="headerlink" href="#optparse.OptionValueError" title="Link to this definition">¶</a></dt>
  2008. <dd><p>Raised if an invalid option value is encountered on the command line.</p>
  2009. </dd></dl>
  2010. <dl class="py exception">
  2011. <dt class="sig sig-object py" id="optparse.BadOptionError">
  2012. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">optparse.</span></span><span class="sig-name descname"><span class="pre">BadOptionError</span></span><a class="headerlink" href="#optparse.BadOptionError" title="Link to this definition">¶</a></dt>
  2013. <dd><p>Raised if an invalid option is passed on the command line.</p>
  2014. </dd></dl>
  2015. <dl class="py exception">
  2016. <dt class="sig sig-object py" id="optparse.AmbiguousOptionError">
  2017. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">optparse.</span></span><span class="sig-name descname"><span class="pre">AmbiguousOptionError</span></span><a class="headerlink" href="#optparse.AmbiguousOptionError" title="Link to this definition">¶</a></dt>
  2018. <dd><p>Raised if an ambiguous option is passed on the command line.</p>
  2019. </dd></dl>
  2020. </section>
  2021. </section>
  2022. <div class="clearer"></div>
  2023. </div>
  2024. </div>
  2025. </div>
  2026. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  2027. <div class="sphinxsidebarwrapper">
  2028. <div>
  2029. <h3><a href="../contents.html">Table of Contents</a></h3>
  2030. <ul>
  2031. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code> — Parser for command line options</a><ul>
  2032. <li><a class="reference internal" href="#background">Background</a><ul>
  2033. <li><a class="reference internal" href="#terminology">Terminology</a></li>
  2034. <li><a class="reference internal" href="#what-are-options-for">What are options for?</a></li>
  2035. <li><a class="reference internal" href="#what-are-positional-arguments-for">What are positional arguments for?</a></li>
  2036. </ul>
  2037. </li>
  2038. <li><a class="reference internal" href="#tutorial">Tutorial</a><ul>
  2039. <li><a class="reference internal" href="#understanding-option-actions">Understanding option actions</a></li>
  2040. <li><a class="reference internal" href="#the-store-action">The store action</a></li>
  2041. <li><a class="reference internal" href="#handling-boolean-flag-options">Handling boolean (flag) options</a></li>
  2042. <li><a class="reference internal" href="#other-actions">Other actions</a></li>
  2043. <li><a class="reference internal" href="#default-values">Default values</a></li>
  2044. <li><a class="reference internal" href="#generating-help">Generating help</a><ul>
  2045. <li><a class="reference internal" href="#grouping-options">Grouping Options</a></li>
  2046. </ul>
  2047. </li>
  2048. <li><a class="reference internal" href="#printing-a-version-string">Printing a version string</a></li>
  2049. <li><a class="reference internal" href="#how-optparse-handles-errors">How <code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code> handles errors</a></li>
  2050. <li><a class="reference internal" href="#putting-it-all-together">Putting it all together</a></li>
  2051. </ul>
  2052. </li>
  2053. <li><a class="reference internal" href="#reference-guide">Reference Guide</a><ul>
  2054. <li><a class="reference internal" href="#creating-the-parser">Creating the parser</a></li>
  2055. <li><a class="reference internal" href="#populating-the-parser">Populating the parser</a></li>
  2056. <li><a class="reference internal" href="#defining-options">Defining options</a></li>
  2057. <li><a class="reference internal" href="#option-attributes">Option attributes</a></li>
  2058. <li><a class="reference internal" href="#standard-option-actions">Standard option actions</a></li>
  2059. <li><a class="reference internal" href="#standard-option-types">Standard option types</a></li>
  2060. <li><a class="reference internal" href="#parsing-arguments">Parsing arguments</a></li>
  2061. <li><a class="reference internal" href="#querying-and-manipulating-your-option-parser">Querying and manipulating your option parser</a></li>
  2062. <li><a class="reference internal" href="#conflicts-between-options">Conflicts between options</a></li>
  2063. <li><a class="reference internal" href="#cleanup">Cleanup</a></li>
  2064. <li><a class="reference internal" href="#other-methods">Other methods</a></li>
  2065. </ul>
  2066. </li>
  2067. <li><a class="reference internal" href="#option-callbacks">Option Callbacks</a><ul>
  2068. <li><a class="reference internal" href="#defining-a-callback-option">Defining a callback option</a></li>
  2069. <li><a class="reference internal" href="#how-callbacks-are-called">How callbacks are called</a></li>
  2070. <li><a class="reference internal" href="#raising-errors-in-a-callback">Raising errors in a callback</a></li>
  2071. <li><a class="reference internal" href="#callback-example-1-trivial-callback">Callback example 1: trivial callback</a></li>
  2072. <li><a class="reference internal" href="#callback-example-2-check-option-order">Callback example 2: check option order</a></li>
  2073. <li><a class="reference internal" href="#callback-example-3-check-option-order-generalized">Callback example 3: check option order (generalized)</a></li>
  2074. <li><a class="reference internal" href="#callback-example-4-check-arbitrary-condition">Callback example 4: check arbitrary condition</a></li>
  2075. <li><a class="reference internal" href="#callback-example-5-fixed-arguments">Callback example 5: fixed arguments</a></li>
  2076. <li><a class="reference internal" href="#callback-example-6-variable-arguments">Callback example 6: variable arguments</a></li>
  2077. </ul>
  2078. </li>
  2079. <li><a class="reference internal" href="#extending-optparse">Extending <code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a><ul>
  2080. <li><a class="reference internal" href="#adding-new-types">Adding new types</a></li>
  2081. <li><a class="reference internal" href="#adding-new-actions">Adding new actions</a></li>
  2082. </ul>
  2083. </li>
  2084. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  2085. </ul>
  2086. </li>
  2087. </ul>
  2088. </div>
  2089. <div>
  2090. <h4>Previous topic</h4>
  2091. <p class="topless"><a href="nntplib.html"
  2092. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">nntplib</span></code> — NNTP protocol client</a></p>
  2093. </div>
  2094. <div>
  2095. <h4>Next topic</h4>
  2096. <p class="topless"><a href="ossaudiodev.html"
  2097. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ossaudiodev</span></code> — Access to OSS-compatible audio devices</a></p>
  2098. </div>
  2099. <div role="note" aria-label="source link">
  2100. <h3>This Page</h3>
  2101. <ul class="this-page-menu">
  2102. <li><a href="../bugs.html">Report a Bug</a></li>
  2103. <li>
  2104. <a href="https://github.com/python/cpython/blob/main/Doc/library/optparse.rst"
  2105. rel="nofollow">Show Source
  2106. </a>
  2107. </li>
  2108. </ul>
  2109. </div>
  2110. </div>
  2111. <div id="sidebarbutton" title="Collapse sidebar">
  2112. <span>«</span>
  2113. </div>
  2114. </div>
  2115. <div class="clearer"></div>
  2116. </div>
  2117. <div class="related" role="navigation" aria-label="related navigation">
  2118. <h3>Navigation</h3>
  2119. <ul>
  2120. <li class="right" style="margin-right: 10px">
  2121. <a href="../genindex.html" title="General Index"
  2122. >index</a></li>
  2123. <li class="right" >
  2124. <a href="../py-modindex.html" title="Python Module Index"
  2125. >modules</a> |</li>
  2126. <li class="right" >
  2127. <a href="ossaudiodev.html" title="ossaudiodev — Access to OSS-compatible audio devices"
  2128. >next</a> |</li>
  2129. <li class="right" >
  2130. <a href="nntplib.html" title="nntplib — NNTP protocol client"
  2131. >previous</a> |</li>
  2132. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  2133. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  2134. <li class="switchers">
  2135. <div class="language_switcher_placeholder"></div>
  2136. <div class="version_switcher_placeholder"></div>
  2137. </li>
  2138. <li>
  2139. </li>
  2140. <li id="cpython-language-and-version">
  2141. <a href="../index.html">3.12.3 Documentation</a> &#187;
  2142. </li>
  2143. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  2144. <li class="nav-item nav-item-2"><a href="superseded.html" >Superseded Modules</a> &#187;</li>
  2145. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code> — Parser for command line options</a></li>
  2146. <li class="right">
  2147. <div class="inline-search" role="search">
  2148. <form class="inline-search" action="../search.html" method="get">
  2149. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  2150. <input type="submit" value="Go" />
  2151. </form>
  2152. </div>
  2153. |
  2154. </li>
  2155. <li class="right">
  2156. <label class="theme-selector-label">
  2157. Theme
  2158. <select class="theme-selector" oninput="activateTheme(this.value)">
  2159. <option value="auto" selected>Auto</option>
  2160. <option value="light">Light</option>
  2161. <option value="dark">Dark</option>
  2162. </select>
  2163. </label> |</li>
  2164. </ul>
  2165. </div>
  2166. <div class="footer">
  2167. &copy;
  2168. <a href="../copyright.html">
  2169. Copyright
  2170. </a>
  2171. 2001-2024, Python Software Foundation.
  2172. <br />
  2173. This page is licensed under the Python Software Foundation License Version 2.
  2174. <br />
  2175. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  2176. <br />
  2177. See <a href="/license.html">History and License</a> for more information.<br />
  2178. <br />
  2179. The Python Software Foundation is a non-profit corporation.
  2180. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  2181. <br />
  2182. <br />
  2183. Last updated on Apr 09, 2024 (13:47 UTC).
  2184. <a href="/bugs.html">Found a bug</a>?
  2185. <br />
  2186. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  2187. </div>
  2188. </body>
  2189. </html>
上海开阖软件有限公司 沪ICP备12045867号-1