parseopt.html 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <!-- This file is generated by Nim. -->
  4. <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>std/parseopt</title>
  9. <!-- Google fonts -->
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
  11. <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
  12. <!-- Favicon -->
  13. <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
  14. <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
  15. <!-- CSS -->
  16. <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
  17. <!-- JS -->
  18. <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
  19. </head>
  20. <body>
  21. <div class="document" id="documentId">
  22. <div class="container">
  23. <h1 class="title">std/parseopt</h1>
  24. <div class="row">
  25. <div class="three columns">
  26. <div class="theme-select-wrapper">
  27. <label for="theme-select">Theme:&nbsp;</label>
  28. <select id="theme-select" onchange="setTheme(this.value)">
  29. <option value="auto">🌗 Match OS</option>
  30. <option value="dark">🌑 Dark</option>
  31. <option value="light">🌕 Light</option>
  32. </select>
  33. </div>
  34. <div id="global-links">
  35. <ul class="simple-boot">
  36. <li><a href="manual.html">Manual</a></li>
  37. <li><a href="lib.html">Standard library</a></li>
  38. <li> <a id="indexLink" href="theindex.html">Index</a></li>
  39. <li><a href="compiler/theindex.html">Compiler docs</a></li>
  40. <li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
  41. <li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
  42. </ul>
  43. </div>
  44. <div id="searchInputDiv">
  45. Search: <input type="search" id="searchInput"
  46. oninput="search()" />
  47. </div>
  48. <ul class="simple simple-toc" id="toc-list">
  49. <li><a class="reference" id="supported-syntax_toc" href="#supported-syntax">Supported Syntax</a></li>
  50. <li><a class="reference" id="parsing_toc" href="#parsing">Parsing</a></li>
  51. <li><a class="reference" id="nimshortnoval-and-nimlongnoval_toc" href="#nimshortnoval-and-nimlongnoval"><tt class="docutils literal"><span class="pre"><span class="Identifier">shortNoVal</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">longNoVal</span></span></tt></a></li>
  52. <li><a class="reference" id="see-also_toc" href="#see-also">See also</a></li>
  53. <li>
  54. <a class="reference reference-toplevel" href="#6" id="56">Imports</a>
  55. </li>
  56. <li>
  57. <details open>
  58. <summary><a class="reference reference-toplevel" href="#7" id="57">Types</a></summary>
  59. <ul class="simple simple-toc-section">
  60. <li><a class="reference" href="#CmdLineKind" title="CmdLineKind = enum
  61. cmdEnd, ## End of command line reached
  62. cmdArgument, ## An argument such as a filename
  63. cmdLongOption, ## A long option such as --option
  64. cmdShortOption ## A short option such as -c">CmdLineKind</a></li>
  65. <li><a class="reference" href="#OptParser" title="OptParser = object of RootObj
  66. kind*: CmdLineKind ## The detected command line token
  67. key*, val*: string ## Key and value pair; the key is the option
  68. ## or the argument, and the value is not &quot;&quot; if
  69. ## the option was given a value">OptParser</a></li>
  70. </ul>
  71. </details>
  72. </li>
  73. <li>
  74. <details open>
  75. <summary><a class="reference reference-toplevel" href="#12" id="62">Procs</a></summary>
  76. <ul class="simple simple-toc-section">
  77. <ul class="simple nested-toc-section">cmdLineRest
  78. <li><a class="reference" href="#cmdLineRest%2COptParser" title="cmdLineRest(p: OptParser): string">cmdLineRest(p: OptParser): string</a></li>
  79. </ul>
  80. <ul class="simple nested-toc-section">initOptParser
  81. <li><a class="reference" href="#initOptParser%2Cstring%2Cset%5Bchar%5D%2Cseq%5Bstring%5D" title="initOptParser(cmdline = &quot;&quot;; shortNoVal: set[char] = {};
  82. longNoVal: seq[string] = @[]; allowWhitespaceAfterColon = true): OptParser">initOptParser(cmdline = &quot;&quot;; shortNoVal: set[char] = {};
  83. longNoVal: seq[string] = @[]; allowWhitespaceAfterColon = true): OptParser</a></li>
  84. <li><a class="reference" href="#initOptParser%2Cseq%5Bstring%5D%2Cset%5Bchar%5D%2Cseq%5Bstring%5D" title="initOptParser(cmdline: seq[string]; shortNoVal: set[char] = {};
  85. longNoVal: seq[string] = @[]; allowWhitespaceAfterColon = true): OptParser">initOptParser(cmdline: seq[string]; shortNoVal: set[char] = {};
  86. longNoVal: seq[string] = @[]; allowWhitespaceAfterColon = true): OptParser</a></li>
  87. </ul>
  88. <ul class="simple nested-toc-section">next
  89. <li><a class="reference" href="#next%2COptParser" title="next(p: var OptParser)">next(p: var OptParser)</a></li>
  90. </ul>
  91. <ul class="simple nested-toc-section">remainingArgs
  92. <li><a class="reference" href="#remainingArgs%2COptParser" title="remainingArgs(p: OptParser): seq[string]">remainingArgs(p: OptParser): seq[string]</a></li>
  93. </ul>
  94. </ul>
  95. </details>
  96. </li>
  97. <li>
  98. <details open>
  99. <summary><a class="reference reference-toplevel" href="#15" id="65">Iterators</a></summary>
  100. <ul class="simple simple-toc-section">
  101. <ul class="simple nested-toc-section">getopt
  102. <li><a class="reference" href="#getopt.i%2Cseq%5Bstring%5D%2Cset%5Bchar%5D%2Cseq%5Bstring%5D" title="getopt(cmdline: seq[string] = @[]; shortNoVal: set[char] = {};
  103. longNoVal: seq[string] = @[]): tuple[kind: CmdLineKind, key, val: string]">getopt(cmdline: seq[string] = @[]; shortNoVal: set[char] = {};
  104. longNoVal: seq[string] = @[]): tuple[kind: CmdLineKind, key, val: string]</a></li>
  105. <li><a class="reference" href="#getopt.i%2COptParser" title="getopt(p: var OptParser): tuple[kind: CmdLineKind, key, val: string]">getopt(p: var OptParser): tuple[kind: CmdLineKind, key, val: string]</a></li>
  106. </ul>
  107. </ul>
  108. </details>
  109. </li>
  110. </ul>
  111. </div>
  112. <div class="nine columns" id="content">
  113. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L1" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  114. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L1" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  115. <div id="tocRoot"></div>
  116. <p class="module-desc">This module provides the standard Nim command line parser. It supports one convenience iterator over all command line options and some lower-level features.
  117. <h1><a class="toc-backref" id="supported-syntax" href="#supported-syntax">Supported Syntax</a></h1><p>The following syntax is supported when arguments for the <tt class="docutils literal"><span class="pre"><span class="Identifier">shortNoVal</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">longNoVal</span></span></tt> parameters, which are <a class="reference external" href="#nimshortnoval-and-nimlongnoval">described later</a>, are not provided:</p>
  118. <ol class="simple"><li>Short options: <tt class="docutils literal"><span class="pre"><span class="Operator">-</span><span class="Identifier">abcd</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Operator">-</span><span class="Identifier">e</span><span class="Punctuation">:</span><span class="DecNumber">5</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Operator">-</span><span class="Identifier">e</span><span class="Operator">=</span><span class="DecNumber">5</span></span></tt></li>
  119. <li>Long options: <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">foo</span><span class="Punctuation">:</span><span class="Identifier">bar</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">foo</span><span class="Operator">=</span><span class="Identifier">bar</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">foo</span></span></tt></li>
  120. <li>Arguments: everything that does not start with a <tt class="docutils literal"><span class="pre"><span class="Operator">-</span></span></tt></li>
  121. </ol>
  122. <p>These three kinds of tokens are enumerated in the <a class="reference external" href="#CmdLineKind">CmdLineKind enum</a>.</p>
  123. <p>When option values begin with ':' or '=', they need to be doubled up (as in <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">delim</span><span class="Punctuation">:</span><span class="Punctuation">:</span></span></tt>) or alternated (as in <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">delim</span><span class="Operator">=:</span></span></tt>).</p>
  124. <p>The <tt class="docutils literal"><span class="pre"><span class="Operator">--</span></span></tt> option, commonly used to denote that every token that follows is an argument, is interpreted as a long option, and its name is the empty string.</p>
  125. <h1><a class="toc-backref" id="parsing" href="#parsing">Parsing</a></h1><p>Use an <a class="reference external" href="#OptParser">OptParser</a> to parse command line options. It can be created with <a class="reference external" href="#initOptParser,string,set[char],seq[string]">initOptParser</a>, and <a class="reference external" href="#next,OptParser">next</a> advances the parser by one token.</p>
  126. <p>For each token, the parser's <tt class="docutils literal"><span class="pre"><span class="Identifier">kind</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">key</span></span></tt>, and <tt class="docutils literal"><span class="pre"><span class="Identifier">val</span></span></tt> fields give information about that token. If the token is a long or short option, <tt class="docutils literal"><span class="pre"><span class="Identifier">key</span></span></tt> is the option's name, and <tt class="docutils literal"><span class="pre"><span class="Identifier">val</span></span></tt> is either the option's value, if provided, or the empty string. For arguments, the <tt class="docutils literal"><span class="pre"><span class="Identifier">key</span></span></tt> field contains the argument itself, and <tt class="docutils literal"><span class="pre"><span class="Identifier">val</span></span></tt> is unused. To check if the end of the command line has been reached, check if <tt class="docutils literal"><span class="pre"><span class="Identifier">kind</span></span></tt> is equal to <tt class="docutils literal"><span class="pre"><span class="Identifier">cmdEnd</span></span></tt>.</p>
  127. <p>Here is an example:</p>
  128. <p><pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">parseopt</span>
  129. <span class="Keyword">var</span> <span class="Identifier">p</span> <span class="Operator">=</span> <span class="Identifier">initOptParser</span><span class="Punctuation">(</span><span class="StringLit">&quot;-ab -e:5 --foo --bar=20 file.txt&quot;</span><span class="Punctuation">)</span>
  130. <span class="Keyword">while</span> <span class="Identifier">true</span><span class="Punctuation">:</span>
  131. <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  132. <span class="Keyword">case</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">kind</span>
  133. <span class="Keyword">of</span> <span class="Identifier">cmdEnd</span><span class="Punctuation">:</span> <span class="Keyword">break</span>
  134. <span class="Keyword">of</span> <span class="Identifier">cmdShortOption</span><span class="Punctuation">,</span> <span class="Identifier">cmdLongOption</span><span class="Punctuation">:</span>
  135. <span class="Keyword">if</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">val</span> <span class="Operator">==</span> <span class="StringLit">&quot;&quot;</span><span class="Punctuation">:</span>
  136. <span class="Identifier">echo</span> <span class="StringLit">&quot;Option: &quot;</span><span class="Punctuation">,</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">key</span>
  137. <span class="Keyword">else</span><span class="Punctuation">:</span>
  138. <span class="Identifier">echo</span> <span class="StringLit">&quot;Option and value: &quot;</span><span class="Punctuation">,</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">key</span><span class="Punctuation">,</span> <span class="StringLit">&quot;, &quot;</span><span class="Punctuation">,</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">val</span>
  139. <span class="Keyword">of</span> <span class="Identifier">cmdArgument</span><span class="Punctuation">:</span>
  140. <span class="Identifier">echo</span> <span class="StringLit">&quot;Argument: &quot;</span><span class="Punctuation">,</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">key</span>
  141. <span class="Comment"># Output:</span>
  142. <span class="Comment"># Option: a</span>
  143. <span class="Comment"># Option: b</span>
  144. <span class="Comment"># Option and value: e, 5</span>
  145. <span class="Comment"># Option: foo</span>
  146. <span class="Comment"># Option and value: bar, 20</span>
  147. <span class="Comment"># Argument: file.txt</span></pre></p>
  148. <p>The <a class="reference external" href="#getopt.i,OptParser">getopt iterator</a>, which is provided for convenience, can be used to iterate through all command line options as well.</p>
  149. <p>To set a default value for a variable assigned through <tt class="docutils literal"><span class="pre"><span class="Identifier">getopt</span></span></tt> and accept arguments from the cmd line. Assign the default value to a variable before parsing. Then set the variable to the new value while parsing.</p>
  150. <p>Here is an example:</p>
  151. <p><pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">parseopt</span>
  152. <span class="Keyword">var</span> <span class="Identifier">varName</span><span class="Punctuation">:</span> <span class="Identifier">string</span> <span class="Operator">=</span> <span class="StringLit">&quot;defaultValue&quot;</span>
  153. <span class="Keyword">for</span> <span class="Identifier">kind</span><span class="Punctuation">,</span> <span class="Identifier">key</span><span class="Punctuation">,</span> <span class="Identifier">val</span> <span class="Keyword">in</span> <span class="Identifier">getopt</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
  154. <span class="Keyword">case</span> <span class="Identifier">kind</span>
  155. <span class="Keyword">of</span> <span class="Identifier">cmdArgument</span><span class="Punctuation">:</span>
  156. <span class="Keyword">discard</span>
  157. <span class="Keyword">of</span> <span class="Identifier">cmdLongOption</span><span class="Punctuation">,</span> <span class="Identifier">cmdShortOption</span><span class="Punctuation">:</span>
  158. <span class="Keyword">case</span> <span class="Identifier">key</span><span class="Punctuation">:</span>
  159. <span class="Keyword">of</span> <span class="StringLit">&quot;varName&quot;</span><span class="Punctuation">:</span> <span class="Comment"># --varName:&lt;value&gt; in the console when executing</span>
  160. <span class="Identifier">varName</span> <span class="Operator">=</span> <span class="Identifier">val</span> <span class="Comment"># do input sanitization in production systems</span>
  161. <span class="Keyword">of</span> <span class="Identifier">cmdEnd</span><span class="Punctuation">:</span>
  162. <span class="Keyword">discard</span></pre></p>
  163. <h1><a class="toc-backref" id="nimshortnoval-and-nimlongnoval" href="#nimshortnoval-and-nimlongnoval"><tt class="docutils literal"><span class="pre"><span class="Identifier">shortNoVal</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">longNoVal</span></span></tt></a></h1><p>The optional <tt class="docutils literal"><span class="pre"><span class="Identifier">shortNoVal</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">longNoVal</span></span></tt> parameters present in <a class="reference external" href="#initOptParser,string,set[char],seq[string]">initOptParser</a> are for specifying which short and long options do not accept values.</p>
  164. <p>When <tt class="docutils literal"><span class="pre"><span class="Identifier">shortNoVal</span></span></tt> is non-empty, users are not required to separate short options and their values with a ':' or '=' since the parser knows which options accept values and which ones do not. This behavior also applies for long options if <tt class="docutils literal"><span class="pre"><span class="Identifier">longNoVal</span></span></tt> is non-empty. For short options, <tt class="docutils literal"><span class="pre"><span class="Operator">-</span><span class="Identifier">j4</span></span></tt> becomes supported syntax, and for long options, <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">foo</span> <span class="Identifier">bar</span></span></tt> becomes supported. This is in addition to the <a class="reference external" href="#supported-syntax">previously mentioned syntax</a>. Users can still separate options and their values with ':' or '=', but that becomes optional.</p>
  165. <p>As more options which do not accept values are added to your program, remember to amend <tt class="docutils literal"><span class="pre"><span class="Identifier">shortNoVal</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">longNoVal</span></span></tt> accordingly.</p>
  166. <p>The following example illustrates the difference between having an empty <tt class="docutils literal"><span class="pre"><span class="Identifier">shortNoVal</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">longNoVal</span></span></tt>, which is the default, and providing arguments for those two parameters:</p>
  167. <p><pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">parseopt</span>
  168. <span class="Keyword">proc</span> <span class="Identifier">printToken</span><span class="Punctuation">(</span><span class="Identifier">kind</span><span class="Punctuation">:</span> <span class="Identifier">CmdLineKind</span><span class="Punctuation">,</span> <span class="Identifier">key</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">,</span> <span class="Identifier">val</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">)</span> <span class="Operator">=</span>
  169. <span class="Keyword">case</span> <span class="Identifier">kind</span>
  170. <span class="Keyword">of</span> <span class="Identifier">cmdEnd</span><span class="Punctuation">:</span> <span class="Identifier">doAssert</span><span class="Punctuation">(</span><span class="Identifier">false</span><span class="Punctuation">)</span> <span class="Comment"># Doesn't happen with getopt()</span>
  171. <span class="Keyword">of</span> <span class="Identifier">cmdShortOption</span><span class="Punctuation">,</span> <span class="Identifier">cmdLongOption</span><span class="Punctuation">:</span>
  172. <span class="Keyword">if</span> <span class="Identifier">val</span> <span class="Operator">==</span> <span class="StringLit">&quot;&quot;</span><span class="Punctuation">:</span>
  173. <span class="Identifier">echo</span> <span class="StringLit">&quot;Option: &quot;</span><span class="Punctuation">,</span> <span class="Identifier">key</span>
  174. <span class="Keyword">else</span><span class="Punctuation">:</span>
  175. <span class="Identifier">echo</span> <span class="StringLit">&quot;Option and value: &quot;</span><span class="Punctuation">,</span> <span class="Identifier">key</span><span class="Punctuation">,</span> <span class="StringLit">&quot;, &quot;</span><span class="Punctuation">,</span> <span class="Identifier">val</span>
  176. <span class="Keyword">of</span> <span class="Identifier">cmdArgument</span><span class="Punctuation">:</span>
  177. <span class="Identifier">echo</span> <span class="StringLit">&quot;Argument: &quot;</span><span class="Punctuation">,</span> <span class="Identifier">key</span>
  178. <span class="Keyword">let</span> <span class="Identifier">cmdLine</span> <span class="Operator">=</span> <span class="StringLit">&quot;-j4 --first bar&quot;</span>
  179. <span class="Keyword">var</span> <span class="Identifier">emptyNoVal</span> <span class="Operator">=</span> <span class="Identifier">initOptParser</span><span class="Punctuation">(</span><span class="Identifier">cmdLine</span><span class="Punctuation">)</span>
  180. <span class="Keyword">for</span> <span class="Identifier">kind</span><span class="Punctuation">,</span> <span class="Identifier">key</span><span class="Punctuation">,</span> <span class="Identifier">val</span> <span class="Keyword">in</span> <span class="Identifier">emptyNoVal</span><span class="Operator">.</span><span class="Identifier">getopt</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
  181. <span class="Identifier">printToken</span><span class="Punctuation">(</span><span class="Identifier">kind</span><span class="Punctuation">,</span> <span class="Identifier">key</span><span class="Punctuation">,</span> <span class="Identifier">val</span><span class="Punctuation">)</span>
  182. <span class="Comment"># Output:</span>
  183. <span class="Comment"># Option: j</span>
  184. <span class="Comment"># Option: 4</span>
  185. <span class="Comment"># Option: first</span>
  186. <span class="Comment"># Argument: bar</span>
  187. <span class="Keyword">var</span> <span class="Identifier">withNoVal</span> <span class="Operator">=</span> <span class="Identifier">initOptParser</span><span class="Punctuation">(</span><span class="Identifier">cmdLine</span><span class="Punctuation">,</span> <span class="Identifier">shortNoVal</span> <span class="Operator">=</span> <span class="Punctuation">{</span><span class="Punctuation">'</span><span class="Identifier">c</span><span class="CharLit">'},</span>
  188. <span class="Identifier">longNoVal</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;second&quot;</span><span class="Punctuation">]</span><span class="Punctuation">)</span>
  189. <span class="Keyword">for</span> <span class="Identifier">kind</span><span class="Punctuation">,</span> <span class="Identifier">key</span><span class="Punctuation">,</span> <span class="Identifier">val</span> <span class="Keyword">in</span> <span class="Identifier">withNoVal</span><span class="Operator">.</span><span class="Identifier">getopt</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
  190. <span class="Identifier">printToken</span><span class="Punctuation">(</span><span class="Identifier">kind</span><span class="Punctuation">,</span> <span class="Identifier">key</span><span class="Punctuation">,</span> <span class="Identifier">val</span><span class="Punctuation">)</span>
  191. <span class="Comment"># Output:</span>
  192. <span class="Comment"># Option and value: j, 4</span>
  193. <span class="Comment"># Option and value: first, bar</span></pre></p>
  194. <h1><a class="toc-backref" id="see-also" href="#see-also">See also</a></h1><ul class="simple"><li><a class="reference external" href="os.html">os module</a> for lower-level command line parsing procs</li>
  195. <li><a class="reference external" href="parseutils.html">parseutils module</a> for helpers that parse tokens, numbers, identifiers, etc.</li>
  196. <li><a class="reference external" href="strutils.html">strutils module</a> for common string handling operations</li>
  197. <li><a class="reference external" href="json.html">json module</a> for a JSON parser</li>
  198. <li><a class="reference external" href="parsecfg.html">parsecfg module</a> for a configuration file parser</li>
  199. <li><a class="reference external" href="parsecsv.html">parsecsv module</a> for a simple CSV (comma separated value) parser</li>
  200. <li><a class="reference external" href="parsexml.html">parsexml module</a> for a XML / HTML parser</li>
  201. <li><a class="reference external" href="lib.html#pure-libraries-parsers">other parsers</a> for more parsers</li>
  202. </ul>
  203. </p>
  204. <div class="section" id="6">
  205. <h1><a class="toc-backref" href="#6">Imports</a></h1>
  206. <dl class="item">
  207. <a class="reference external" href="strutils.html">strutils</a>, <a class="reference external" href="os.html">os</a>
  208. </dl>
  209. </div>
  210. <div class="section" id="7">
  211. <h1><a class="toc-backref" href="#7">Types</a></h1>
  212. <dl class="item">
  213. <div id="CmdLineKind">
  214. <dt><pre><a href="parseopt.html#CmdLineKind"><span class="Identifier">CmdLineKind</span></a> <span class="Other">=</span> <span class="Keyword">enum</span>
  215. <span class="Identifier">cmdEnd</span><span class="Other">,</span> <span class="Comment">## End of command line reached</span>
  216. <span class="Identifier">cmdArgument</span><span class="Other">,</span> <span class="Comment">## An argument such as a filename</span>
  217. <span class="Identifier">cmdLongOption</span><span class="Other">,</span> <span class="Comment">## A long option such as --option</span>
  218. <span class="Identifier">cmdShortOption</span> <span class="Comment">## A short option such as -c</span></pre></dt>
  219. <dd>
  220. The detected command line token.
  221. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L183" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  222. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L183" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  223. </dd>
  224. </div>
  225. <div id="OptParser">
  226. <dt><pre><a href="parseopt.html#OptParser"><span class="Identifier">OptParser</span></a> <span class="Other">=</span> <span class="Keyword">object</span> <span class="Keyword">of</span> <a href="system.html#RootObj"><span class="Identifier">RootObj</span></a>
  227. <span class="Identifier">kind</span><span class="Operator">*</span><span class="Other">:</span> <a href="parseopt.html#CmdLineKind"><span class="Identifier">CmdLineKind</span></a> <span class="Comment">## The detected command line token</span>
  228. <span class="Identifier">key</span><span class="Operator">*</span><span class="Other">,</span> <span class="Identifier">val</span><span class="Operator">*</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a> <span class="Comment">## Key and value pair; the key is the option</span>
  229. <span class="Comment">## or the argument, and the value is not &quot;&quot; if</span>
  230. <span class="Comment">## the option was given a value</span></pre></dt>
  231. <dd>
  232. <p>
  233. Implementation of the command line parser.</p>
  234. <p>To initialize it, use the <a class="reference external" href="#initOptParser,string,set[char],seq[string]">initOptParser proc</a>.</p>
  235. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L188" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  236. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L188" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  237. </dd>
  238. </div>
  239. </dl>
  240. </div>
  241. <div class="section" id="12">
  242. <h1><a class="toc-backref" href="#12">Procs</a></h1>
  243. <dl class="item">
  244. <div id="cmdLineRest-procs-all">
  245. <div id="cmdLineRest,OptParser">
  246. <dt><pre><span class="Keyword">proc</span> <a href="#cmdLineRest%2COptParser"><span class="Identifier">cmdLineRest</span></a><span class="Other">(</span><span class="Identifier">p</span><span class="Other">:</span> <a href="parseopt.html#OptParser"><span class="Identifier">OptParser</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">gcsafe</span><span class="Other">,</span> <span class="Identifier">extern</span><span class="Other">:</span> <span class="StringLit">&quot;npo$1&quot;</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
  247. <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  248. <dd>
  249. <p>Retrieves the rest of the command line that has not been parsed yet.</p>
  250. <p>See also:</p>
  251. <ul class="simple"><li><a class="reference external" href="#remainingArgs,OptParser">remainingArgs proc</a></li>
  252. </ul>
  253. <p><strong>Examples:</strong></p>
  254. <p><pre class="listing"><span class="Keyword">var</span> <span class="Identifier">p</span> <span class="Operator">=</span> <span class="Identifier">initOptParser</span><span class="Punctuation">(</span><span class="StringLit">&quot;--left -r:2 -- foo.txt bar.txt&quot;</span><span class="Punctuation">)</span>
  255. <span class="Keyword">while</span> <span class="Identifier">true</span><span class="Punctuation">:</span>
  256. <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  257. <span class="Keyword">if</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">==</span> <span class="Identifier">cmdLongOption</span> <span class="Keyword">and</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">key</span> <span class="Operator">==</span> <span class="StringLit">&quot;&quot;</span><span class="Punctuation">:</span> <span class="Comment"># Look for &quot;--&quot;</span>
  258. <span class="Keyword">break</span>
  259. <span class="Identifier">doAssert</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">cmdLineRest</span> <span class="Operator">==</span> <span class="StringLit">&quot;foo.txt bar.txt&quot;</span></pre></p>
  260. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L396" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  261. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L396" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  262. </dd>
  263. </div>
  264. </div>
  265. <div id="initOptParser-procs-all">
  266. <div id="initOptParser,string,set[char],seq[string]">
  267. <dt><pre><span class="Keyword">proc</span> <a href="#initOptParser%2Cstring%2Cset%5Bchar%5D%2Cseq%5Bstring%5D"><span class="Identifier">initOptParser</span></a><span class="Other">(</span><span class="Identifier">cmdline</span> <span class="Other">=</span> <span class="StringLit">&quot;&quot;</span><span class="Other">;</span> <span class="Identifier">shortNoVal</span><span class="Other">:</span> <a href="system.html#set"><span class="Identifier">set</span></a><span class="Other">[</span><a href="system.html#char"><span class="Identifier">char</span></a><span class="Other">]</span> <span class="Other">=</span> <span class="Other">{</span><span class="Other">}</span><span class="Other">;</span>
  268. <span class="Identifier">longNoVal</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><a href="system.html#string"><span class="Identifier">string</span></a><span class="Other">]</span> <span class="Other">=</span> <span class="Operator">@</span><span class="Other">[</span><span class="Other">]</span><span class="Other">;</span>
  269. <span class="Identifier">allowWhitespaceAfterColon</span> <span class="Other">=</span> <span class="Identifier">true</span><span class="Other">)</span><span class="Other">:</span> <a href="parseopt.html#OptParser"><span class="Identifier">OptParser</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
  270. <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">ReadIOEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  271. <dd>
  272. <p>Initializes the command line parser.</p>
  273. <p>If <tt class="docutils literal"><span class="pre"><span class="Identifier">cmdline</span> <span class="Operator">==</span> <span class="StringLit">&quot;&quot;</span></span></tt>, the real command line as provided by the <tt class="docutils literal"><span class="pre"><span class="Identifier">os</span></span></tt> module is retrieved instead if it is available. If the command line is not available, a <tt class="docutils literal"><span class="pre"><span class="Identifier">ValueError</span></span></tt> will be raised.</p>
  274. <p><tt class="docutils literal"><span class="pre"><span class="Identifier">shortNoVal</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">longNoVal</span></span></tt> are used to specify which options do not take values. See the <a class="reference external" href="#nimshortnoval-and-nimlongnoval">documentation about these parameters</a> for more information on how this affects parsing.</p>
  275. <p>This does not provide a way of passing default values to arguments.</p>
  276. <p>See also:</p>
  277. <ul class="simple"><li><a class="reference external" href="#getopt.i,OptParser">getopt iterator</a></li>
  278. </ul>
  279. <p><strong class="examples_text">Example:</strong></p>
  280. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">p</span> <span class="Operator">=</span> <span class="Identifier">initOptParser</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  281. <span class="Identifier">p</span> <span class="Operator">=</span> <span class="Identifier">initOptParser</span><span class="Punctuation">(</span><span class="StringLit">&quot;--left --debug:3 -l -r:2&quot;</span><span class="Punctuation">)</span>
  282. <span class="Identifier">p</span> <span class="Operator">=</span> <span class="Identifier">initOptParser</span><span class="Punctuation">(</span><span class="StringLit">&quot;--left --debug:3 -l -r:2&quot;</span><span class="Punctuation">,</span>
  283. <span class="Identifier">shortNoVal</span> <span class="Operator">=</span> <span class="Punctuation">{</span><span class="Punctuation">'</span><span class="Identifier">l</span><span class="CharLit">'}, longNoVal = @[&quot;left&quot;])</span></pre>
  284. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L273" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  285. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L273" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  286. </dd>
  287. </div>
  288. <div id="initOptParser,seq[string],set[char],seq[string]">
  289. <dt><pre><span class="Keyword">proc</span> <a href="#initOptParser%2Cseq%5Bstring%5D%2Cset%5Bchar%5D%2Cseq%5Bstring%5D"><span class="Identifier">initOptParser</span></a><span class="Other">(</span><span class="Identifier">cmdline</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><a href="system.html#string"><span class="Identifier">string</span></a><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">shortNoVal</span><span class="Other">:</span> <a href="system.html#set"><span class="Identifier">set</span></a><span class="Other">[</span><a href="system.html#char"><span class="Identifier">char</span></a><span class="Other">]</span> <span class="Other">=</span> <span class="Other">{</span><span class="Other">}</span><span class="Other">;</span>
  290. <span class="Identifier">longNoVal</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><a href="system.html#string"><span class="Identifier">string</span></a><span class="Other">]</span> <span class="Other">=</span> <span class="Operator">@</span><span class="Other">[</span><span class="Other">]</span><span class="Other">;</span>
  291. <span class="Identifier">allowWhitespaceAfterColon</span> <span class="Other">=</span> <span class="Identifier">true</span><span class="Other">)</span><span class="Other">:</span> <a href="parseopt.html#OptParser"><span class="Identifier">OptParser</span></a> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
  292. <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">ReadIOEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  293. <dd>
  294. <p>Initializes the command line parser.</p>
  295. <p>If <tt class="docutils literal"><span class="pre"><span class="Identifier">cmdline</span><span class="Operator">.</span><span class="Identifier">len</span> <span class="Operator">==</span> <span class="DecNumber">0</span></span></tt>, the real command line as provided by the <tt class="docutils literal"><span class="pre"><span class="Identifier">os</span></span></tt> module is retrieved instead if it is available. If the command line is not available, a <tt class="docutils literal"><span class="pre"><span class="Identifier">ValueError</span></span></tt> will be raised. Behavior of the other parameters remains the same as in <a class="reference external" href="#initOptParser,string,set[char],seq[string]">initOptParser(string, ...)</a>.</p>
  296. <p>See also:</p>
  297. <ul class="simple"><li><a class="reference external" href="#getopt.i,seq[string],set[char],seq[string]">getopt iterator</a></li>
  298. </ul>
  299. <p><strong class="examples_text">Example:</strong></p>
  300. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">p</span> <span class="Operator">=</span> <span class="Identifier">initOptParser</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  301. <span class="Identifier">p</span> <span class="Operator">=</span> <span class="Identifier">initOptParser</span><span class="Punctuation">(</span><span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;--left&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;--debug:3&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;-l&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;-r:2&quot;</span><span class="Punctuation">]</span><span class="Punctuation">)</span>
  302. <span class="Identifier">p</span> <span class="Operator">=</span> <span class="Identifier">initOptParser</span><span class="Punctuation">(</span><span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;--left&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;--debug:3&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;-l&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;-r:2&quot;</span><span class="Punctuation">]</span><span class="Punctuation">,</span>
  303. <span class="Identifier">shortNoVal</span> <span class="Operator">=</span> <span class="Punctuation">{</span><span class="Punctuation">'</span><span class="Identifier">l</span><span class="CharLit">'}, longNoVal = @[&quot;left&quot;])</span></pre>
  304. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L221" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  305. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L221" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  306. </dd>
  307. </div>
  308. </div>
  309. <div id="next-procs-all">
  310. <div id="next,OptParser">
  311. <dt><pre><span class="Keyword">proc</span> <a href="#next%2COptParser"><span class="Identifier">next</span></a><span class="Other">(</span><span class="Identifier">p</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="parseopt.html#OptParser"><span class="Identifier">OptParser</span></a><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">gcsafe</span><span class="Other">,</span> <span class="Identifier">extern</span><span class="Other">:</span> <span class="StringLit">&quot;npo$1&quot;</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span>
  312. <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  313. <dd>
  314. <p>Parses the next token.</p>
  315. <p><tt class="docutils literal"><span class="pre"><span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">kind</span></span></tt> describes what kind of token has been parsed. <tt class="docutils literal"><span class="pre"><span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">key</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">val</span></span></tt> are set accordingly.</p>
  316. <p><strong class="examples_text">Example:</strong></p>
  317. <pre class="listing"><span class="Keyword">var</span> <span class="Identifier">p</span> <span class="Operator">=</span> <span class="Identifier">initOptParser</span><span class="Punctuation">(</span><span class="StringLit">&quot;--left -r:2 file.txt&quot;</span><span class="Punctuation">)</span>
  318. <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  319. <span class="Identifier">doAssert</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">==</span> <span class="Identifier">cmdLongOption</span> <span class="Keyword">and</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">key</span> <span class="Operator">==</span> <span class="StringLit">&quot;left&quot;</span>
  320. <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  321. <span class="Identifier">doAssert</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">==</span> <span class="Identifier">cmdShortOption</span> <span class="Keyword">and</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">key</span> <span class="Operator">==</span> <span class="StringLit">&quot;r&quot;</span> <span class="Keyword">and</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">val</span> <span class="Operator">==</span> <span class="StringLit">&quot;2&quot;</span>
  322. <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  323. <span class="Identifier">doAssert</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">==</span> <span class="Identifier">cmdArgument</span> <span class="Keyword">and</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">key</span> <span class="Operator">==</span> <span class="StringLit">&quot;file.txt&quot;</span>
  324. <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  325. <span class="Identifier">doAssert</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">==</span> <span class="Identifier">cmdEnd</span></pre>
  326. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L325" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  327. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L325" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  328. </dd>
  329. </div>
  330. </div>
  331. <div id="remainingArgs-procs-all">
  332. <div id="remainingArgs,OptParser">
  333. <dt><pre><span class="Keyword">proc</span> <a href="#remainingArgs%2COptParser"><span class="Identifier">remainingArgs</span></a><span class="Other">(</span><span class="Identifier">p</span><span class="Other">:</span> <a href="parseopt.html#OptParser"><span class="Identifier">OptParser</span></a><span class="Other">)</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><a href="system.html#string"><span class="Identifier">string</span></a><span class="Other">]</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">gcsafe</span><span class="Other">,</span> <span class="Identifier">extern</span><span class="Other">:</span> <span class="StringLit">&quot;npo$1&quot;</span><span class="Other">,</span>
  334. <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  335. <dd>
  336. <p>Retrieves a sequence of the arguments that have not been parsed yet.</p>
  337. <p>See also:</p>
  338. <ul class="simple"><li><a class="reference external" href="#cmdLineRest,OptParser">cmdLineRest proc</a></li>
  339. </ul>
  340. <p><strong>Examples:</strong></p>
  341. <p><pre class="listing"><span class="Keyword">var</span> <span class="Identifier">p</span> <span class="Operator">=</span> <span class="Identifier">initOptParser</span><span class="Punctuation">(</span><span class="StringLit">&quot;--left -r:2 -- foo.txt bar.txt&quot;</span><span class="Punctuation">)</span>
  342. <span class="Keyword">while</span> <span class="Identifier">true</span><span class="Punctuation">:</span>
  343. <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  344. <span class="Keyword">if</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">==</span> <span class="Identifier">cmdLongOption</span> <span class="Keyword">and</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">key</span> <span class="Operator">==</span> <span class="StringLit">&quot;&quot;</span><span class="Punctuation">:</span> <span class="Comment"># Look for &quot;--&quot;</span>
  345. <span class="Keyword">break</span>
  346. <span class="Identifier">doAssert</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">remainingArgs</span> <span class="Operator">==</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;foo.txt&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;bar.txt&quot;</span><span class="Punctuation">]</span></pre></p>
  347. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L413" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  348. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L413" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  349. </dd>
  350. </div>
  351. </div>
  352. </dl>
  353. </div>
  354. <div class="section" id="15">
  355. <h1><a class="toc-backref" href="#15">Iterators</a></h1>
  356. <dl class="item">
  357. <div id="getopt-iterators-all">
  358. <div id="getopt.i,seq[string],set[char],seq[string]">
  359. <dt><pre><span class="Keyword">iterator</span> <a href="#getopt.i%2Cseq%5Bstring%5D%2Cset%5Bchar%5D%2Cseq%5Bstring%5D"><span class="Identifier">getopt</span></a><span class="Other">(</span><span class="Identifier">cmdline</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><a href="system.html#string"><span class="Identifier">string</span></a><span class="Other">]</span> <span class="Other">=</span> <span class="Operator">@</span><span class="Other">[</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">shortNoVal</span><span class="Other">:</span> <a href="system.html#set"><span class="Identifier">set</span></a><span class="Other">[</span><a href="system.html#char"><span class="Identifier">char</span></a><span class="Other">]</span> <span class="Other">=</span> <span class="Other">{</span><span class="Other">}</span><span class="Other">;</span>
  360. <span class="Identifier">longNoVal</span><span class="Other">:</span> <a href="system.html#seq"><span class="Identifier">seq</span></a><span class="Other">[</span><a href="system.html#string"><span class="Identifier">string</span></a><span class="Other">]</span> <span class="Other">=</span> <span class="Operator">@</span><span class="Other">[</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Keyword">tuple</span><span class="Other">[</span><span class="Identifier">kind</span><span class="Other">:</span> <a href="parseopt.html#CmdLineKind"><span class="Identifier">CmdLineKind</span></a><span class="Other">,</span>
  361. <span class="Identifier">key</span><span class="Other">,</span> <span class="Identifier">val</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a><span class="Other">]</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">ReadIOEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  362. <dd>
  363. <p>Convenience iterator for iterating over command line arguments.</p>
  364. <p>This creates a new <a class="reference external" href="#OptParser">OptParser</a>. If no command line arguments are provided, the real command line as provided by the <tt class="docutils literal"><span class="pre"><span class="Identifier">os</span></span></tt> module is retrieved instead.</p>
  365. <p><tt class="docutils literal"><span class="pre"><span class="Identifier">shortNoVal</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">longNoVal</span></span></tt> are used to specify which options do not take values. See the <a class="reference external" href="#nimshortnoval-and-nimlongnoval">documentation about these parameters</a> for more information on how this affects parsing.</p>
  366. <p>There is no need to check for <tt class="docutils literal"><span class="pre"><span class="Identifier">cmdEnd</span></span></tt> while iterating. If using <tt class="docutils literal"><span class="pre"><span class="Identifier">getopt</span></span></tt> with case switching, checking for <tt class="docutils literal"><span class="pre"><span class="Identifier">cmdEnd</span></span></tt> is required.</p>
  367. <p>See also:</p>
  368. <ul class="simple"><li><a class="reference external" href="#initOptParser,seq[string],set[char],seq[string]">initOptParser proc</a></li>
  369. </ul>
  370. <p><strong>Examples:</strong></p>
  371. <p><pre class="listing"><span class="Comment"># these are placeholders, of course</span>
  372. <span class="Keyword">proc</span> <span class="Identifier">writeHelp</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">=</span> <span class="Keyword">discard</span>
  373. <span class="Keyword">proc</span> <span class="Identifier">writeVersion</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">=</span> <span class="Keyword">discard</span>
  374. <span class="Keyword">var</span> <span class="Identifier">filename</span><span class="Punctuation">:</span> <span class="Identifier">string</span>
  375. <span class="Keyword">let</span> <span class="Identifier">params</span> <span class="Operator">=</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">&quot;--left&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;--debug:3&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;-l&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;-r:2&quot;</span><span class="Punctuation">]</span>
  376. <span class="Keyword">for</span> <span class="Identifier">kind</span><span class="Punctuation">,</span> <span class="Identifier">key</span><span class="Punctuation">,</span> <span class="Identifier">val</span> <span class="Keyword">in</span> <span class="Identifier">getopt</span><span class="Punctuation">(</span><span class="Identifier">params</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
  377. <span class="Keyword">case</span> <span class="Identifier">kind</span>
  378. <span class="Keyword">of</span> <span class="Identifier">cmdArgument</span><span class="Punctuation">:</span>
  379. <span class="Identifier">filename</span> <span class="Operator">=</span> <span class="Identifier">key</span>
  380. <span class="Keyword">of</span> <span class="Identifier">cmdLongOption</span><span class="Punctuation">,</span> <span class="Identifier">cmdShortOption</span><span class="Punctuation">:</span>
  381. <span class="Keyword">case</span> <span class="Identifier">key</span>
  382. <span class="Keyword">of</span> <span class="StringLit">&quot;help&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;h&quot;</span><span class="Punctuation">:</span> <span class="Identifier">writeHelp</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  383. <span class="Keyword">of</span> <span class="StringLit">&quot;version&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;v&quot;</span><span class="Punctuation">:</span> <span class="Identifier">writeVersion</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  384. <span class="Keyword">of</span> <span class="Identifier">cmdEnd</span><span class="Punctuation">:</span> <span class="Identifier">assert</span><span class="Punctuation">(</span><span class="Identifier">false</span><span class="Punctuation">)</span> <span class="Comment"># cannot happen</span>
  385. <span class="Keyword">if</span> <span class="Identifier">filename</span> <span class="Operator">==</span> <span class="StringLit">&quot;&quot;</span><span class="Punctuation">:</span>
  386. <span class="Comment"># no filename has been written, so we show the help</span>
  387. <span class="Identifier">writeHelp</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></p>
  388. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L472" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  389. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L472" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  390. </dd>
  391. </div>
  392. <div id="getopt.i,OptParser">
  393. <dt><pre><span class="Keyword">iterator</span> <a href="#getopt.i%2COptParser"><span class="Identifier">getopt</span></a><span class="Other">(</span><span class="Identifier">p</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="parseopt.html#OptParser"><span class="Identifier">OptParser</span></a><span class="Other">)</span><span class="Other">:</span> <span class="Keyword">tuple</span><span class="Other">[</span><span class="Identifier">kind</span><span class="Other">:</span> <a href="parseopt.html#CmdLineKind"><span class="Identifier">CmdLineKind</span></a><span class="Other">,</span> <span class="Identifier">key</span><span class="Other">,</span> <span class="Identifier">val</span><span class="Other">:</span> <a href="system.html#string"><span class="Identifier">string</span></a><span class="Other">]</span> {.
  394. <span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
  395. <dd>
  396. <p>Convenience iterator for iterating over the given <a class="reference external" href="#OptParser">OptParser</a>.</p>
  397. <p>There is no need to check for <tt class="docutils literal"><span class="pre"><span class="Identifier">cmdEnd</span></span></tt> while iterating. If using <tt class="docutils literal"><span class="pre"><span class="Identifier">getopt</span></span></tt> with case switching, checking for <tt class="docutils literal"><span class="pre"><span class="Identifier">cmdEnd</span></span></tt> is required.</p>
  398. <p>See also:</p>
  399. <ul class="simple"><li><a class="reference external" href="#initOptParser,string,set[char],seq[string]">initOptParser proc</a></li>
  400. </ul>
  401. <p><strong>Examples:</strong></p>
  402. <p><pre class="listing"><span class="Comment"># these are placeholders, of course</span>
  403. <span class="Keyword">proc</span> <span class="Identifier">writeHelp</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">=</span> <span class="Keyword">discard</span>
  404. <span class="Keyword">proc</span> <span class="Identifier">writeVersion</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">=</span> <span class="Keyword">discard</span>
  405. <span class="Keyword">var</span> <span class="Identifier">filename</span><span class="Punctuation">:</span> <span class="Identifier">string</span>
  406. <span class="Keyword">var</span> <span class="Identifier">p</span> <span class="Operator">=</span> <span class="Identifier">initOptParser</span><span class="Punctuation">(</span><span class="StringLit">&quot;--left --debug:3 -l -r:2&quot;</span><span class="Punctuation">)</span>
  407. <span class="Keyword">for</span> <span class="Identifier">kind</span><span class="Punctuation">,</span> <span class="Identifier">key</span><span class="Punctuation">,</span> <span class="Identifier">val</span> <span class="Keyword">in</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">getopt</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
  408. <span class="Keyword">case</span> <span class="Identifier">kind</span>
  409. <span class="Keyword">of</span> <span class="Identifier">cmdArgument</span><span class="Punctuation">:</span>
  410. <span class="Identifier">filename</span> <span class="Operator">=</span> <span class="Identifier">key</span>
  411. <span class="Keyword">of</span> <span class="Identifier">cmdLongOption</span><span class="Punctuation">,</span> <span class="Identifier">cmdShortOption</span><span class="Punctuation">:</span>
  412. <span class="Keyword">case</span> <span class="Identifier">key</span>
  413. <span class="Keyword">of</span> <span class="StringLit">&quot;help&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;h&quot;</span><span class="Punctuation">:</span> <span class="Identifier">writeHelp</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  414. <span class="Keyword">of</span> <span class="StringLit">&quot;version&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;v&quot;</span><span class="Punctuation">:</span> <span class="Identifier">writeVersion</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
  415. <span class="Keyword">of</span> <span class="Identifier">cmdEnd</span><span class="Punctuation">:</span> <span class="Identifier">assert</span><span class="Punctuation">(</span><span class="Identifier">false</span><span class="Punctuation">)</span> <span class="Comment"># cannot happen</span>
  416. <span class="Keyword">if</span> <span class="Identifier">filename</span> <span class="Operator">==</span> <span class="StringLit">&quot;&quot;</span><span class="Punctuation">:</span>
  417. <span class="Comment"># no filename has been given, so we show the help</span>
  418. <span class="Identifier">writeHelp</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></p>
  419. <a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L431" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
  420. <a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L431" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
  421. </dd>
  422. </div>
  423. </div>
  424. </dl>
  425. </div>
  426. </div>
  427. </div>
  428. <div class="twelve-columns footer">
  429. <span class="nim-sprite"></span>
  430. <br>
  431. <small style="color: var(--hint);">Made with Nim. Generated: 2025-02-06 22:28:12 UTC</small>
  432. </div>
  433. </div>
  434. </div>
  435. <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
  436. </body>
  437. </html>