clap/docs/clap/macro.value_t!.html
2015-04-16 12:56:51 -04:00

169 lines
No EOL
12 KiB
HTML

<!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 `value_t!` macro in crate `clap`.">
<meta name="keywords" content="rust, rustlang, rust-lang, value_t!">
<title>clap::value_t! - Rust</title>
<link rel="stylesheet" type="text/css" href="../main.css">
</head>
<body class="rustdoc">
<!--[if lte IE 8]>
<div class="warning">
This old browser is unsupported and will most likely display funky
things.
</div>
<![endif]-->
<section class="sidebar">
<p class='location'><a href='index.html'>clap</a></p><script>window.sidebarCurrent = {name: 'value_t!', ty: 'macro', relpath: ''};</script><script defer src="sidebar-items.js"></script>
</section>
<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 macro">
<h1 class='fqn'><span class='in-band'><a href='index.html'>clap</a>::<wbr><a class='macro' href=''>value_t!</a><wbr></span><span class='out-of-band'><span id='render-detail'>
<a id="collapse-all" href="#">[-]</a>&nbsp;<a id="expand-all" href="#">[+]</a>
</span><a id='src-10428' href='../src/clap/macros.rs.html#84-118'>[src]</a></span></h1>
<pre class='rust macro'>
<span class='macro'>macro_rules</span><span class='macro'>!</span> <span class='ident'>value_t</span> {
(<span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>m</span>:<span class='ident'>ident</span>.<span class='ident'>value_of</span>(<span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>v</span>:<span class='ident'>expr</span>), <span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>t</span>:<span class='ident'>ty</span>) <span class='op'>=&gt;</span> {
<span class='kw'>match</span> <span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>m</span>.<span class='ident'>value_of</span>(<span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>v</span>) {
<span class='prelude-val'>Some</span>(<span class='ident'>v</span>) <span class='op'>=&gt;</span> {
<span class='kw'>match</span> <span class='ident'>v</span>.<span class='ident'>parse</span>::<span class='op'>&lt;</span><span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>t</span><span class='op'>&gt;</span>() {
<span class='prelude-val'>Ok</span>(<span class='ident'>val</span>) <span class='op'>=&gt;</span> <span class='prelude-val'>Ok</span>(<span class='ident'>val</span>),
<span class='prelude-val'>Err</span>(_) <span class='op'>=&gt;</span> <span class='prelude-val'>Err</span>(<span class='macro'>format</span><span class='macro'>!</span>(<span class='string'>&quot;{} isn&#39;t a valid {}&quot;</span>,<span class='ident'>v</span>,<span class='macro'>stringify</span><span class='macro'>!</span>(<span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>t</span>))),
}
},
<span class='prelude-val'>None</span> <span class='op'>=&gt;</span> <span class='prelude-val'>Err</span>(<span class='macro'>format</span><span class='macro'>!</span>(<span class='string'>&quot;Argument \&quot;{}\&quot; not found&quot;</span>, <span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>v</span>))
}
};
(<span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>m</span>:<span class='ident'>ident</span>.<span class='ident'>values_of</span>(<span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>v</span>:<span class='ident'>expr</span>), <span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>t</span>:<span class='ident'>ty</span>) <span class='op'>=&gt;</span> {
<span class='kw'>match</span> <span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>m</span>.<span class='ident'>values_of</span>(<span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>v</span>) {
<span class='prelude-val'>Some</span>(<span class='kw-2'>ref</span> <span class='ident'>v</span>) <span class='op'>=&gt;</span> {
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>tmp</span> <span class='op'>=</span> <span class='ident'>Vec</span>::<span class='ident'>with_capacity</span>(<span class='ident'>v</span>.<span class='ident'>len</span>());
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>err</span> <span class='op'>=</span> <span class='prelude-val'>None</span>;
<span class='kw'>for</span> <span class='ident'>pv</span> <span class='kw'>in</span> <span class='ident'>v</span> {
<span class='kw'>match</span> <span class='ident'>pv</span>.<span class='ident'>parse</span>::<span class='op'>&lt;</span><span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>t</span><span class='op'>&gt;</span>() {
<span class='prelude-val'>Ok</span>(<span class='ident'>rv</span>) <span class='op'>=&gt;</span> <span class='ident'>tmp</span>.<span class='ident'>push</span>(<span class='ident'>rv</span>),
<span class='prelude-val'>Err</span>(_) <span class='op'>=&gt;</span> {
<span class='ident'>err</span> <span class='op'>=</span> <span class='prelude-val'>Some</span>(<span class='macro'>format</span><span class='macro'>!</span>(<span class='string'>&quot;{} isn&#39;t a valid {}&quot;</span>,<span class='ident'>pv</span>,<span class='macro'>stringify</span><span class='macro'>!</span>(<span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>t</span>)));
<span class='kw'>break</span>
}
}
}
<span class='kw'>match</span> <span class='ident'>err</span> {
<span class='prelude-val'>Some</span>(<span class='ident'>e</span>) <span class='op'>=&gt;</span> <span class='prelude-val'>Err</span>(<span class='ident'>e</span>),
<span class='prelude-val'>None</span> <span class='op'>=&gt;</span> <span class='prelude-val'>Ok</span>(<span class='ident'>tmp</span>)
}
},
<span class='prelude-val'>None</span> <span class='op'>=&gt;</span> <span class='prelude-val'>Err</span>(<span class='macro'>format</span><span class='macro'>!</span>(<span class='string'>&quot;Argument \&quot;{}\&quot; not found&quot;</span>, <span class='macro-nonterminal'>$</span><span class='macro-nonterminal'>v</span>))
}
};
}
</pre>
<div class='docblock'><p>Convenience macro getting a typed value <code>T</code> where <code>T</code> implements <code>std::str::FromStr</code>
This macro returns a <code>Result&lt;T,String&gt;</code> which allows you as the developer to decide
what you&#39;d like to do on a failed parse. There are two types of errors, parse failures
and those where the argument wasn&#39;t present (such as a non-required argument). </p>
<p>You can use it to get a single value, or a <code>Vec&lt;T&gt;</code> with the <code>values_of()</code></p>
<p><strong>NOTE:</strong> Be cautious, as since this a macro invocation it&#39;s not exactly like
standard syntax.</p>
<p># Example single value</p>
<pre id='rust-example-rendered' class='rust '>
<span class='kw'>let</span> <span class='ident'>matches</span> <span class='op'>=</span> <span class='ident'>App</span>::<span class='ident'>new</span>(<span class='string'>&quot;myapp&quot;</span>)
.<span class='ident'>arg_from_usage</span>(<span class='string'>&quot;[length] &#39;Set the length to use as a pos whole num, i.e. 20&#39;&quot;</span>)
.<span class='ident'>get_matches</span>();
<span class='kw'>let</span> <span class='ident'>len</span> <span class='op'>=</span> <span class='macro'>value_t</span><span class='macro'>!</span>(<span class='ident'>matches</span>.<span class='ident'>value_of</span>(<span class='string'>&quot;length&quot;</span>), <span class='ident'>u32</span>)
.<span class='ident'>unwrap_or_else</span>(<span class='op'>|</span><span class='ident'>e</span><span class='op'>|</span>{
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{}&quot;</span>,<span class='ident'>e</span>);
<span class='ident'>std</span>::<span class='ident'>process</span>::<span class='ident'>exit</span>(<span class='number'>1</span>)
});
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{} + 2: {}&quot;</span>, <span class='ident'>len</span>, <span class='ident'>len</span> <span class='op'>+</span> <span class='number'>2</span>);
</pre>
<p># Example multiple values</p>
<pre id='rust-example-rendered' class='rust '>
<span class='kw'>let</span> <span class='ident'>matches</span> <span class='op'>=</span> <span class='ident'>App</span>::<span class='ident'>new</span>(<span class='string'>&quot;myapp&quot;</span>)
.<span class='ident'>arg_from_usage</span>(<span class='string'>&quot;[seq]... &#39;A sequence of pos whole nums, i.e. 20 45&#39;&quot;</span>)
.<span class='ident'>get_matches</span>();
<span class='kw'>for</span> <span class='ident'>v</span> <span class='kw'>in</span> <span class='macro'>value_t</span><span class='macro'>!</span>(<span class='ident'>matches</span>.<span class='ident'>values_of</span>(<span class='string'>&quot;seq&quot;</span>), <span class='ident'>u32</span>)
.<span class='ident'>unwrap_or_else</span>(<span class='op'>|</span><span class='ident'>e</span><span class='op'>|</span>{
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{}&quot;</span>,<span class='ident'>e</span>);
<span class='ident'>std</span>::<span class='ident'>process</span>::<span class='ident'>exit</span>(<span class='number'>1</span>)
}) {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{} + 2: {}&quot;</span>, <span class='ident'>v</span>, <span class='ident'>v</span> <span class='op'>+</span> <span class='number'>2</span>);
}
</pre>
</div></section>
<section id='search' class="content hidden"></section>
<section class="footer"></section>
<div id="help" class="hidden">
<div class="shortcuts">
<h1>Keyboard shortcuts</h1>
<dl>
<dt>?</dt>
<dd>Show this help dialog</dd>
<dt>S</dt>
<dd>Focus the search field</dd>
<dt>&larrb;</dt>
<dd>Move up in search results</dd>
<dt>&rarrb;</dt>
<dd>Move down in search results</dd>
<dt>&#9166;</dt>
<dd>Go to active search result</dd>
</dl>
</div>
<div class="infos">
<h1>Search tricks</h1>
<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>typedef</code> (or
<code>tdef</code>).
</p>
</div>
</div>
<script>
window.rootPath = "../";
window.currentCrate = "clap";
window.playgroundUrl = "";
</script>
<script src="../jquery.js"></script>
<script src="../main.js"></script>
<script async src="../search-index.js"></script>
</body>
</html>