1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
<!DOCTYPE html>
<html lang="en" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>fish_opt - create an option specification for the argparse command — fish-shell 4.2.1 documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=4da8bbd6" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=f89b4716" />
<script src="../_static/documentation_options.js?v=6fb65176"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="fish_prompt - define the appearance of the command line prompt" href="fish_prompt.html" />
<link rel="prev" title="fish_mode_prompt - define the appearance of the mode indicator" href="fish_mode_prompt.html" />
<link rel="shortcut icon" type="image/png" href="../_static/fish.png" />
</head><body><div id="fmain">
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li><img src="../_static/fish.png" alt=""
style="width: 80px; height: 80px; vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://fishshell.com/">fish-shell</a> »</li>
<a href="../index.html">fish-shell 4.2.1 documentation</a> »
<li class="nav-item nav-item-1"><a href="../commands.html" accesskey="U">Commands</a> »</li>
<li class="nav-item nav-item-this"><a href="">fish_opt - create an option specification for the argparse command</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</li>
<div id="old-docs-notice" style="display: none">
This documents an old version of fish.
<a href="../../current/">See the latest release.</a>
</div>
</ul>
</div>
<div class="document">
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<div>
<h3><a href="../index.html">Documents</a></h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../index.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">Frequently asked questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../interactive.html">Interactive use</a></li>
<li class="toctree-l1"><a class="reference internal" href="../language.html">The fish language</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../commands.html">Commands</a></li>
<li class="toctree-l1"><a class="reference internal" href="../fish_for_bash_users.html">Fish for bash users</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../completions.html">Writing your own completions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../prompt.html">Writing your own prompt</a></li>
<li class="toctree-l1"><a class="reference internal" href="../design.html">Design</a></li>
<li class="toctree-l1"><a class="reference internal" href="../relnotes.html">Release notes</a></li>
<li class="toctree-l1"><a class="reference internal" href="../terminal-compatibility.html">Terminal Compatibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributing.html">Contributing To Fish</a></li>
<li class="toctree-l1"><a class="reference internal" href="../license.html">License</a></li>
</ul>
</div>
<search id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
<div>
<h4><a href="../index.html">Sections</a></h4>
<ul>
<li><a class="reference internal" href="#">fish_opt - create an option specification for the argparse command</a><ul>
<li><a class="reference internal" href="#synopsis">Synopsis</a></li>
<li><a class="reference internal" href="#description">Description</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="fish-opt-create-an-option-specification-for-the-argparse-command">
<h1>fish_opt - create an option specification for the argparse command<a class="headerlink" href="#fish-opt-create-an-option-specification-for-the-argparse-command" title="Link to this heading">¶</a></h1>
<section id="synopsis">
<h2>Synopsis<a class="headerlink" href="#synopsis" title="Link to this heading">¶</a></h2>
<div class="highlight-fish-synopsis notranslate"><div class="highlight"><pre><span></span><span class="nf">fish_opt</span><span class="w"> </span><span class="g g-Other">[</span><span class="no">-s</span><span class="w"> </span><span class="no">ALPHANUM</span><span class="g g-Other">]</span><span class="w"> </span><span class="g g-Other">[</span><span class="no">-l</span><span class="w"> </span><span class="no">LONG-NAME</span><span class="g g-Other">]</span><span class="w"> </span><span class="g g-Other">[</span><span class="no">-ormd</span><span class="g g-Other">]</span><span class="w"> </span><span class="g g-Other">[</span><span class="no">--long-only</span><span class="g g-Other">]</span><span class="w"> </span><span class="g g-Other">[</span><span class="no">-v</span><span class="w"> </span><span class="no">COMMAND</span><span class="w"> </span><span class="no">OPTIONS</span><span class="w"> </span><span class="g g-Other">...</span><span class="no"> ]</span>
<span class="nf">fish_opt</span><span class="w"> </span><span class="no">--help</span>
</pre></div>
</div>
</section>
<section id="description">
<h2>Description<a class="headerlink" href="#description" title="Link to this heading">¶</a></h2>
<p>This command provides a way to produce option specifications suitable for use with the <a class="reference internal" href="argparse.html"><span class="doc">argparse</span></a> command. You can, of course, write the option specifications by hand without using this command. But you might prefer to use this for the clarity it provides.</p>
<p>The following <code class="docutils literal notranslate"><span class="pre">argparse</span></code> options are available:</p>
<dl class="simple">
<dt><strong>-s</strong> or <strong>--short</strong> <em>ALPHANUM</em></dt><dd><p>Takes a single letter or number that is used as the short flag in the option being defined. Either this option or the <strong>--long</strong> option must be provided.</p>
</dd>
<dt><strong>-l</strong> or <strong>--long</strong> <em>LONG-NAME</em></dt><dd><p>Takes a string that is used as the long flag in the option being defined. This option is optional and has no default. If no long flag is defined then only the short flag will be allowed when parsing arguments using the option specification.</p>
</dd>
<dt><strong>--long-only</strong></dt><dd><p>Deprecated. The option being defined will only allow the long flag name to be used, even if the short flag is defined (i.e., <strong>--short</strong> is specified).</p>
</dd>
<dt><strong>-o</strong> or <strong>--optional-val</strong></dt><dd><p>The option being defined can take a value, but it is optional rather than required. If the option is seen more than once when parsing arguments, only the last value seen is saved. This means the resulting flag variable created by <code class="docutils literal notranslate"><span class="pre">argparse</span></code> will zero elements if no value was given with the option else it will have exactly one element.</p>
</dd>
<dt><strong>-r</strong> or <strong>--required-val</strong></dt><dd><p>The option being defined requires a value. If the option is seen more than once when parsing arguments, only the last value seen is saved. This means the resulting flag variable created by <code class="docutils literal notranslate"><span class="pre">argparse</span></code> will have exactly one element.</p>
</dd>
<dt><strong>-m</strong> or <strong>--multiple-vals</strong></dt><dd><p>The value of each instance of the option is accumulated. If <strong>--optional-val</strong> is provided, the value is optional, and an empty string is stored if no value is provided. Otherwise, the <strong>--requiured-val</strong> option is implied and each instance of the option requires a value. This means the resulting flag variable created by <code class="docutils literal notranslate"><span class="pre">argparse</span></code> will have one element for each instance of this option in the arguments, even for instances that did not provide a value.</p>
</dd>
<dt><strong>-d</strong> or <strong>--delete</strong></dt><dd><p>The option and any values will be deleted from the <code class="docutils literal notranslate"><span class="pre">$argv_opts</span></code> variables set by <code class="docutils literal notranslate"><span class="pre">argparse</span></code>
(as with other options, it will also be deleted from <code class="docutils literal notranslate"><span class="pre">$argv</span></code>).</p>
</dd>
<dt><strong>-v</strong> or <strong>--validate</strong> <em>COMMAND</em> <em>OPTION…</em></dt><dd><p>This option must be the last one, and requires one of <code class="docutils literal notranslate"><span class="pre">-o</span></code>, <code class="docutils literal notranslate"><span class="pre">-r</span></code>, or <code class="docutils literal notranslate"><span class="pre">-m</span></code>. All the remaining arguments are interpreted a fish script to run to validate the value of the argument, see <code class="docutils literal notranslate"><span class="pre">argparse</span></code> documentation for more details. Note that the interpretation of <em>COMMAND</em> <em>OPTION…</em> is similar to <code class="docutils literal notranslate"><span class="pre">eval</span></code>, so you may need to quote or escape special characters <em>twice</em> if you want them to be interpreted literally when the validate script is run.</p>
</dd>
<dt><strong>-h</strong> or <strong>--help</strong></dt><dd><p>Displays help about using this command.</p>
</dd>
</dl>
</section>
<section id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
<p>Define a single option specification for the boolean help flag:</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">set</span><span class="w"> </span><span class="no">-l</span><span class="w"> </span><span class="no">options</span><span class="w"> </span><span class="o">(</span><span class="nf">fish_opt</span><span class="w"> </span><span class="no">-s</span><span class="w"> </span><span class="no">h</span><span class="w"> </span><span class="no">-l</span><span class="w"> </span><span class="no">help</span><span class="o">)</span>
<span class="nf">argparse</span><span class="w"> </span><span class="o">$options</span><span class="w"> </span><span class="no">--</span><span class="w"> </span><span class="o">$argv</span>
</pre></div>
</div>
<p>Same as above but with a second flag that requires a value:</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">set</span><span class="w"> </span><span class="no">-l</span><span class="w"> </span><span class="no">options</span><span class="w"> </span><span class="o">(</span><span class="nf">fish_opt</span><span class="w"> </span><span class="no">-s</span><span class="w"> </span><span class="no">h</span><span class="w"> </span><span class="no">-l</span><span class="w"> </span><span class="no">help</span><span class="o">)</span>
<span class="nf">set</span><span class="w"> </span><span class="no">options</span><span class="w"> </span><span class="o">$options</span><span class="w"> </span><span class="o">(</span><span class="nf">fish_opt</span><span class="w"> </span><span class="no">-s</span><span class="w"> </span><span class="no">m</span><span class="w"> </span><span class="no">-l</span><span class="w"> </span><span class="no">max</span><span class="w"> </span><span class="no">-r</span><span class="o">)</span>
<span class="nf">argparse</span><span class="w"> </span><span class="o">$options</span><span class="w"> </span><span class="no">--</span><span class="w"> </span><span class="o">$argv</span>
</pre></div>
</div>
<p>Same as above but the value of the second flag cannot be the empty string:</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">set</span><span class="w"> </span><span class="no">-l</span><span class="w"> </span><span class="no">options</span><span class="w"> </span><span class="o">(</span><span class="nf">fish_opt</span><span class="w"> </span><span class="no">-s</span><span class="w"> </span><span class="no">h</span><span class="w"> </span><span class="no">-l</span><span class="w"> </span><span class="no">help</span><span class="o">)</span>
<span class="nf">set</span><span class="w"> </span><span class="no">options</span><span class="w"> </span><span class="o">$options</span><span class="w"> </span><span class="o">(</span><span class="nf">fish_opt</span><span class="w"> </span><span class="no">-s</span><span class="w"> </span><span class="no">m</span><span class="w"> </span><span class="no">-l</span><span class="w"> </span><span class="no">max</span><span class="w"> </span><span class="no">-rv</span><span class="w"> </span><span class="no">test</span><span class="w"> </span><span class="se">\$</span><span class="no">_flag_valu</span><span class="w"> </span><span class="no">!=</span><span class="w"> </span><span class="s2">"''"</span><span class="o">)</span>
<span class="nf">argparse</span><span class="w"> </span><span class="o">$options</span><span class="w"> </span><span class="no">--</span><span class="w"> </span><span class="o">$argv</span>
</pre></div>
</div>
<p>Same as above but with a third flag that can be given multiple times saving the value of each instance seen and only a long flag name (<code class="docutils literal notranslate"><span class="pre">--token</span></code>) is defined:</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">set</span><span class="w"> </span><span class="no">-l</span><span class="w"> </span><span class="no">options</span><span class="w"> </span><span class="o">(</span><span class="nf">fish_opt</span><span class="w"> </span><span class="no">--short=h</span><span class="w"> </span><span class="no">--long=help</span><span class="o">)</span>
<span class="nf">set</span><span class="w"> </span><span class="no">options</span><span class="w"> </span><span class="o">$options</span><span class="w"> </span><span class="o">(</span><span class="nf">fish_opt</span><span class="w"> </span><span class="no">--short=m</span><span class="w"> </span><span class="no">--long=max</span><span class="w"> </span><span class="no">--required-val</span><span class="w"> </span><span class="no">--validate</span><span class="w"> </span><span class="no">test</span><span class="w"> </span><span class="se">\$</span><span class="no">_flag_valu</span><span class="w"> </span><span class="no">!=</span><span class="w"> </span><span class="s2">"''"</span><span class="o">)</span>
<span class="nf">set</span><span class="w"> </span><span class="no">options</span><span class="w"> </span><span class="o">$options</span><span class="w"> </span><span class="o">(</span><span class="nf">fish_opt</span><span class="w"> </span><span class="no">--long=token</span><span class="w"> </span><span class="no">--multiple-vals</span><span class="o">)</span>
<span class="nf">argparse</span><span class="w"> </span><span class="o">$options</span><span class="w"> </span><span class="no">--</span><span class="w"> </span><span class="o">$argv</span>
</pre></div>
</div>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li><img src="../_static/fish.png" alt=""
style="width: 80px; height: 80px; vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://fishshell.com/">fish-shell</a> »</li>
<a href="../index.html">fish-shell 4.2.1 documentation</a> »
<li class="nav-item nav-item-1"><a href="../commands.html" >Commands</a> »</li>
<li class="nav-item nav-item-this"><a href="">fish_opt - create an option specification for the argparse command</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</li>
<div id="old-docs-notice" style="display: none">
This documents an old version of fish.
<a href="../../current/">See the latest release.</a>
</div>
</ul>
</div>
<div class="footer">
© Copyright fish-shell developers.
<br />
<a href="https://github.com/fish-shell/fish-shell/issues">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.
</div>
</div>
<script type="text/javascript">
FISH_DOCS_VERSION = "4.2";
function copy_to_clipboard(it) {
// Find the pre tag we're interested in.
var pre = it.target;
while (pre.tagName != "PRE") pre = pre.parentNode;
var txt = "";
// Cheesy: If we have a prompt,
// we only copy prompted lines,
// by splitting and matching and stuff
if (pre.querySelector('span.gp')) {
var texts= [];
for (var line of pre.innerText.split('\n')) {
if (line.match(/^>_?.*/)) {
texts.push(line.replace(/^>_?/, ""));
}
}
txt = texts.join("\n");
} else {
// Even cheesier: If we don't have a prompt, we remove the button text from the end.
var txt = pre.innerText.substring(0, pre.innerText.length - it.target.innerText.length).trim();
}
navigator.clipboard.writeText(txt).then(function() {
// Success - set the text to indicate it,
// then set it back after 2 seconds.
var span = pre.querySelector("button span");
if (span) {
var oldText = span.innerText;
span.innerText = "COPIED!";
setTimeout(function() {
span.innerText = oldText;
}, 2000);
}
}, function() {
});
}
(function () {
// Add copy buttons to all the codeblocks.
var codeblocks = document.querySelectorAll('div > pre');
var button = document.createElement('button');
var span = document.createElement('span');
span.innerText = "COPY";
button.appendChild(span);
for (var i of codeblocks) {
var newButton = button.cloneNode(true);
newButton.addEventListener('click', copy_to_clipboard);
i.appendChild(newButton);
}
})();
</script>
</body>
</html>
|