oxipng/doc/clap/struct.ArgMatches.html
2016-03-11 12:13:13 -05:00

240 lines
No EOL
19 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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 `ArgMatches` struct in crate `clap`.">
<meta name="keywords" content="rust, rustlang, rust-lang, ArgMatches">
<title>clap::ArgMatches - Rust</title>
<link rel="stylesheet" type="text/css" href="../rustdoc.css">
<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]-->
<nav class="sidebar">
<p class='location'><a href='index.html'>clap</a></p><script>window.sidebarCurrent = {name: 'ArgMatches', ty: 'struct', relpath: ''};</script><script defer src="sidebar-items.js"></script>
</nav>
<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 struct">
<h1 class='fqn'><span class='in-band'>Struct <a href='index.html'>clap</a>::<wbr><a class='struct' href=''>ArgMatches</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 id='src-3098' class='srclink' href='../src/clap/args/arg_matches.rs.html.html#56-63' title='goto source code'>[src]</a></span></h1>
<pre class='rust struct'>pub struct ArgMatches&lt;'n, 'a&gt; {
// some fields omitted
}</pre><div class='docblock'><p>Used to get information about the arguments that where supplied to the program at runtime by
the user. To get a new instance of this struct you use <code>.get_matches()</code> of the <code>App</code> struct.</p>
<h1 id='examples' class='section-header'><a href='#examples'>Examples</a></h1>
<pre class='rust rust-example-rendered'>
<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='comment'>// adding of arguments and configuration goes here...</span>
.<span class='ident'>get_matches</span>();
<span class='comment'>// if you had an argument named &quot;output&quot; that takes a value</span>
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(<span class='ident'>o</span>) <span class='op'>=</span> <span class='ident'>matches</span>.<span class='ident'>value_of</span>(<span class='string'>&quot;output&quot;</span>) {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;Value for output: {}&quot;</span>, <span class='ident'>o</span>);
}
<span class='comment'>// If you have a required argument you can call .unwrap() because the program will exit long</span>
<span class='comment'>// before this point if the user didn&#39;t specify it at runtime.</span>
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;Config file: {}&quot;</span>, <span class='ident'>matches</span>.<span class='ident'>value_of</span>(<span class='string'>&quot;config&quot;</span>).<span class='ident'>unwrap</span>());
<span class='comment'>// You can check the presence of an argument</span>
<span class='kw'>if</span> <span class='ident'>matches</span>.<span class='ident'>is_present</span>(<span class='string'>&quot;debug&quot;</span>) {
<span class='comment'>// Another way to check if an argument was present, or if it occurred multiple times is to</span>
<span class='comment'>// use occurrences_of() which returns 0 if an argument isn&#39;t found at runtime, or the</span>
<span class='comment'>// number of times that it occurred, if it was. To allow an argument to appear more than</span>
<span class='comment'>// once, you must use the .multiple(true) method, otherwise it will only return 1 or 0.</span>
<span class='kw'>if</span> <span class='ident'>matches</span>.<span class='ident'>occurrences_of</span>(<span class='string'>&quot;debug&quot;</span>) <span class='op'>&gt;</span> <span class='number'>2</span> {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;Debug mode is REALLY on&quot;</span>);
} <span class='kw'>else</span> {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;Debug mode kind of on&quot;</span>);
}
}
<span class='comment'>// You can get the sub-matches of a particular subcommand (in this case &quot;test&quot;)</span>
<span class='comment'>// If &quot;test&quot; had it&#39;s own &quot;-l&quot; flag you could check for it&#39;s presence accordingly</span>
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(<span class='kw-2'>ref</span> <span class='ident'>matches</span>) <span class='op'>=</span> <span class='ident'>matches</span>.<span class='ident'>subcommand_matches</span>(<span class='string'>&quot;test&quot;</span>) {
<span class='kw'>if</span> <span class='ident'>matches</span>.<span class='ident'>is_present</span>(<span class='string'>&quot;list&quot;</span>) {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;Printing testing lists...&quot;</span>);
} <span class='kw'>else</span> {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;Not printing testing lists...&quot;</span>);
}
}</pre>
</div><h2 id='methods'>Methods</h2><h3 class='impl'><code>impl&lt;'n, 'a&gt; <a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a>&lt;'n, 'a&gt;</code></h3><div class='impl-items'><h4 id='method.value_of' class='method'><code>fn <a href='#method.value_of' class='fnname'>value_of</a>(&amp;self, name: &amp;<a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>) -&gt; <a class='enum' href='https://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a>&lt;&amp;<a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>&gt;</code></h4>
<div class='docblock'><p>Gets the value of a specific option or positional argument (i.e. an argument that takes
an additional value at runtime). If the option wasn&#39;t present at runtime
it returns <code>None</code>.</p>
<p><em>NOTE:</em> If getting a value for an option or positional argument that allows multiples,
prefer <code>values_of()</code> as <code>value_of()</code> will only return the <em><em>first</em></em> value.</p>
<h1 id='examples-1' class='section-header'><a href='#examples-1'>Examples</a></h1>
<pre class='rust rust-example-rendered'>
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(<span class='ident'>o</span>) <span class='op'>=</span> <span class='ident'>matches</span>.<span class='ident'>value_of</span>(<span class='string'>&quot;output&quot;</span>) {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;Value for output: {}&quot;</span>, <span class='ident'>o</span>);
}</pre>
</div><h4 id='method.values_of' class='method'><code>fn <a href='#method.values_of' class='fnname'>values_of</a>(&amp;'a self, name: &amp;<a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>) -&gt; <a class='enum' href='https://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a>&lt;<a class='struct' href='https://doc.rust-lang.org/nightly/collections/vec/struct.Vec.html' title='collections::vec::Vec'>Vec</a>&lt;&amp;'a <a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>&gt;&gt;</code></h4>
<div class='docblock'><p>Gets the values of a specific option or positional argument in a vector (i.e. an argument
that takes multiple values at runtime). If the option wasn&#39;t present at runtime it
returns <code>None</code></p>
<h1 id='examples-2' class='section-header'><a href='#examples-2'>Examples</a></h1>
<pre class='rust rust-example-rendered'>
<span class='comment'>// If the program had option &quot;-c&quot; that took a value and was run</span>
<span class='comment'>// via &quot;myapp -o some -o other -o file&quot;</span>
<span class='comment'>// values_of() would return a [&amp;str; 3] (&quot;some&quot;, &quot;other&quot;, &quot;file&quot;)</span>
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(<span class='ident'>os</span>) <span class='op'>=</span> <span class='ident'>matches</span>.<span class='ident'>values_of</span>(<span class='string'>&quot;output&quot;</span>) {
<span class='kw'>for</span> <span class='ident'>o</span> <span class='kw'>in</span> <span class='ident'>os</span> {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;A value for output: {}&quot;</span>, <span class='ident'>o</span>);
}
}</pre>
</div><h4 id='method.is_present' class='method'><code>fn <a href='#method.is_present' class='fnname'>is_present</a>(&amp;self, name: &amp;<a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>) -&gt; <a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.bool.html'>bool</a></code></h4>
<div class='docblock'><p>Returns if an argument was present at runtime.</p>
<h1 id='examples-3' class='section-header'><a href='#examples-3'>Examples</a></h1>
<pre class='rust rust-example-rendered'>
<span class='kw'>if</span> <span class='ident'>matches</span>.<span class='ident'>is_present</span>(<span class='string'>&quot;output&quot;</span>) {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;The output argument was used!&quot;</span>);
}</pre>
</div><h4 id='method.occurrences_of' class='method'><code>fn <a href='#method.occurrences_of' class='fnname'>occurrences_of</a>(&amp;self, name: &amp;<a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>) -&gt; <a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.u8.html'>u8</a></code></h4>
<div class='docblock'><p>Returns the number of occurrences of an option, flag, or positional argument at runtime.
If an argument isn&#39;t present it will return <code>0</code>. Can be used on arguments which <em>don&#39;t</em>
allow multiple occurrences, but will obviously only return <code>0</code> or <code>1</code>.</p>
<h1 id='examples-4' class='section-header'><a href='#examples-4'>Examples</a></h1>
<pre class='rust rust-example-rendered'>
<span class='kw'>if</span> <span class='ident'>matches</span>.<span class='ident'>occurrences_of</span>(<span class='string'>&quot;debug&quot;</span>) <span class='op'>&gt;</span> <span class='number'>1</span> {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;Debug mode is REALLY on&quot;</span>);
} <span class='kw'>else</span> {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;Debug mode kind of on&quot;</span>);
}</pre>
</div><h4 id='method.subcommand_matches' class='method'><code>fn <a href='#method.subcommand_matches' class='fnname'>subcommand_matches</a>&lt;'na&gt;(&amp;self, name: &amp;'na <a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>) -&gt; <a class='enum' href='https://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a>&lt;&amp;<a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a>&gt;</code></h4>
<div class='docblock'><p>Returns the <code>ArgMatches</code> for a particular subcommand or None if the subcommand wasn&#39;t
present at runtime.</p>
<h1 id='examples-5' class='section-header'><a href='#examples-5'>Examples</a></h1>
<pre class='rust rust-example-rendered'>
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(<span class='ident'>matches</span>) <span class='op'>=</span> <span class='ident'>app_matches</span>.<span class='ident'>subcommand_matches</span>(<span class='string'>&quot;test&quot;</span>) {
<span class='comment'>// Use matches as normal</span>
}</pre>
</div><h4 id='method.subcommand_name' class='method'><code>fn <a href='#method.subcommand_name' class='fnname'>subcommand_name</a>(&amp;self) -&gt; <a class='enum' href='https://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a>&lt;&amp;<a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>&gt;</code></h4>
<div class='docblock'><p>Returns the name of the subcommand used of the parent <code>App</code>, or <code>None</code> if one wasn&#39;t found</p>
<p><em>NOTE</em>: Only a single subcommand may be present per <code>App</code> at runtime, does <em>NOT</em> check for
the name of sub-subcommand&#39;s names</p>
<h1 id='examples-6' class='section-header'><a href='#examples-6'>Examples</a></h1>
<pre class='rust rust-example-rendered'>
<span class='kw'>match</span> <span class='ident'>app_matches</span>.<span class='ident'>subcommand_name</span>() {
<span class='prelude-val'>Some</span>(<span class='string'>&quot;test&quot;</span>) <span class='op'>=&gt;</span> {}, <span class='comment'>// test was used</span>
<span class='prelude-val'>Some</span>(<span class='string'>&quot;config&quot;</span>) <span class='op'>=&gt;</span> {}, <span class='comment'>// config was used</span>
_ <span class='op'>=&gt;</span> {}, <span class='comment'>// Either no subcommand or one not tested for...</span>
}</pre>
</div><h4 id='method.subcommand' class='method'><code>fn <a href='#method.subcommand' class='fnname'>subcommand</a>(&amp;self) -&gt; <a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.tuple.html'>(</a>&amp;<a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>, <a class='enum' href='https://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a>&lt;&amp;<a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a>&gt;<a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.tuple.html'>)</a></code></h4>
<div class='docblock'><p>Returns the name and <code>ArgMatches</code> of the subcommand used at runtime or (&quot;&quot;, None) if one
wasn&#39;t found.</p>
<h1 id='examples-7' class='section-header'><a href='#examples-7'>Examples</a></h1>
<pre class='rust rust-example-rendered'>
<span class='kw'>match</span> <span class='ident'>app_matches</span>.<span class='ident'>subcommand</span>() {
(<span class='string'>&quot;test&quot;</span>, <span class='prelude-val'>Some</span>(<span class='ident'>matches</span>)) <span class='op'>=&gt;</span> {}, <span class='comment'>// test was used</span>
(<span class='string'>&quot;config&quot;</span>, <span class='prelude-val'>Some</span>(<span class='ident'>matches</span>)) <span class='op'>=&gt;</span> {}, <span class='comment'>// config was used</span>
_ <span class='op'>=&gt;</span> {}, <span class='comment'>// Either no subcommand or one not tested for...</span>
}</pre>
</div><h4 id='method.usage' class='method'><code>fn <a href='#method.usage' class='fnname'>usage</a>(&amp;self) -&gt; &amp;<a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a></code></h4>
<div class='docblock'><p>Returns a string slice of the usage statement for the <code>App</code> (or <code>SubCommand</code>)</p>
<h1 id='examples-8' class='section-header'><a href='#examples-8'>Examples</a></h1>
<pre class='rust rust-example-rendered'>
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{}&quot;</span>,<span class='ident'>app_matches</span>.<span class='ident'>usage</span>());</pre>
</div></div><h2 id='implementations'>Trait Implementations</h2><h3 id='derived_implementations'>Derived Implementations </h3><h3 class='impl'><code>impl&lt;'n, 'a&gt; <a class='trait' href='https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html' title='core::fmt::Debug'>Debug</a> for <a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a>&lt;'n, 'a&gt;</code></h3><div class='impl-items'><h4 id='method.fmt' class='method'><code>fn <a href='https://doc.rust-lang.org/nightly/core/fmt/trait.Debug.html#method.fmt' class='fnname'>fmt</a>(&amp;self, __arg_0: &amp;mut <a class='struct' href='https://doc.rust-lang.org/nightly/core/fmt/struct.Formatter.html' title='core::fmt::Formatter'>Formatter</a>) -&gt; <a class='type' href='https://doc.rust-lang.org/nightly/core/fmt/type.Result.html' title='core::fmt::Result'>Result</a></code></h4>
</div></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>?</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">
<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 = "clap";
window.playgroundUrl = "";
</script>
<script src="../jquery.js"></script>
<script src="../main.js"></script>
<script defer src="../search-index.js"></script>
</body>
</html>