mentat/docs/apis/rust/getopts/index.html

258 lines
15 KiB
HTML
Raw Normal View History

2018-06-21 14:44:35 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="rustdoc">
<meta name="description" content="API documentation for the Rust `getopts` crate.">
<meta name="keywords" content="rust, rustlang, rust-lang, getopts">
<title>getopts - Rust</title>
<link rel="stylesheet" type="text/css" href="../normalize.css">
<link rel="stylesheet" type="text/css" href="../rustdoc.css"
id="mainThemeStyle">
<link rel="stylesheet" type="text/css" href="../dark.css">
<link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle">
<script src="../storage.js"></script>
<link rel="shortcut icon" href="https://www.rust-lang.org/favicon.ico">
</head>
<body class="rustdoc mod">
<!--[if lte IE 8]>
<div class="warning">
This old browser is unsupported and will most likely display funky
things.
</div>
<![endif]-->
<nav class="sidebar">
<div class="sidebar-menu">&#9776;</div>
<a href='../getopts/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a>
<p class='location'>Crate getopts</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#types">Type Definitions</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'getopts', ty: 'mod', relpath: '../'};</script></div>
</nav>
<div class="theme-picker">
<button id="theme-picker" aria-label="Pick another theme!">
<img src="../brush.svg" width="18" alt="Pick another theme!">
</button>
<div id="theme-choices"></div>
</div>
<script src="../theme.js"></script>
<nav class="sub">
<form class="search-form js-only">
<div class="search-container">
<input class="search-input" name="search"
autocomplete="off"
placeholder="Click or press S to search, ? for more options…"
type="search">
</div>
</form>
</nav>
<section id='main' class="content"><h1 class='fqn'><span class='in-band'>Crate <a class="mod" href=''>getopts</a></span><span class='out-of-band'><span id='render-detail'>
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
[<span class='inner'>&#x2212;</span>]
</a>
</span><a class='srclink' href='../src/getopts/lib.rs.html#13-2009' title='goto source code'>[src]</a></span></h1><div class='docblock'><p>Simple getopt alternative.</p>
<p>Construct a vector of options, either by using <code>reqopt</code>, <code>optopt</code>, and
<code>optflag</code> or by building them from components yourself, and pass them to
<code>getopts</code>, along with a vector of actual arguments (not including
<code>argv[0]</code>). You'll either get a failure code back, or a match. You'll have
to verify whether the amount of 'free' arguments in the match is what you
expect. Use <code>opt_*</code> accessors to get argument values out of the matches
object.</p>
<p>Single-character options are expected to appear on the command line with a
single preceding dash; multiple-character options are expected to be
proceeded by two dashes. Options that expect an argument accept their
argument following either a space or an equals sign. Single-character
options don't require the space.</p>
<h1 id="usage" class="section-header"><a href="#usage">Usage</a></h1>
<p>This crate is <a href="https://crates.io/crates/getopts">on crates.io</a> and can be
used by adding <code>getopts</code> to the dependencies in your project's <code>Cargo.toml</code>.</p>
<pre><code class="language-toml">[dependencies]
getopts = &quot;0.2&quot;
</code></pre>
<p>and this to your crate root:</p>
<pre class="rust rust-example-rendered">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">getopts</span>;</pre>
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
<p>The following example shows simple command line parsing for an application
that requires an input file to be specified, accepts an optional output file
name following <code>-o</code>, and accepts both <code>-h</code> and <code>--help</code> as optional flags.</p>
<pre class="rust rust-example-rendered">
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">getopts</span>;
<span class="kw">use</span> <span class="ident">getopts</span>::<span class="ident">Options</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">env</span>;
<span class="kw">fn</span> <span class="ident">do_work</span>(<span class="ident">inp</span>: <span class="kw-2">&amp;</span><span class="ident">str</span>, <span class="ident">out</span>: <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">String</span><span class="op">&gt;</span>) {
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">inp</span>);
<span class="kw">match</span> <span class="ident">out</span> {
<span class="prelude-val">Some</span>(<span class="ident">x</span>) <span class="op">=&gt;</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">x</span>),
<span class="prelude-val">None</span> <span class="op">=&gt;</span> <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;No Output&quot;</span>),
}
}
<span class="kw">fn</span> <span class="ident">print_usage</span>(<span class="ident">program</span>: <span class="kw-2">&amp;</span><span class="ident">str</span>, <span class="ident">opts</span>: <span class="ident">Options</span>) {
<span class="kw">let</span> <span class="ident">brief</span> <span class="op">=</span> <span class="macro">format</span><span class="macro">!</span>(<span class="string">&quot;Usage: {} FILE [options]&quot;</span>, <span class="ident">program</span>);
<span class="macro">print</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">opts</span>.<span class="ident">usage</span>(<span class="kw-2">&amp;</span><span class="ident">brief</span>));
}
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> <span class="ident">args</span>: <span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">String</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">env</span>::<span class="ident">args</span>().<span class="ident">collect</span>();
<span class="kw">let</span> <span class="ident">program</span> <span class="op">=</span> <span class="ident">args</span>[<span class="number">0</span>].<span class="ident">clone</span>();
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">opts</span> <span class="op">=</span> <span class="ident">Options</span>::<span class="ident">new</span>();
<span class="ident">opts</span>.<span class="ident">optopt</span>(<span class="string">&quot;o&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;set output file name&quot;</span>, <span class="string">&quot;NAME&quot;</span>);
<span class="ident">opts</span>.<span class="ident">optflag</span>(<span class="string">&quot;h&quot;</span>, <span class="string">&quot;help&quot;</span>, <span class="string">&quot;print this help menu&quot;</span>);
<span class="kw">let</span> <span class="ident">matches</span> <span class="op">=</span> <span class="kw">match</span> <span class="ident">opts</span>.<span class="ident">parse</span>(<span class="kw-2">&amp;</span><span class="ident">args</span>[<span class="number">1</span>..]) {
<span class="prelude-val">Ok</span>(<span class="ident">m</span>) <span class="op">=&gt;</span> { <span class="ident">m</span> }
<span class="prelude-val">Err</span>(<span class="ident">f</span>) <span class="op">=&gt;</span> { <span class="macro">panic</span><span class="macro">!</span>(<span class="ident">f</span>.<span class="ident">to_string</span>()) }
};
<span class="kw">if</span> <span class="ident">matches</span>.<span class="ident">opt_present</span>(<span class="string">&quot;h&quot;</span>) {
<span class="ident">print_usage</span>(<span class="kw-2">&amp;</span><span class="ident">program</span>, <span class="ident">opts</span>);
<span class="kw">return</span>;
}
<span class="kw">let</span> <span class="ident">output</span> <span class="op">=</span> <span class="ident">matches</span>.<span class="ident">opt_str</span>(<span class="string">&quot;o&quot;</span>);
<span class="kw">let</span> <span class="ident">input</span> <span class="op">=</span> <span class="kw">if</span> <span class="op">!</span><span class="ident">matches</span>.<span class="ident">free</span>.<span class="ident">is_empty</span>() {
<span class="ident">matches</span>.<span class="ident">free</span>[<span class="number">0</span>].<span class="ident">clone</span>()
} <span class="kw">else</span> {
<span class="ident">print_usage</span>(<span class="kw-2">&amp;</span><span class="ident">program</span>, <span class="ident">opts</span>);
<span class="kw">return</span>;
};
<span class="ident">do_work</span>(<span class="kw-2">&amp;</span><span class="ident">input</span>, <span class="ident">output</span>);
}</pre>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table>
<tr class=' module-item'>
<td><a class="struct" href="struct.Matches.html"
title='struct getopts::Matches'>Matches</a></td>
<td class='docblock-short'>
<p>The result of checking command line arguments. Contains a vector
of matches and a vector of free strings.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="struct" href="struct.Options.html"
title='struct getopts::Options'>Options</a></td>
<td class='docblock-short'>
<p>A description of the options that a program can handle.</p>
</td>
</tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
<table>
<tr class=' module-item'>
<td><a class="enum" href="enum.Fail.html"
title='enum getopts::Fail'>Fail</a></td>
<td class='docblock-short'>
<p>The type returned when the command line does not conform to the
expected format. Use the <code>Debug</code> implementation to output detailed
information.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.HasArg.html"
title='enum getopts::HasArg'>HasArg</a></td>
<td class='docblock-short'>
<p>Describes whether an option has an argument.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.Occur.html"
title='enum getopts::Occur'>Occur</a></td>
<td class='docblock-short'>
<p>Describes how often an option may occur.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="enum" href="enum.ParsingStyle.html"
title='enum getopts::ParsingStyle'>ParsingStyle</a></td>
<td class='docblock-short'>
<p>What parsing style to use when parsing arguments.</p>
</td>
</tr></table><h2 id='types' class='section-header'><a href="#types">Type Definitions</a></h2>
<table>
<tr class=' module-item'>
<td><a class="type" href="type.Result.html"
title='type getopts::Result'>Result</a></td>
<td class='docblock-short'>
<p>The result of parsing a command line with a set of options.</p>
</td>
</tr></table></section>
<section id='search' class="content hidden"></section>
<section class="footer"></section>
<aside id="help" class="hidden">
<div>
<h1 class="hidden">Help</h1>
<div class="shortcuts">
<h2>Keyboard Shortcuts</h2>
<dl>
<dt><kbd>?</kbd></dt>
<dd>Show this help dialog</dd>
<dt><kbd>S</kbd></dt>
<dd>Focus the search field</dd>
<dt><kbd></kbd></dt>
<dd>Move up in search results</dd>
<dt><kbd></kbd></dt>
<dd>Move down in search results</dd>
<dt><kbd></kbd></dt>
<dd>Switch tab</dd>
<dt><kbd>&#9166;</kbd></dt>
<dd>Go to active search result</dd>
<dt><kbd>+</kbd></dt>
<dd>Expand all sections</dd>
<dt><kbd>-</kbd></dt>
<dd>Collapse all sections</dd>
</dl>
</div>
<div class="infos">
<h2>Search Tricks</h2>
<p>
Prefix searches with a type followed by a colon (e.g.
<code>fn:</code>) to restrict the search to a given type.
</p>
<p>
Accepted types are: <code>fn</code>, <code>mod</code>,
<code>struct</code>, <code>enum</code>,
<code>trait</code>, <code>type</code>, <code>macro</code>,
and <code>const</code>.
</p>
<p>
Search functions by type signature (e.g.
<code>vec -> usize</code> or <code>* -> vec</code>)
</p>
</div>
</div>
</aside>
<script>
window.rootPath = "../";
window.currentCrate = "getopts";
</script>
<script src="../main.js"></script>
<script defer src="../search-index.js"></script>
</body>
</html>