Files
Nim/parseopt.html
github-actions[bot] de413ab6bb Deploy to GitHub pages
2025-12-25 20:14:24 +00:00

509 lines
65 KiB
HTML

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- This file is generated by Nim. -->
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>std/parseopt</title>
<!-- Google fonts -->
<link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
<!-- Favicon -->
<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=="/>
<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=">
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
<!-- JS -->
<script type="text/javascript" src="dochack.js?v=2.3.1"></script>
</head>
<body>
<div class="document" id="documentId">
<div class="container">
<h1 class="title">std/parseopt</h1>
<div class="row">
<div class="three columns">
<div class="theme-select-wrapper">
<label for="theme-select">Theme:&nbsp;</label>
<select id="theme-select" onchange="setTheme(this.value)">
<option value="auto">🌗 Match OS</option>
<option value="dark">🌑 Dark</option>
<option value="light">🌕 Light</option>
</select>
</div>
<div id="global-links">
<ul class="simple-boot">
<li><a href="manual.html">Manual</a></li>
<li><a href="lib.html">Standard library</a></li>
<li> <a id="indexLink" href="theindex.html">Index</a></li>
<li><a href="compiler/theindex.html">Compiler docs</a></li>
<li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
<li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
</ul>
</div>
<div id="searchInputDiv">
Search: <input type="search" id="searchInput"
oninput="search()" />
</div>
<ul class="simple simple-toc" id="toc-list">
<li><a class="reference" id="supported-syntax_toc" href="#supported-syntax">Supported Syntax</a></li>
<li><a class="reference" id="parsing_toc" href="#parsing">Parsing</a></li>
<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>
<li><a class="reference" id="see-also_toc" href="#see-also">See also</a></li>
<li>
<a class="reference reference-toplevel" href="#6" id="56">Imports</a>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#7" id="57">Types</a></summary>
<ul class="simple simple-toc-section">
<li><a class="reference" href="#CmdLineKind" title="CmdLineKind = enum
cmdEnd, ## End of command line reached
cmdArgument, ## An argument such as a filename
cmdLongOption, ## A long option such as --option
cmdShortOption ## A short option such as -c">CmdLineKind</a></li>
<li><a class="reference" href="#OptParser" title="OptParser = object of RootObj
kind*: CmdLineKind ## The detected command line token
key*, val*: string ## Key and value pair; the key is the option
## or the argument, and the value is not &quot;&quot; if
## the option was given a value">OptParser</a></li>
</ul>
</details>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#12" id="62">Procs</a></summary>
<ul class="simple simple-toc-section">
<ul class="simple nested-toc-section">cmdLineRest
<li><a class="reference" href="#cmdLineRest%2COptParser" title="cmdLineRest(p: OptParser): string">cmdLineRest(p: OptParser): string</a></li>
</ul>
<ul class="simple nested-toc-section">initOptParser
<li><a class="reference" href="#initOptParser%2Cstring%2Cset%5Bchar%5D%2Cseq%5Bstring%5D" title="initOptParser(cmdline = &quot;&quot;; shortNoVal: set[char] = {};
longNoVal: seq[string] = @[]; allowWhitespaceAfterColon = true): OptParser">initOptParser(cmdline = &quot;&quot;; shortNoVal: set[char] = {};
longNoVal: seq[string] = @[]; allowWhitespaceAfterColon = true): OptParser</a></li>
<li><a class="reference" href="#initOptParser%2Cseq%5Bstring%5D%2Cset%5Bchar%5D%2Cseq%5Bstring%5D" title="initOptParser(cmdline: seq[string]; shortNoVal: set[char] = {};
longNoVal: seq[string] = @[]; allowWhitespaceAfterColon = true): OptParser">initOptParser(cmdline: seq[string]; shortNoVal: set[char] = {};
longNoVal: seq[string] = @[]; allowWhitespaceAfterColon = true): OptParser</a></li>
</ul>
<ul class="simple nested-toc-section">next
<li><a class="reference" href="#next%2COptParser" title="next(p: var OptParser)">next(p: var OptParser)</a></li>
</ul>
<ul class="simple nested-toc-section">remainingArgs
<li><a class="reference" href="#remainingArgs%2COptParser" title="remainingArgs(p: OptParser): seq[string]">remainingArgs(p: OptParser): seq[string]</a></li>
</ul>
</ul>
</details>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#15" id="65">Iterators</a></summary>
<ul class="simple simple-toc-section">
<ul class="simple nested-toc-section">getopt
<li><a class="reference" href="#getopt.i%2Cseq%5Bstring%5D%2Cset%5Bchar%5D%2Cseq%5Bstring%5D" title="getopt(cmdline: seq[string] = @[]; shortNoVal: set[char] = {};
longNoVal: seq[string] = @[]): tuple[kind: CmdLineKind, key, val: string]">getopt(cmdline: seq[string] = @[]; shortNoVal: set[char] = {};
longNoVal: seq[string] = @[]): tuple[kind: CmdLineKind, key, val: string]</a></li>
<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>
</ul>
</ul>
</details>
</li>
</ul>
</div>
<div class="nine columns" id="content">
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L1" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L1" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
<div id="tocRoot"></div>
<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.
<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>
<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>
<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>
<li>Arguments: everything that does not start with a <tt class="docutils literal"><span class="pre"><span class="Operator">-</span></span></tt></li>
</ol>
<p>These three kinds of tokens are enumerated in the <a class="reference external" href="#CmdLineKind">CmdLineKind enum</a>.</p>
<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>
<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>
<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>
<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>
<p>Here is an example:</p>
<p><pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">parseopt</span>
<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>
<span class="Keyword">while</span> <span class="Identifier">true</span><span class="Punctuation">:</span>
<span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<span class="Keyword">case</span> <span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">kind</span>
<span class="Keyword">of</span> <span class="Identifier">cmdEnd</span><span class="Punctuation">:</span> <span class="Keyword">break</span>
<span class="Keyword">of</span> <span class="Identifier">cmdShortOption</span><span class="Punctuation">,</span> <span class="Identifier">cmdLongOption</span><span class="Punctuation">:</span>
<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>
<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>
<span class="Keyword">else</span><span class="Punctuation">:</span>
<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>
<span class="Keyword">of</span> <span class="Identifier">cmdArgument</span><span class="Punctuation">:</span>
<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>
<span class="Comment"># Output:</span>
<span class="Comment"># Option: a</span>
<span class="Comment"># Option: b</span>
<span class="Comment"># Option and value: e, 5</span>
<span class="Comment"># Option: foo</span>
<span class="Comment"># Option and value: bar, 20</span>
<span class="Comment"># Argument: file.txt</span></pre></p>
<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>
<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>
<p>Here is an example:</p>
<p><pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">parseopt</span>
<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>
<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>
<span class="Keyword">case</span> <span class="Identifier">kind</span>
<span class="Keyword">of</span> <span class="Identifier">cmdArgument</span><span class="Punctuation">:</span>
<span class="Keyword">discard</span>
<span class="Keyword">of</span> <span class="Identifier">cmdLongOption</span><span class="Punctuation">,</span> <span class="Identifier">cmdShortOption</span><span class="Punctuation">:</span>
<span class="Keyword">case</span> <span class="Identifier">key</span><span class="Punctuation">:</span>
<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>
<span class="Identifier">varName</span> <span class="Operator">=</span> <span class="Identifier">val</span> <span class="Comment"># do input sanitization in production systems</span>
<span class="Keyword">of</span> <span class="Identifier">cmdEnd</span><span class="Punctuation">:</span>
<span class="Keyword">discard</span></pre></p>
<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>
<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>
<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>
<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>
<p><pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">parseopt</span>
<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>
<span class="Keyword">case</span> <span class="Identifier">kind</span>
<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>
<span class="Keyword">of</span> <span class="Identifier">cmdShortOption</span><span class="Punctuation">,</span> <span class="Identifier">cmdLongOption</span><span class="Punctuation">:</span>
<span class="Keyword">if</span> <span class="Identifier">val</span> <span class="Operator">==</span> <span class="StringLit">&quot;&quot;</span><span class="Punctuation">:</span>
<span class="Identifier">echo</span> <span class="StringLit">&quot;Option: &quot;</span><span class="Punctuation">,</span> <span class="Identifier">key</span>
<span class="Keyword">else</span><span class="Punctuation">:</span>
<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>
<span class="Keyword">of</span> <span class="Identifier">cmdArgument</span><span class="Punctuation">:</span>
<span class="Identifier">echo</span> <span class="StringLit">&quot;Argument: &quot;</span><span class="Punctuation">,</span> <span class="Identifier">key</span>
<span class="Keyword">let</span> <span class="Identifier">cmdLine</span> <span class="Operator">=</span> <span class="StringLit">&quot;-j4 --first bar&quot;</span>
<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>
<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>
<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>
<span class="Comment"># Output:</span>
<span class="Comment"># Option: j</span>
<span class="Comment"># Option: 4</span>
<span class="Comment"># Option: first</span>
<span class="Comment"># Argument: bar</span>
<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>
<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>
<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>
<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>
<span class="Comment"># Output:</span>
<span class="Comment"># Option and value: j, 4</span>
<span class="Comment"># Option and value: first, bar</span></pre></p>
<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>
<li><a class="reference external" href="parseutils.html">parseutils module</a> for helpers that parse tokens, numbers, identifiers, etc.</li>
<li><a class="reference external" href="strutils.html">strutils module</a> for common string handling operations</li>
<li><a class="reference external" href="json.html">json module</a> for a JSON parser</li>
<li><a class="reference external" href="parsecfg.html">parsecfg module</a> for a configuration file parser</li>
<li><a class="reference external" href="parsecsv.html">parsecsv module</a> for a simple CSV (comma separated value) parser</li>
<li><a class="reference external" href="parsexml.html">parsexml module</a> for a XML / HTML parser</li>
<li><a class="reference external" href="lib.html#pure-libraries-parsers">other parsers</a> for more parsers</li>
</ul>
</p>
<div class="section" id="6">
<h1><a class="toc-backref" href="#6">Imports</a></h1>
<dl class="item">
<a class="reference external" href="strutils.html">strutils</a>, <a class="reference external" href="os.html">os</a>
</dl>
</div>
<div class="section" id="7">
<h1><a class="toc-backref" href="#7">Types</a></h1>
<dl class="item">
<div id="CmdLineKind">
<dt><pre><a href="parseopt.html#CmdLineKind"><span class="Identifier">CmdLineKind</span></a> <span class="Other">=</span> <span class="Keyword">enum</span>
<span class="Identifier">cmdEnd</span><span class="Other">,</span> <span class="Comment">## End of command line reached</span>
<span class="Identifier">cmdArgument</span><span class="Other">,</span> <span class="Comment">## An argument such as a filename</span>
<span class="Identifier">cmdLongOption</span><span class="Other">,</span> <span class="Comment">## A long option such as --option</span>
<span class="Identifier">cmdShortOption</span> <span class="Comment">## A short option such as -c</span></pre></dt>
<dd>
The detected command line token.
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L183" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L183" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
<div id="OptParser">
<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>
<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>
<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>
<span class="Comment">## or the argument, and the value is not &quot;&quot; if</span>
<span class="Comment">## the option was given a value</span></pre></dt>
<dd>
<p>
Implementation of the command line parser.</p>
<p>To initialize it, use the <a class="reference external" href="#initOptParser,string,set[char],seq[string]">initOptParser proc</a>.</p>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L188" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L188" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</dl>
</div>
<div class="section" id="12">
<h1><a class="toc-backref" href="#12">Procs</a></h1>
<dl class="item">
<div id="cmdLineRest-procs-all">
<div id="cmdLineRest,OptParser">
<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>
<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>
<dd>
<p>Retrieves the rest of the command line that has not been parsed yet.</p>
<p>See also:</p>
<ul class="simple"><li><a class="reference external" href="#remainingArgs,OptParser">remainingArgs proc</a></li>
</ul>
<p><strong>Examples:</strong></p>
<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>
<span class="Keyword">while</span> <span class="Identifier">true</span><span class="Punctuation">:</span>
<span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<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>
<span class="Keyword">break</span>
<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>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L396" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L396" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="initOptParser-procs-all">
<div id="initOptParser,string,set[char],seq[string]">
<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>
<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="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>
<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>
<dd>
<p>Initializes the command line parser.</p>
<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>
<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>
<p>This does not provide a way of passing default values to arguments.</p>
<p>See also:</p>
<ul class="simple"><li><a class="reference external" href="#getopt.i,OptParser">getopt iterator</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></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="Punctuation">)</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>
<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>
<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>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L273" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L273" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
<div id="initOptParser,seq[string],set[char],seq[string]">
<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>
<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="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>
<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>
<dd>
<p>Initializes the command line parser.</p>
<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>
<p>See also:</p>
<ul class="simple"><li><a class="reference external" href="#getopt.i,seq[string],set[char],seq[string]">getopt iterator</a></li>
</ul>
<p><strong class="examples_text">Example:</strong></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="Punctuation">)</span>
<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>
<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>
<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>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L221" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L221" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="next-procs-all">
<div id="next,OptParser">
<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>
<span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
<dd>
<p>Parses the next token.</p>
<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>
<p><strong class="examples_text">Example:</strong></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 file.txt&quot;</span><span class="Punctuation">)</span>
<span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<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>
<span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<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>
<span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<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>
<span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<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>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L325" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L325" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="remainingArgs-procs-all">
<div id="remainingArgs,OptParser">
<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>
<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>
<dd>
<p>Retrieves a sequence of the arguments that have not been parsed yet.</p>
<p>See also:</p>
<ul class="simple"><li><a class="reference external" href="#cmdLineRest,OptParser">cmdLineRest proc</a></li>
</ul>
<p><strong>Examples:</strong></p>
<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>
<span class="Keyword">while</span> <span class="Identifier">true</span><span class="Punctuation">:</span>
<span class="Identifier">p</span><span class="Operator">.</span><span class="Identifier">next</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
<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>
<span class="Keyword">break</span>
<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>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L413" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L413" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
</dl>
</div>
<div class="section" id="15">
<h1><a class="toc-backref" href="#15">Iterators</a></h1>
<dl class="item">
<div id="getopt-iterators-all">
<div id="getopt.i,seq[string],set[char],seq[string]">
<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>
<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>
<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>
<dd>
<p>Convenience iterator for iterating over command line arguments.</p>
<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>
<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>
<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>
<p>See also:</p>
<ul class="simple"><li><a class="reference external" href="#initOptParser,seq[string],set[char],seq[string]">initOptParser proc</a></li>
</ul>
<p><strong>Examples:</strong></p>
<p><pre class="listing"><span class="Comment"># these are placeholders, of course</span>
<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>
<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>
<span class="Keyword">var</span> <span class="Identifier">filename</span><span class="Punctuation">:</span> <span class="Identifier">string</span>
<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>
<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>
<span class="Keyword">case</span> <span class="Identifier">kind</span>
<span class="Keyword">of</span> <span class="Identifier">cmdArgument</span><span class="Punctuation">:</span>
<span class="Identifier">filename</span> <span class="Operator">=</span> <span class="Identifier">key</span>
<span class="Keyword">of</span> <span class="Identifier">cmdLongOption</span><span class="Punctuation">,</span> <span class="Identifier">cmdShortOption</span><span class="Punctuation">:</span>
<span class="Keyword">case</span> <span class="Identifier">key</span>
<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>
<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>
<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>
<span class="Keyword">if</span> <span class="Identifier">filename</span> <span class="Operator">==</span> <span class="StringLit">&quot;&quot;</span><span class="Punctuation">:</span>
<span class="Comment"># no filename has been written, so we show the help</span>
<span class="Identifier">writeHelp</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></p>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L472" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L472" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
<div id="getopt.i,OptParser">
<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> {.
<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>
<dd>
<p>Convenience iterator for iterating over the given <a class="reference external" href="#OptParser">OptParser</a>.</p>
<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>
<p>See also:</p>
<ul class="simple"><li><a class="reference external" href="#initOptParser,string,set[char],seq[string]">initOptParser proc</a></li>
</ul>
<p><strong>Examples:</strong></p>
<p><pre class="listing"><span class="Comment"># these are placeholders, of course</span>
<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>
<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>
<span class="Keyword">var</span> <span class="Identifier">filename</span><span class="Punctuation">:</span> <span class="Identifier">string</span>
<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>
<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>
<span class="Keyword">case</span> <span class="Identifier">kind</span>
<span class="Keyword">of</span> <span class="Identifier">cmdArgument</span><span class="Punctuation">:</span>
<span class="Identifier">filename</span> <span class="Operator">=</span> <span class="Identifier">key</span>
<span class="Keyword">of</span> <span class="Identifier">cmdLongOption</span><span class="Punctuation">,</span> <span class="Identifier">cmdShortOption</span><span class="Punctuation">:</span>
<span class="Keyword">case</span> <span class="Identifier">key</span>
<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>
<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>
<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>
<span class="Keyword">if</span> <span class="Identifier">filename</span> <span class="Operator">==</span> <span class="StringLit">&quot;&quot;</span><span class="Punctuation">:</span>
<span class="Comment"># no filename has been given, so we show the help</span>
<span class="Identifier">writeHelp</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></p>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/parseopt.nim#L431" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/parseopt.nim#L431" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
</dl>
</div>
</div>
</div>
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br>
<small style="color: var(--hint);">Made with Nim. Generated: 2025-12-25 20:13:12 UTC</small>
</div>
</div>
</div>
<script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
</body>
</html>