240 lines
No EOL
19 KiB
HTML
240 lines
No EOL
19 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 `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'>−</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<'n, 'a> {
|
||
// 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'>"MyApp"</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 "output" 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'>"output"</span>) {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Value for output: {}"</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't specify it at runtime.</span>
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Config file: {}"</span>, <span class='ident'>matches</span>.<span class='ident'>value_of</span>(<span class='string'>"config"</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'>"debug"</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'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'>"debug"</span>) <span class='op'>></span> <span class='number'>2</span> {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Debug mode is REALLY on"</span>);
|
||
} <span class='kw'>else</span> {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Debug mode kind of on"</span>);
|
||
}
|
||
}
|
||
|
||
<span class='comment'>// You can get the sub-matches of a particular subcommand (in this case "test")</span>
|
||
<span class='comment'>// If "test" had it's own "-l" flag you could check for it'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'>"test"</span>) {
|
||
<span class='kw'>if</span> <span class='ident'>matches</span>.<span class='ident'>is_present</span>(<span class='string'>"list"</span>) {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Printing testing lists..."</span>);
|
||
} <span class='kw'>else</span> {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Not printing testing lists..."</span>);
|
||
}
|
||
}</pre>
|
||
</div><h2 id='methods'>Methods</h2><h3 class='impl'><code>impl<'n, 'a> <a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a><'n, 'a></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>(&self, name: &<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><&<a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>></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'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'>"output"</span>) {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Value for output: {}"</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>(&'a self, name: &<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><<a class='struct' href='https://doc.rust-lang.org/nightly/collections/vec/struct.Vec.html' title='collections::vec::Vec'>Vec</a><&'a <a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>>></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'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 "-c" that took a value and was run</span>
|
||
<span class='comment'>// via "myapp -o some -o other -o file"</span>
|
||
<span class='comment'>// values_of() would return a [&str; 3] ("some", "other", "file")</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'>"output"</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'>"A value for output: {}"</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>(&self, name: &<a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>) -> <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'>"output"</span>) {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"The output argument was used!"</span>);
|
||
}</pre>
|
||
</div><h4 id='method.occurrences_of' class='method'><code>fn <a href='#method.occurrences_of' class='fnname'>occurrences_of</a>(&self, name: &<a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>) -> <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't present it will return <code>0</code>. Can be used on arguments which <em>don'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'>"debug"</span>) <span class='op'>></span> <span class='number'>1</span> {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Debug mode is REALLY on"</span>);
|
||
} <span class='kw'>else</span> {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Debug mode kind of on"</span>);
|
||
}</pre>
|
||
</div><h4 id='method.subcommand_matches' class='method'><code>fn <a href='#method.subcommand_matches' class='fnname'>subcommand_matches</a><'na>(&self, name: &'na <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><&<a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a>></code></h4>
|
||
<div class='docblock'><p>Returns the <code>ArgMatches</code> for a particular subcommand or None if the subcommand wasn'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'>"test"</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>(&self) -> <a class='enum' href='https://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a><&<a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>></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'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'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'>"test"</span>) <span class='op'>=></span> {}, <span class='comment'>// test was used</span>
|
||
<span class='prelude-val'>Some</span>(<span class='string'>"config"</span>) <span class='op'>=></span> {}, <span class='comment'>// config was used</span>
|
||
_ <span class='op'>=></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>(&self) -> <a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.tuple.html'>(</a>&<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><&<a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a>><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 ("", None) if one
|
||
wasn'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'>"test"</span>, <span class='prelude-val'>Some</span>(<span class='ident'>matches</span>)) <span class='op'>=></span> {}, <span class='comment'>// test was used</span>
|
||
(<span class='string'>"config"</span>, <span class='prelude-val'>Some</span>(<span class='ident'>matches</span>)) <span class='op'>=></span> {}, <span class='comment'>// config was used</span>
|
||
_ <span class='op'>=></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>(&self) -> &<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'>"{}"</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<'n, 'a> <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><'n, 'a></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>(&self, __arg_0: &mut <a class='struct' href='https://doc.rust-lang.org/nightly/core/fmt/struct.Formatter.html' title='core::fmt::Formatter'>Formatter</a>) -> <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>⇤</dt>
|
||
<dd>Move up in search results</dd>
|
||
<dt>⇥</dt>
|
||
<dd>Move down in search results</dd>
|
||
<dt>⏎</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> |