Added support for subcommands

This commit is contained in:
Kevin K 2015-03-15 17:17:52 -04:00
parent 5ca006cbb5
commit 65a8a4f045
35 changed files with 997 additions and 187 deletions

2
Cargo.lock generated
View file

@ -1,4 +1,4 @@
[root]
name = "clap"
version = "0.3.7"
version = "0.4.0"

View file

@ -1,7 +1,7 @@
[package]
name = "clap"
version = "0.3.7"
version = "0.4.0"
authors = ["Kevin K. <kbknapp@gmail.com>"]
exclude = ["docs/*"]
description = "A Command Line Argument Parser written in Rust"

View file

@ -0,0 +1 @@
initSidebarItems({"struct":[["App","Used to create a representation of the program and all possible command line arguments for parsing at runtime."]]});

View file

@ -0,0 +1 @@
initSidebarItems({"struct":[["Arg","The abstract representation of a command line argument used by the consumer of the library. "]]});

View file

@ -0,0 +1 @@
initSidebarItems({"struct":[["ArgMatches","Used to get information about the arguments that where supplied to the program at runtime."]]});

View file

@ -26,7 +26,7 @@
<section class="sidebar">
<p class='location'></p>
<p class='location'></p><script>window.sidebarCurrent = {name: 'clap', ty: 'mod', relpath: '../'};</script>
</section>
<nav class="sub">
@ -43,7 +43,7 @@
<section id='main' class="content mod">
<h1 class='fqn'><span class='in-band'>Crate <a class='mod' href=''>clap</a><wbr></span><span class='out-of-band'><a href='stability.html'>[stability]</a> <span id='render-detail'>
<a id="collapse-all" href="#">[-]</a>&nbsp;<a id="expand-all" href="#">[+]</a>
</span><a id='src-0' href='../src/clap/lib.rs.html#1-136'>[src]</a></span></h1>
</span><a id='src-0' href='../src/clap/lib.rs.html#1-155'>[src]</a></span></h1>
<div class='docblock'><p>A simply library for parsing command line arguments when writing
command line and console applications.</p>
@ -77,6 +77,11 @@
.<span class='ident'>short</span>(<span class='string'>&quot;d&quot;</span>)
.<span class='ident'>multiple</span>(<span class='boolval'>true</span>)
.<span class='ident'>help</span>(<span class='string'>&quot;Turn debugging information on&quot;</span>))
.<span class='ident'>subcomamnd</span>(<span class='ident'>SubCommand</span>::<span class='ident'>new</span>(<span class='string'>&quot;test&quot;</span>)
.<span class='ident'>about</span>(<span class='string'>&quot;Has test sub functionality&quot;</span>)
.<span class='ident'>arg</span>(<span class='ident'>Arg</span>::<span class='ident'>new</span>(<span class='string'>&quot;verbose&quot;</span>)
.<span class='ident'>short</span>(<span class='string'>&quot;v&quot;</span>)
.<span class='ident'>help</span>(<span class='string'>&quot;Display verbose information&quot;</span>)))
.<span class='ident'>get_matches</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>) {
@ -94,6 +99,14 @@
<span class='number'>3</span> <span class='op'>|</span> _ <span class='op'>=&gt;</span> <span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;Don&#39;t be crazy&quot;</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;verbose&quot;</span>) {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;Printing verbose test info...&quot;</span>);
} <span class='kw'>else</span> {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;Not printing regular test info...&quot;</span>);
}
}
<span class='comment'>// more porgram logic goes here...</span>
</pre>
@ -105,7 +118,7 @@
Does awesome things
USAGE:
MyApp [FLAGS] [OPTIONS] [POSITIONAL]
MyApp [FLAGS] [OPTIONS] [POSITIONAL] [SUBCOMMANDS]
FLAGS:
-d Turn debugging information on
@ -117,6 +130,10 @@
POSITIONAL ARGUMENTS:
output Sets an optional output file
SUBCOMMANDS:
help Prints this message
test Has test sub-functionality
</code></pre>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table>
@ -140,6 +157,13 @@
title='clap::ArgMatches'>ArgMatches</a></td>
<td class='docblock short'><p>Used to get information about the arguments that
where supplied to the program at runtime.</p>
</td>
</tr>
<tr>
<td><a class='stability Unmarked' title='No stability level'></a><a class='struct' href='struct.SubCommand.html'
title='clap::SubCommand'>SubCommand</a></td>
<td class='docblock short'><p>The abstract representation of a command line subcommand used by the consumer of the library.</p>
</td>
</tr>
</table></section>

View file

@ -0,0 +1 @@
initSidebarItems({"struct":[["App","Used to create a representation of the program and all possible command line arguments for parsing at runtime."],["Arg","The abstract representation of a command line argument used by the consumer of the library. "],["ArgMatches","Used to get information about the arguments that where supplied to the program at runtime."],["SubCommand","The abstract representation of a command line subcommand used by the consumer of the library. "]]});

View file

@ -26,7 +26,7 @@
<section class="sidebar">
<p class='location'></p>
<p class='location'></p><script>window.sidebarCurrent = {name: 'clap', ty: 'mod', relpath: '../'};</script>
</section>
<nav class="sub">

View file

@ -1 +1 @@
{"name":"clap","counts":{"deprecated":0,"unstable":0,"stable":0,"unmarked":30},"submodules":[]}
{"name":"clap","counts":{"deprecated":0,"unstable":0,"stable":0,"unmarked":37},"submodules":[]}

View file

@ -26,7 +26,7 @@
<section class="sidebar">
<p class='location'><a href='index.html'>clap</a></p><div class='block struct'><h2>Structs</h2><a class='struct current' href='struct.App.html' title='Used to create a representation of the program and all possible command line arguments for parsing at runtime.'>App</a><a class='struct ' href='struct.Arg.html' title='The abstract representation of a command line argument used by the consumer of the library. '>Arg</a><a class='struct ' href='struct.ArgMatches.html' title='Used to get information about the arguments that where supplied to the program at runtime.'>ArgMatches</a></div>
<p class='location'><a href='index.html'>clap</a></p><script>window.sidebarCurrent = {name: 'App', ty: 'struct', relpath: ''};</script><script defer src="sidebar-items.js"></script>
</section>
<nav class="sub">
@ -43,7 +43,7 @@
<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=''>App</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-19' href='../src/clap/app.rs.html#37-59'>[src]</a></span></h1>
</span><a id='src-22' href='../src/clap/app.rs.html#39-64'>[src]</a></span></h1>
<pre class='rust struct'>pub struct App {
pub name: &amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>,
pub author: <a class='enum' href='http://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a>&lt;&amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>&gt;,
@ -122,7 +122,9 @@ to the user when they print version or help and usage information.</p>
.<span class='ident'>args</span>( <span class='macro'>vec</span><span class='macro'>!</span>[<span class='ident'>Arg</span>::<span class='ident'>new</span>(<span class='string'>&quot;config&quot;</span>).<span class='ident'>short</span>(<span class='string'>&quot;c&quot;</span>),
<span class='ident'>Arg</span>::<span class='ident'>new</span>(<span class='string'>&quot;debug&quot;</span>).<span class='ident'>short</span>(<span class='string'>&quot;d&quot;</span>)])
</pre>
</div><h4 id='method.get_matches' class='method'><a class='stability Unmarked' title='No stability level'></a><code>fn <a href='#method.get_matches' class='fnname'>get_matches</a>(self) -&gt; <a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a></code></h4>
</div><h4 id='method.subcommand' class='method'><a class='stability Unmarked' title='No stability level'></a><code>fn <a href='#method.subcommand' class='fnname'>subcommand</a>(self, subcmd: <a class='struct' href='../clap/struct.App.html' title='clap::App'>App</a>) -&gt; <a class='struct' href='../clap/struct.App.html' title='clap::App'>App</a></code></h4>
<h4 id='method.subcommands' class='method'><a class='stability Unmarked' title='No stability level'></a><code>fn <a href='#method.subcommands' class='fnname'>subcommands</a>(self, subcmds: <a class='struct' href='http://doc.rust-lang.org/nightly/collections/vec/struct.Vec.html' title='collections::vec::Vec'>Vec</a>&lt;<a class='struct' href='../clap/struct.App.html' title='clap::App'>App</a>&gt;) -&gt; <a class='struct' href='../clap/struct.App.html' title='clap::App'>App</a></code></h4>
<h4 id='method.get_matches' class='method'><a class='stability Unmarked' title='No stability level'></a><code>fn <a href='#method.get_matches' class='fnname'>get_matches</a>(self) -&gt; <a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a></code></h4>
</div></section>
<section id='search' class="content hidden"></section>

View file

@ -26,7 +26,7 @@
<section class="sidebar">
<p class='location'><a href='index.html'>clap</a></p><div class='block struct'><h2>Structs</h2><a class='struct ' href='struct.App.html' title='Used to create a representation of the program and all possible command line arguments for parsing at runtime.'>App</a><a class='struct current' href='struct.Arg.html' title='The abstract representation of a command line argument used by the consumer of the library. '>Arg</a><a class='struct ' href='struct.ArgMatches.html' title='Used to get information about the arguments that where supplied to the program at runtime.'>ArgMatches</a></div>
<p class='location'><a href='index.html'>clap</a></p><script>window.sidebarCurrent = {name: 'Arg', ty: 'struct', relpath: ''};</script><script defer src="sidebar-items.js"></script>
</section>
<nav class="sub">
@ -43,7 +43,7 @@
<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=''>Arg</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-4358' href='../src/clap/arg.rs.html#21-53'>[src]</a></span></h1>
</span><a id='src-4820' href='../src/clap/arg.rs.html#21-53'>[src]</a></span></h1>
<pre class='rust struct'>pub struct Arg {
pub name: &amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>,
pub short: <a class='enum' href='http://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a>&lt;<a href='http://doc.rust-lang.org/nightly/std/primitive.char.html'>char</a>&gt;,
@ -62,8 +62,8 @@ their program.
and then evaluates the settings the consumer provided and determines the concret
argument struct to use when parsing.</p>
<p>Example:</p>
<pre id='rust-example-rendered' class='rust '>
<h1 id="example" class='section-header'><a
href="#example">Example</a></h1><pre id='rust-example-rendered' class='rust '>
<span class='ident'>Arg</span>::<span class='ident'>new</span>(<span class='string'>&quot;conifg&quot;</span>)
.<span class='ident'>short</span>(<span class='string'>&quot;c&quot;</span>)
.<span class='ident'>long</span>(<span class='string'>&quot;config&quot;</span>)

View file

@ -26,7 +26,7 @@
<section class="sidebar">
<p class='location'><a href='index.html'>clap</a></p><div class='block struct'><h2>Structs</h2><a class='struct ' href='struct.App.html' title='Used to create a representation of the program and all possible command line arguments for parsing at runtime.'>App</a><a class='struct ' href='struct.Arg.html' title='The abstract representation of a command line argument used by the consumer of the library. '>Arg</a><a class='struct current' href='struct.ArgMatches.html' title='Used to get information about the arguments that where supplied to the program at runtime.'>ArgMatches</a></div>
<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>
</section>
<nav class="sub">
@ -43,18 +43,21 @@
<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><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-4188' href='../src/clap/argmatches.rs.html#49-59'>[src]</a></span></h1>
</span><a id='src-4545' href='../src/clap/argmatches.rs.html#61-67'>[src]</a></span></h1>
<pre class='rust struct'>pub struct ArgMatches {
pub name: &amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>,
pub matches_of: &amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>,
pub flags: <a class='struct' href='http://doc.rust-lang.org/nightly/std/collections/hash/map/struct.HashMap.html' title='std::collections::hash::map::HashMap'>HashMap</a>&lt;&amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>, FlagArg&gt;,
pub opts: <a class='struct' href='http://doc.rust-lang.org/nightly/std/collections/hash/map/struct.HashMap.html' title='std::collections::hash::map::HashMap'>HashMap</a>&lt;&amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>, OptArg&gt;,
pub positionals: <a class='struct' href='http://doc.rust-lang.org/nightly/std/collections/hash/map/struct.HashMap.html' title='std::collections::hash::map::HashMap'>HashMap</a>&lt;&amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>, PosArg&gt;,
pub subcommand: <a class='enum' href='http://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a>&lt;<a href='http://doc.rust-lang.org/nightly/std/primitive.tuple.html'>(&amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>, <a class='struct' href='http://doc.rust-lang.org/nightly/alloc/boxed/struct.Box.html' title='alloc::boxed::Box'>Box</a>&lt;<a class='struct' href='../clap/struct.SubCommand.html' title='clap::SubCommand'>SubCommand</a>&gt;)</a>&gt;,
}</pre><div class='docblock'><p>Used to get information about the arguments that
where supplied to the program at runtime.</p>
<p>Fields of <code>ArgMatches</code> aren&#39;t designed to be used directly, only
the methods in order to query information.</p>
<pre id='rust-example-rendered' class='rust '>
<h1 id="example" class='section-header'><a
href="#example">Example</a></h1><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='comment'>// adding of arguments and configuration goes here...</span>
.<span class='ident'>get_matches</span>();
@ -80,15 +83,26 @@ the methods in order to query information.</p>
} <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 class='fields'>Fields</h2>
<table><tr><td id='structfield.name'><a class='stability Unmarked' title='No stability level'></a><code>name</code></td><td></td></tr><tr><td id='structfield.flags'><a class='stability Unmarked' title='No stability level'></a><code>flags</code></td><td></td></tr><tr><td id='structfield.opts'><a class='stability Unmarked' title='No stability level'></a><code>opts</code></td><td></td></tr><tr><td id='structfield.positionals'><a class='stability Unmarked' title='No stability level'></a><code>positionals</code></td><td></td></tr></table><h2 id='methods'>Methods</h2><h3 class='impl'><a class='stability Unmarked' title='No stability level'></a><code>impl <a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a></code></h3><div class='impl-items'><h4 id='method.new' class='method'><a class='stability Unmarked' title='No stability level'></a><code>fn <a href='#method.new' class='fnname'>new</a>(app: &amp;<a class='struct' href='../clap/struct.App.html' title='clap::App'>App</a>) -&gt; <a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a></code></h4>
<table><tr><td id='structfield.matches_of'><a class='stability Unmarked' title='No stability level'></a><code>matches_of</code></td><td></td></tr><tr><td id='structfield.flags'><a class='stability Unmarked' title='No stability level'></a><code>flags</code></td><td></td></tr><tr><td id='structfield.opts'><a class='stability Unmarked' title='No stability level'></a><code>opts</code></td><td></td></tr><tr><td id='structfield.positionals'><a class='stability Unmarked' title='No stability level'></a><code>positionals</code></td><td></td></tr><tr><td id='structfield.subcommand'><a class='stability Unmarked' title='No stability level'></a><code>subcommand</code></td><td></td></tr></table><h2 id='methods'>Methods</h2><h3 class='impl'><a class='stability Unmarked' title='No stability level'></a><code>impl <a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a></code></h3><div class='impl-items'><h4 id='method.new' class='method'><a class='stability Unmarked' title='No stability level'></a><code>fn <a href='#method.new' class='fnname'>new</a>(name: &amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>) -&gt; <a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a></code></h4>
<div class='docblock'><p>Creates a new instance of <code>ArgMatches</code>. This ins&#39;t called directly, but
through the <code>.get_matches()</code> method of <code>App</code></p>
<p>Example:</p>
<pre id='rust-example-rendered' class='rust '>
<h1 id="example" class='section-header'><a
href="#example">Example</a></h1><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;myprog&quot;</span>).<span class='ident'>get_matches</span>();
</pre>
</div><h4 id='method.value_of' class='method'><a class='stability Unmarked' title='No stability level'></a><code>fn <a href='#method.value_of' class='fnname'>value_of</a>(&amp;self, name: &amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>) -&gt; <a class='enum' href='http://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a>&lt;&amp;<a class='struct' href='http://doc.rust-lang.org/nightly/collections/string/struct.String.html' title='collections::string::String'>String</a>&gt;</code></h4>
@ -96,8 +110,8 @@ through the <code>.get_matches()</code> method of <code>App</code></p>
an additional value at runtime). If the option wasn&#39;t present at runtime
it returns <code>None</code></p>
<p>Example:</p>
<pre id='rust-example-rendered' class='rust '>
<h1 id="example" class='section-header'><a
href="#example">Example</a></h1><pre id='rust-example-rendered' class='rust '>
<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>);
}
@ -106,8 +120,8 @@ it returns <code>None</code></p>
<div class='docblock'><p>Checks if a flag was argument was supplied at runtime. <strong>DOES NOT</strong> work for
option or positional arguments (use <code>.value_of()</code> instead)</p>
<p>Example:</p>
<pre id='rust-example-rendered' class='rust '>
<h1 id="example" class='section-header'><a
href="#example">Example</a></h1><pre id='rust-example-rendered' class='rust '>
<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>);
}
@ -121,14 +135,34 @@ return <code>0</code>, if a flag doesn&#39;t allow multiple occurrences, it will
return <code>1</code> no matter how many times it occurred (unless it wasn&#39;t prsent)
at all.</p>
<p>Example:</p>
<pre id='rust-example-rendered' class='rust '>
<h1 id="example" class='section-header'><a
href="#example">Example</a></h1><pre id='rust-example-rendered' class='rust '>
<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'><a class='stability Unmarked' title='No stability level'></a><code>fn <a href='#method.subcommand_matches' class='fnname'>subcommand_matches</a>(&amp;self, name: &amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>) -&gt; <a class='enum' href='http://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>If a subcommand was found, returns the ArgMatches struct associated with it&#39;s matches</p>
<h1 id="example" class='section-header'><a
href="#example">Example</a></h1><pre id='rust-example-rendered' class='rust '>
<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'><a class='stability Unmarked' title='No stability level'></a><code>fn <a href='#method.subcommand_name' class='fnname'>subcommand_name</a>(&amp;self) -&gt; <a class='enum' href='http://doc.rust-lang.org/nightly/core/option/enum.Option.html' title='core::option::Option'>Option</a>&lt;&amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>&gt;</code></h4>
<div class='docblock'><p>If a subcommand was found, returns the name associated with it</p>
<h1 id="example" class='section-header'><a
href="#example">Example</a></h1><pre id='rust-example-rendered' class='rust '>
<span class='kw'>match</span> <span class='ident'>app_matches</span>.<span class='ident'>subcommand_</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></div></section>
<section id='search' class="content hidden"></section>

View file

@ -0,0 +1,121 @@
<!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 `SubCommand` struct in crate `clap`.">
<meta name="keywords" content="rust, rustlang, rust-lang, SubCommand">
<title>clap::SubCommand - 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: 'SubCommand', ty: 'struct', 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 struct">
<h1 class='fqn'><span class='in-band'>Struct <a href='index.html'>clap</a>::<wbr><a class='struct' href=''>SubCommand</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-5519' href='../src/clap/subcommand.rs.html#23-26'>[src]</a></span></h1>
<pre class='rust struct'>pub struct SubCommand {
pub name: &amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>,
pub matches: <a class='struct' href='../clap/struct.ArgMatches.html' title='clap::ArgMatches'>ArgMatches</a>,
}</pre><div class='docblock'><p>The abstract representation of a command line subcommand used by the consumer of the library.</p>
<p>This struct is used by the library consumer and describes all the valid options of the subcommand for
their program. SubCommands are treated like &quot;sub apps&quot; and contain all the same possibilities (such as
their own arguments and subcommands).</p>
<p># Example</p>
<pre id='rust-example-rendered' class='rust '>
<span class='ident'>SubCommand</span>::<span class='ident'>new</span>(<span class='string'>&quot;conifg&quot;</span>)
.<span class='ident'>about</span>(<span class='string'>&quot;Used for configuration&quot;</span>)
.<span class='ident'>arg</span>(<span class='ident'>Arg</span>::<span class='ident'>new</span>(<span class='string'>&quot;config_file&quot;</span>)
.<span class='ident'>help</span>(<span class='string'>&quot;The configuration file to use&quot;</span>)
.<span class='ident'>index</span>(<span class='number'>1</span>))
</pre>
</div><h2 class='fields'>Fields</h2>
<table><tr><td id='structfield.name'><a class='stability Unmarked' title='No stability level'></a><code>name</code></td><td></td></tr><tr><td id='structfield.matches'><a class='stability Unmarked' title='No stability level'></a><code>matches</code></td><td></td></tr></table><h2 id='methods'>Methods</h2><h3 class='impl'><a class='stability Unmarked' title='No stability level'></a><code>impl <a class='struct' href='../clap/struct.SubCommand.html' title='clap::SubCommand'>SubCommand</a></code></h3><div class='impl-items'><h4 id='method.new' class='method'><a class='stability Unmarked' title='No stability level'></a><code>fn <a href='#method.new' class='fnname'>new</a>(name: &amp;'static <a href='http://doc.rust-lang.org/nightly/std/primitive.str.html'>str</a>) -&gt; <a class='struct' href='../clap/struct.App.html' title='clap::App'>App</a></code></h4>
<div class='docblock'><p>Creates a new instance of a subcommand requiring a name. Will be displayed
to the user when they print version or help and usage information.</p>
<h1 id="example" class='section-header'><a
href="#example">Example</a></h1><pre id='rust-example-rendered' class='rust '>
<span class='ident'>SubCommand</span>::<span class='ident'>new</span>(<span class='string'>&quot;config&quot;</span>)
</pre>
</div></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>

View file

View file

@ -0,0 +1 @@
initSidebarItems({"struct":[["SubCommand","The abstract representation of a command line subcommand used by the consumer of the library. "]]});

View file

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=../../clap/struct.SubCommand.html">
</head>
<body>
<p>Redirecting to <a href="../../clap/struct.SubCommand.html">../../clap/struct.SubCommand.html</a>...</p>
<script>location.replace("../../clap/struct.SubCommand.html" + location.search + location.hash);</script>
</body>
</html>

View file

@ -83,7 +83,7 @@ h2 {
h3 {
font-size: 1.3em;
}
h1, h2, h3:not(.impl):not(.method):not(.type), h4:not(.method):not(.type) {
h1, h2, h3:not(.impl):not(.method):not(.type):not(.tymethod), h4:not(.method):not(.type):not(.tymethod) {
color: black;
font-weight: 500;
margin: 20px 0 15px 0;
@ -93,7 +93,7 @@ h1.fqn {
border-bottom: 1px dashed #D5D5D5;
margin-top: 0;
}
h2, h3:not(.impl):not(.method):not(.type), h4:not(.method):not(.type) {
h2, h3:not(.impl):not(.method):not(.type):not(.tymethod), h4:not(.method):not(.type):not(.tymethod) {
border-bottom: 1px solid #DDDDDD;
}
h3.impl, h3.method, h4.method, h3.type, h4.type {

View file

@ -15,6 +15,27 @@
"use strict";
var resizeTimeout, interval;
// This mapping table should match the discriminants of
// `rustdoc::html::item_type::ItemType` type in Rust.
var itemTypes = ["mod",
"externcrate",
"import",
"struct",
"enum",
"fn",
"type",
"static",
"trait",
"impl",
"tymethod",
"method",
"structfield",
"variant",
"macro",
"primitive",
"associatedtype",
"constant"];
$('.js-only').removeClass('js-only');
function getQueryStringParams() {
@ -33,23 +54,6 @@
return window.history && typeof window.history.pushState === "function";
}
function resizeShortBlocks() {
if (resizeTimeout) {
clearTimeout(resizeTimeout);
}
resizeTimeout = setTimeout(function() {
var contentWidth = $('.content').width();
$('.docblock.short').width(function() {
return contentWidth - 40 - $(this).prev().width();
}).addClass('nowrap');
$('.summary-column').width(function() {
return contentWidth - 40 - $(this).prev().width();
})
}, 150);
}
resizeShortBlocks();
$(window).on('resize', resizeShortBlocks);
function highlightSourceLines(ev) {
var i, from, to, match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
if (match) {
@ -552,27 +556,6 @@
showResults(results);
}
// This mapping table should match the discriminants of
// `rustdoc::html::item_type::ItemType` type in Rust.
var itemTypes = ["mod",
"externcrate",
"import",
"struct",
"enum",
"fn",
"type",
"static",
"trait",
"impl",
"tymethod",
"method",
"structfield",
"variant",
"macro",
"primitive",
"associatedtype",
"constant"];
function itemTypeFromName(typename) {
for (var i = 0; i < itemTypes.length; ++i) {
if (itemTypes[i] === typename) return i;
@ -708,6 +691,50 @@
window.initSearch = initSearch;
// delayed sidebar rendering.
function initSidebarItems(items) {
var sidebar = $('.sidebar');
var current = window.sidebarCurrent;
function block(shortty, longty) {
var filtered = items[shortty];
if (!filtered) return;
var div = $('<div>').attr('class', 'block ' + shortty);
div.append($('<h2>').text(longty));
for (var i = 0; i < filtered.length; ++i) {
var item = filtered[i];
var name = item[0];
var desc = item[1]; // can be null
var klass = shortty;
if (name === current.name && shortty == current.ty) {
klass += ' current';
}
var path;
if (shortty === 'mod') {
path = name + '/index.html';
} else {
path = shortty + '.' + name + '.html';
}
div.append($('<a>', {'href': current.relpath + path,
'title': desc,
'class': klass}).text(name));
}
sidebar.append(div);
}
block("mod", "Modules");
block("struct", "Structs");
block("enum", "Enums");
block("trait", "Traits");
block("fn", "Functions");
block("macro", "Macros");
}
window.initSidebarItems = initSidebarItems;
window.register_implementors = function(imp) {
var list = $('#implementors-list');
var libs = Object.getOwnPropertyNames(imp);

View file

@ -15,7 +15,7 @@
if (window.playgroundUrl) {
$('pre.rust').hover(function() {
var a = $('<a>').text('⇱').attr('class', 'test-arrow');
var code = $(this).siblings(".rusttest").text();
var code = $(this).prev(".rusttest").text();
a.attr('href', window.playgroundUrl + '?code=' +
encodeURIComponent(code));
a.attr('target', '_blank');

File diff suppressed because one or more lines are too long

View file

@ -9,7 +9,7 @@
<title>app.rs.html -- source</title>
<link rel="stylesheet" type="text/css" href="../../main.css">
<link rel="stylesheet" type="text/css" href="../../../main.css">
@ -761,6 +761,55 @@
<span id="719">719</span>
<span id="720">720</span>
<span id="721">721</span>
<span id="722">722</span>
<span id="723">723</span>
<span id="724">724</span>
<span id="725">725</span>
<span id="726">726</span>
<span id="727">727</span>
<span id="728">728</span>
<span id="729">729</span>
<span id="730">730</span>
<span id="731">731</span>
<span id="732">732</span>
<span id="733">733</span>
<span id="734">734</span>
<span id="735">735</span>
<span id="736">736</span>
<span id="737">737</span>
<span id="738">738</span>
<span id="739">739</span>
<span id="740">740</span>
<span id="741">741</span>
<span id="742">742</span>
<span id="743">743</span>
<span id="744">744</span>
<span id="745">745</span>
<span id="746">746</span>
<span id="747">747</span>
<span id="748">748</span>
<span id="749">749</span>
<span id="750">750</span>
<span id="751">751</span>
<span id="752">752</span>
<span id="753">753</span>
<span id="754">754</span>
<span id="755">755</span>
<span id="756">756</span>
<span id="757">757</span>
<span id="758">758</span>
<span id="759">759</span>
<span id="760">760</span>
<span id="761">761</span>
<span id="762">762</span>
<span id="763">763</span>
<span id="764">764</span>
<span id="765">765</span>
<span id="766">766</span>
<span id="767">767</span>
<span id="768">768</span>
<span id="769">769</span>
<span id="770">770</span>
</pre><pre class='rust '>
<span class='kw'>extern</span> <span class='kw'>crate</span> <span class='ident'>libc</span>;
@ -768,12 +817,14 @@
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>collections</span>::<span class='ident'>HashMap</span>;
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>collections</span>::<span class='ident'>HashSet</span>;
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>env</span>;
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>vec</span>::<span class='ident'>IntoIter</span>;
<span class='kw'>use</span> <span class='ident'>argmatches</span>::<span class='ident'>ArgMatches</span>;
<span class='kw'>use</span> <span class='ident'>Arg</span>;
<span class='kw'>use</span> <span class='ident'>args</span>::<span class='ident'>OptArg</span>;
<span class='kw'>use</span> <span class='ident'>args</span>::<span class='ident'>FlagArg</span>;
<span class='kw'>use</span> <span class='ident'>args</span>::<span class='ident'>PosArg</span>;
<span class='kw'>use</span> <span class='ident'>subcommand</span>::<span class='ident'>SubCommand</span>;
<span class='doccomment'>/// Used to create a representation of the program and all possible command line arguments</span>
<span class='doccomment'>/// for parsing at runtime.</span>
@ -810,16 +861,19 @@
<span class='ident'>flags</span>: <span class='ident'>HashMap</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>, <span class='ident'>FlagArg</span><span class='op'>&gt;</span>,
<span class='ident'>opts</span>: <span class='ident'>HashMap</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>, <span class='ident'>OptArg</span><span class='op'>&gt;</span>,
<span class='ident'>positionals_idx</span>: <span class='ident'>BTreeMap</span><span class='op'>&lt;</span><span class='ident'>u8</span>, <span class='ident'>PosArg</span><span class='op'>&gt;</span>,
<span class='ident'>subcommands</span>: <span class='ident'>HashMap</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>, <span class='ident'>Box</span><span class='op'>&lt;</span><span class='ident'>App</span><span class='op'>&gt;&gt;</span>,
<span class='comment'>// positionals_name: HashMap&lt;&amp;&#39;static str, PosArg&gt;,</span>
<span class='ident'>needs_long_help</span>: <span class='ident'>bool</span>,
<span class='ident'>needs_long_version</span>: <span class='ident'>bool</span>,
<span class='ident'>needs_short_help</span>: <span class='ident'>bool</span>,
<span class='ident'>needs_short_version</span>: <span class='ident'>bool</span>,
<span class='ident'>needs_subcmd_help</span>: <span class='ident'>bool</span>,
<span class='ident'>required</span>: <span class='ident'>HashSet</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span><span class='op'>&gt;</span>,
<span class='ident'>arg_list</span>: <span class='ident'>HashSet</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span><span class='op'>&gt;</span>,
<span class='ident'>short_list</span>: <span class='ident'>HashSet</span><span class='op'>&lt;</span><span class='ident'>char</span><span class='op'>&gt;</span>,
<span class='ident'>long_list</span>: <span class='ident'>HashSet</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span><span class='op'>&gt;</span>,
<span class='ident'>blacklist</span>: <span class='ident'>HashSet</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span><span class='op'>&gt;</span>,
}
<span class='kw'>impl</span> <span class='ident'>App</span> {
@ -842,10 +896,12 @@
<span class='ident'>flags</span>: <span class='ident'>HashMap</span>::<span class='ident'>new</span>(),
<span class='ident'>opts</span>: <span class='ident'>HashMap</span>::<span class='ident'>new</span>(),
<span class='ident'>positionals_idx</span>: <span class='ident'>BTreeMap</span>::<span class='ident'>new</span>(),
<span class='ident'>subcommands</span>: <span class='ident'>HashMap</span>::<span class='ident'>new</span>(),
<span class='comment'>// positionals_name: HashMap::new(),</span>
<span class='ident'>needs_long_version</span>: <span class='boolval'>true</span>,
<span class='ident'>needs_long_help</span>: <span class='boolval'>true</span>,
<span class='ident'>needs_short_help</span>: <span class='boolval'>true</span>,
<span class='ident'>needs_subcmd_help</span>: <span class='boolval'>true</span>,
<span class='ident'>needs_short_version</span>: <span class='boolval'>true</span>,
<span class='ident'>required</span>: <span class='ident'>HashSet</span>::<span class='ident'>new</span>(),
<span class='ident'>arg_list</span>: <span class='ident'>HashSet</span>::<span class='ident'>new</span>(),
@ -937,15 +993,6 @@
<span class='self'>self</span>.<span class='ident'>required</span>.<span class='ident'>insert</span>(<span class='ident'>a</span>.<span class='ident'>name</span>);
}
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(<span class='ident'>i</span>) <span class='op'>=</span> <span class='ident'>a</span>.<span class='ident'>index</span> {
<span class='comment'>// self.positionals_name.insert(a.name, PosArg {</span>
<span class='comment'>// name: a.name,</span>
<span class='comment'>// index: i,</span>
<span class='comment'>// required: a.required,</span>
<span class='comment'>// help: a.help,</span>
<span class='comment'>// blacklist: a.blacklist,</span>
<span class='comment'>// requires: a.requires,</span>
<span class='comment'>// value: None</span>
<span class='comment'>// });</span>
<span class='self'>self</span>.<span class='ident'>positionals_idx</span>.<span class='ident'>insert</span>(<span class='ident'>i</span>, <span class='ident'>PosArg</span> {
<span class='ident'>name</span>: <span class='ident'>a</span>.<span class='ident'>name</span>,
<span class='ident'>index</span>: <span class='ident'>i</span>,
@ -1023,6 +1070,20 @@
<span class='self'>self</span>
}
<span class='kw'>pub</span> <span class='kw'>fn</span> <span class='ident'>subcommand</span>(<span class='kw-2'>mut</span> <span class='self'>self</span>, <span class='ident'>subcmd</span>: <span class='ident'>App</span>) <span class='op'>-&gt;</span> <span class='ident'>App</span> {
<span class='kw'>if</span> <span class='ident'>subcmd</span>.<span class='ident'>name</span> <span class='op'>==</span> <span class='string'>&quot;help&quot;</span> { <span class='self'>self</span>.<span class='ident'>needs_subcmd_help</span> <span class='op'>=</span> <span class='boolval'>false</span>; }
<span class='self'>self</span>.<span class='ident'>subcommands</span>.<span class='ident'>insert</span>(<span class='ident'>subcmd</span>.<span class='ident'>name</span>, <span class='ident'>Box</span>::<span class='ident'>new</span>(<span class='ident'>subcmd</span>));
<span class='self'>self</span>
}
<span class='kw'>pub</span> <span class='kw'>fn</span> <span class='ident'>subcommands</span>(<span class='kw-2'>mut</span> <span class='self'>self</span>, <span class='ident'>subcmds</span>: <span class='ident'>Vec</span><span class='op'>&lt;</span><span class='ident'>App</span><span class='op'>&gt;</span>) <span class='op'>-&gt;</span> <span class='ident'>App</span> {
<span class='kw'>for</span> <span class='ident'>subcmd</span> <span class='kw'>in</span> <span class='ident'>subcmds</span>.<span class='ident'>into_iter</span>() {
<span class='self'>self</span> <span class='op'>=</span> <span class='self'>self</span>.<span class='ident'>subcommand</span>(<span class='ident'>subcmd</span>);
}
<span class='self'>self</span>
}
<span class='kw'>fn</span> <span class='ident'>exit</span>(<span class='kw-2'>&amp;</span><span class='self'>self</span>) {
<span class='kw'>unsafe</span> { <span class='ident'>libc</span>::<span class='ident'>exit</span>(<span class='number'>0</span>); }
}
@ -1038,6 +1099,7 @@
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>flags</span> <span class='op'>=</span> <span class='boolval'>false</span>;
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>pos</span> <span class='op'>=</span> <span class='boolval'>false</span>;
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>opts</span> <span class='op'>=</span> <span class='boolval'>false</span>;
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>subcmds</span> <span class='op'>=</span> <span class='boolval'>false</span>;
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(<span class='ident'>author</span>) <span class='op'>=</span> <span class='self'>self</span>.<span class='ident'>author</span> {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{}&quot;</span>, <span class='ident'>author</span>);
@ -1047,11 +1109,12 @@
}
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;&quot;</span>);
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;USAGE:&quot;</span>);
<span class='macro'>print</span><span class='macro'>!</span>(<span class='string'>&quot;\t{} {} {} {}&quot;</span>, <span class='self'>self</span>.<span class='ident'>name</span>,
<span class='macro'>print</span><span class='macro'>!</span>(<span class='string'>&quot;\t{} {} {} {} {}&quot;</span>, <span class='self'>self</span>.<span class='ident'>name</span>,
<span class='kw'>if</span> <span class='op'>!</span> <span class='self'>self</span>.<span class='ident'>subcommands</span>.<span class='ident'>is_empty</span>() {<span class='ident'>subcmds</span> <span class='op'>=</span> <span class='boolval'>true</span>; <span class='string'>&quot;[SUBCOMMANDS]&quot;</span>} <span class='kw'>else</span> {<span class='string'>&quot;&quot;</span>},
<span class='kw'>if</span> <span class='op'>!</span> <span class='self'>self</span>.<span class='ident'>flags</span>.<span class='ident'>is_empty</span>() {<span class='ident'>flags</span> <span class='op'>=</span> <span class='boolval'>true</span>; <span class='string'>&quot;[FLAGS]&quot;</span>} <span class='kw'>else</span> {<span class='string'>&quot;&quot;</span>},
<span class='kw'>if</span> <span class='op'>!</span> <span class='self'>self</span>.<span class='ident'>opts</span>.<span class='ident'>is_empty</span>() {<span class='ident'>opts</span> <span class='op'>=</span> <span class='boolval'>true</span>; <span class='string'>&quot;[OPTIONS]&quot;</span>} <span class='kw'>else</span> {<span class='string'>&quot;&quot;</span>},
<span class='kw'>if</span> <span class='op'>!</span> <span class='self'>self</span>.<span class='ident'>positionals_idx</span>.<span class='ident'>is_empty</span>() {<span class='ident'>pos</span> <span class='op'>=</span> <span class='boolval'>true</span>; <span class='string'>&quot;[POSITIONAL]&quot;</span>} <span class='kw'>else</span> {<span class='string'>&quot;&quot;</span>});
<span class='kw'>if</span> <span class='ident'>flags</span> <span class='op'>||</span> <span class='ident'>opts</span> <span class='op'>||</span> <span class='ident'>pos</span> {
<span class='kw'>if</span> <span class='ident'>flags</span> <span class='op'>||</span> <span class='ident'>opts</span> <span class='op'>||</span> <span class='ident'>pos</span> <span class='op'>||</span> <span class='ident'>subcmds</span> {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;&quot;</span>);
}
<span class='kw'>if</span> <span class='ident'>flags</span> {
@ -1083,6 +1146,14 @@
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(<span class='ident'>h</span>) <span class='op'>=</span> <span class='ident'>v</span>.<span class='ident'>help</span> {<span class='ident'>h</span>} <span class='kw'>else</span> {<span class='string'>&quot; &quot;</span>} );
}
}
<span class='kw'>if</span> <span class='ident'>subcmds</span> {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;&quot;</span>);
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;SUBCOMMANDS:&quot;</span>);
<span class='kw'>for</span> <span class='ident'>sc</span> <span class='kw'>in</span> <span class='self'>self</span>.<span class='ident'>subcommands</span>.<span class='ident'>values</span>() {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;\t{}\t\t{}&quot;</span>, <span class='ident'>sc</span>.<span class='ident'>name</span>,
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(<span class='ident'>a</span>) <span class='op'>=</span> <span class='ident'>sc</span>.<span class='ident'>about</span> {<span class='ident'>a</span>} <span class='kw'>else</span> {<span class='string'>&quot; &quot;</span>} );
}
}
<span class='self'>self</span>.<span class='ident'>exit</span>();
}
@ -1349,17 +1420,19 @@
<span class='ident'>occurrences</span>: <span class='number'>1</span>
});
}
<span class='kw'>if</span> <span class='self'>self</span>.<span class='ident'>needs_subcmd_help</span> {
<span class='self'>self</span>.<span class='ident'>subcommands</span>.<span class='ident'>insert</span>(<span class='string'>&quot;help&quot;</span>, <span class='ident'>Box</span>::<span class='ident'>new</span>(<span class='ident'>App</span>::<span class='ident'>new</span>(<span class='string'>&quot;help&quot;</span>).<span class='ident'>about</span>(<span class='string'>&quot;Prints this message&quot;</span>)));
}
}
<span class='kw'>pub</span> <span class='kw'>fn</span> <span class='ident'>get_matches</span>(<span class='kw-2'>mut</span> <span class='self'>self</span>) <span class='op'>-&gt;</span> <span class='ident'>ArgMatches</span> {
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>matches</span> <span class='op'>=</span> <span class='ident'>ArgMatches</span>::<span class='ident'>new</span>(<span class='kw-2'>&amp;</span><span class='self'>self</span>);
<span class='kw'>fn</span> <span class='ident'>get_matches_from</span>(<span class='kw-2'>&amp;</span><span class='kw-2'>mut</span> <span class='self'>self</span>, <span class='ident'>matches</span>: <span class='kw-2'>&amp;</span><span class='kw-2'>mut</span> <span class='ident'>ArgMatches</span>, <span class='ident'>it</span>: <span class='kw-2'>&amp;</span><span class='kw-2'>mut</span> <span class='ident'>IntoIter</span><span class='op'>&lt;</span><span class='ident'>String</span><span class='op'>&gt;</span>) {
<span class='self'>self</span>.<span class='ident'>create_help_and_version</span>();
<span class='comment'>// let mut needs_val = false;</span>
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>subcmd_name</span>: <span class='prelude-ty'>Option</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span><span class='op'>&gt;</span> <span class='op'>=</span> <span class='prelude-val'>None</span>;
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>needs_val_of</span>: <span class='prelude-ty'>Option</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span><span class='op'>&gt;</span> <span class='op'>=</span> <span class='prelude-val'>None</span>;
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>pos_counter</span> <span class='op'>=</span> <span class='number'>1</span>;
<span class='kw'>for</span> <span class='ident'>arg</span> <span class='kw'>in</span> <span class='ident'>env</span>::<span class='ident'>args</span>().<span class='ident'>collect</span>::<span class='op'>&lt;</span><span class='ident'>Vec</span><span class='op'>&lt;</span>_<span class='op'>&gt;&gt;</span>().<span class='ident'>tail</span>() {
<span class='kw'>while</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(<span class='ident'>arg</span>) <span class='op'>=</span> <span class='ident'>it</span>.<span class='ident'>next</span>() {
<span class='kw'>let</span> <span class='ident'>arg_slice</span> <span class='op'>=</span> <span class='ident'>arg</span>.<span class='ident'>as_slice</span>();
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>skip</span> <span class='op'>=</span> <span class='boolval'>false</span>;
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(<span class='ident'>nvo</span>) <span class='op'>=</span> <span class='ident'>needs_val_of</span> {
@ -1412,12 +1485,19 @@
}
<span class='kw'>if</span> <span class='ident'>arg_slice</span>.<span class='ident'>starts_with</span>(<span class='string'>&quot;--&quot;</span>) {
<span class='comment'>// Single flag, or option long version</span>
<span class='ident'>needs_val_of</span> <span class='op'>=</span> <span class='self'>self</span>.<span class='ident'>parse_long_arg</span>(<span class='kw-2'>&amp;</span><span class='kw-2'>mut</span> <span class='ident'>matches</span>, <span class='kw-2'>&amp;</span><span class='ident'>arg</span>);
<span class='ident'>needs_val_of</span> <span class='op'>=</span> <span class='self'>self</span>.<span class='ident'>parse_long_arg</span>(<span class='ident'>matches</span>, <span class='kw-2'>&amp;</span><span class='ident'>arg</span>);
} <span class='kw'>else</span> <span class='kw'>if</span> <span class='ident'>arg_slice</span>.<span class='ident'>starts_with</span>(<span class='string'>&quot;-&quot;</span>) {
<span class='ident'>needs_val_of</span> <span class='op'>=</span> <span class='self'>self</span>.<span class='ident'>parse_short_arg</span>(<span class='kw-2'>&amp;</span><span class='kw-2'>mut</span> <span class='ident'>matches</span>, <span class='kw-2'>&amp;</span><span class='ident'>arg</span>);
<span class='ident'>needs_val_of</span> <span class='op'>=</span> <span class='self'>self</span>.<span class='ident'>parse_short_arg</span>(<span class='ident'>matches</span>, <span class='kw-2'>&amp;</span><span class='ident'>arg</span>);
} <span class='kw'>else</span> {
<span class='comment'>// Positional</span>
<span class='comment'>// Positional or Subcommand</span>
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(<span class='ident'>sca</span>) <span class='op'>=</span> <span class='self'>self</span>.<span class='ident'>subcommands</span>.<span class='ident'>get</span>(<span class='ident'>arg_slice</span>) {
<span class='kw'>if</span> <span class='ident'>sca</span>.<span class='ident'>name</span> <span class='op'>==</span> <span class='string'>&quot;help&quot;</span> {
<span class='self'>self</span>.<span class='ident'>print_help</span>();
}
<span class='ident'>subcmd_name</span> <span class='op'>=</span> <span class='prelude-val'>Some</span>(<span class='ident'>sca</span>.<span class='ident'>name</span>);
<span class='kw'>break</span>;
}
<span class='kw'>if</span> <span class='self'>self</span>.<span class='ident'>positionals_idx</span>.<span class='ident'>is_empty</span>() { <span class='comment'>// || self.positionals_name.is_empty() {</span>
<span class='self'>self</span>.<span class='ident'>report_error</span>(
@ -1480,6 +1560,24 @@
<span class='self'>self</span>.<span class='ident'>validate_blacklist</span>(<span class='kw-2'>&amp;</span><span class='ident'>matches</span>);
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(<span class='ident'>sc_name</span>) <span class='op'>=</span> <span class='ident'>subcmd_name</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='kw-2'>mut</span> <span class='ident'>sc</span>) <span class='op'>=</span> <span class='self'>self</span>.<span class='ident'>subcommands</span>.<span class='ident'>get_mut</span>(<span class='ident'>sc_name</span>) {
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>new_matches</span> <span class='op'>=</span> <span class='ident'>ArgMatches</span>::<span class='ident'>new</span>(<span class='ident'>sc_name</span>);
<span class='ident'>sc</span>.<span class='ident'>get_matches_from</span>(<span class='kw-2'>&amp;</span><span class='kw-2'>mut</span> <span class='ident'>new_matches</span>, <span class='ident'>it</span>);
<span class='ident'>matches</span>.<span class='ident'>subcommand</span> <span class='op'>=</span> <span class='prelude-val'>Some</span>((<span class='ident'>sc_name</span>, <span class='ident'>Box</span>::<span class='ident'>new</span>(<span class='ident'>SubCommand</span>{
<span class='ident'>name</span>: <span class='ident'>sc_name</span>,
<span class='ident'>matches</span>: <span class='ident'>new_matches</span>})));
}
}
}
<span class='kw'>pub</span> <span class='kw'>fn</span> <span class='ident'>get_matches</span>(<span class='kw-2'>mut</span> <span class='self'>self</span>) <span class='op'>-&gt;</span> <span class='ident'>ArgMatches</span> {
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>matches</span> <span class='op'>=</span> <span class='ident'>ArgMatches</span>::<span class='ident'>new</span>(<span class='self'>self</span>.<span class='ident'>name</span>);
<span class='kw'>let</span> <span class='ident'>args</span> <span class='op'>=</span> <span class='ident'>env</span>::<span class='ident'>args</span>().<span class='ident'>collect</span>::<span class='op'>&lt;</span><span class='ident'>Vec</span><span class='op'>&lt;</span>_<span class='op'>&gt;&gt;</span>();
<span class='self'>self</span>.<span class='ident'>get_matches_from</span>(<span class='kw-2'>&amp;</span><span class='kw-2'>mut</span> <span class='ident'>matches</span>, <span class='kw-2'>&amp;</span><span class='kw-2'>mut</span> <span class='ident'>args</span>.<span class='ident'>into_iter</span>());
<span class='ident'>matches</span>
}
}
@ -1523,13 +1621,13 @@
<script>
window.rootPath = "../../";
window.rootPath = "../../../";
window.currentCrate = "clap";
window.playgroundUrl = "";
</script>
<script src="../../jquery.js"></script>
<script src="../../main.js"></script>
<script src="../../../jquery.js"></script>
<script src="../../../main.js"></script>
<script async src="../../search-index.js"></script>
<script async src="../../../search-index.js"></script>
</body>
</html>

View file

@ -9,7 +9,7 @@
<title>arg.rs.html -- source</title>
<link rel="stylesheet" type="text/css" href="../../main.css">
<link rel="stylesheet" type="text/css" href="../../../main.css">
@ -387,7 +387,7 @@
<span class='doccomment'>/// and then evaluates the settings the consumer provided and determines the concret</span>
<span class='doccomment'>/// argument struct to use when parsing.</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// Example:</span>
<span class='doccomment'>/// # Example</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// ```no_run</span>
<span class='doccomment'>/// # use clap::{App, Arg};</span>
@ -757,13 +757,13 @@
<script>
window.rootPath = "../../";
window.rootPath = "../../../";
window.currentCrate = "clap";
window.playgroundUrl = "";
</script>
<script src="../../jquery.js"></script>
<script src="../../main.js"></script>
<script src="../../../jquery.js"></script>
<script src="../../../main.js"></script>
<script async src="../../search-index.js"></script>
<script async src="../../../search-index.js"></script>
</body>
</html>

View file

@ -9,7 +9,7 @@
<title>argmatches.rs.html -- source</title>
<link rel="stylesheet" type="text/css" href="../../main.css">
<link rel="stylesheet" type="text/css" href="../../../main.css">
@ -198,12 +198,61 @@
<span id="156">156</span>
<span id="157">157</span>
<span id="158">158</span>
<span id="159">159</span>
<span id="160">160</span>
<span id="161">161</span>
<span id="162">162</span>
<span id="163">163</span>
<span id="164">164</span>
<span id="165">165</span>
<span id="166">166</span>
<span id="167">167</span>
<span id="168">168</span>
<span id="169">169</span>
<span id="170">170</span>
<span id="171">171</span>
<span id="172">172</span>
<span id="173">173</span>
<span id="174">174</span>
<span id="175">175</span>
<span id="176">176</span>
<span id="177">177</span>
<span id="178">178</span>
<span id="179">179</span>
<span id="180">180</span>
<span id="181">181</span>
<span id="182">182</span>
<span id="183">183</span>
<span id="184">184</span>
<span id="185">185</span>
<span id="186">186</span>
<span id="187">187</span>
<span id="188">188</span>
<span id="189">189</span>
<span id="190">190</span>
<span id="191">191</span>
<span id="192">192</span>
<span id="193">193</span>
<span id="194">194</span>
<span id="195">195</span>
<span id="196">196</span>
<span id="197">197</span>
<span id="198">198</span>
<span id="199">199</span>
<span id="200">200</span>
<span id="201">201</span>
<span id="202">202</span>
<span id="203">203</span>
<span id="204">204</span>
<span id="205">205</span>
<span id="206">206</span>
<span id="207">207</span>
<span id="208">208</span>
</pre><pre class='rust '>
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>collections</span>::<span class='ident'>HashMap</span>;
<span class='comment'>// use std::collections::HashSet;</span>
<span class='kw'>use</span> <span class='ident'>app</span>::<span class='ident'>App</span>;
<span class='kw'>use</span> <span class='ident'>args</span>::{ <span class='ident'>FlagArg</span>, <span class='ident'>OptArg</span>, <span class='ident'>PosArg</span> };
<span class='kw'>use</span> <span class='ident'>subcommand</span>::<span class='ident'>SubCommand</span>;
<span class='doccomment'>/// Used to get information about the arguments that</span>
<span class='doccomment'>/// where supplied to the program at runtime.</span>
@ -212,6 +261,8 @@
<span class='doccomment'>/// Fields of `ArgMatches` aren&#39;t designed to be used directly, only </span>
<span class='doccomment'>/// the methods in order to query information.</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// # Example</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// ```no_run</span>
<span class='doccomment'>/// # use clap::{App, Arg};</span>
<span class='doccomment'>/// let matches = App::new(&quot;MyApp&quot;)</span>
@ -246,40 +297,43 @@
<span class='doccomment'>/// } else {</span>
<span class='doccomment'>/// println!(&quot;Debug mode kind of on&quot;);</span>
<span class='doccomment'>/// }</span>
<span class='doccomment'>/// }</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// // You can get the sub-matches of a particular subcommand (in this case &quot;test&quot;)</span>
<span class='doccomment'>/// // 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='doccomment'>/// if let Some(ref matches) = matches.subcommand_matches(&quot;test&quot;) {</span>
<span class='doccomment'>/// if matches.is_present(&quot;list&quot;) {</span>
<span class='doccomment'>/// println!(&quot;Printing testing lists...&quot;);</span>
<span class='doccomment'>/// } else {</span>
<span class='doccomment'>/// println!(&quot;Not printing testing lists...&quot;);</span>
<span class='doccomment'>/// }</span>
<span class='doccomment'>/// }</span>
<span class='doccomment'>/// }</span>
<span class='kw'>pub</span> <span class='kw'>struct</span> <span class='ident'>ArgMatches</span> {
<span class='kw'>pub</span> <span class='ident'>name</span>: <span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>,
<span class='comment'>// pub author: Option&lt;&amp;&#39;static str&gt;,</span>
<span class='comment'>// pub about: Option&lt;&amp;&#39;static str&gt;,</span>
<span class='comment'>// pub version: Option&lt;&amp;&#39;static str&gt;,</span>
<span class='comment'>// pub required: Vec&lt;&amp;&#39;static str&gt;,</span>
<span class='comment'>// pub blacklist: HashSet&lt;&amp;&#39;static str&gt;,</span>
<span class='kw'>pub</span> <span class='ident'>matches_of</span>: <span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>,
<span class='kw'>pub</span> <span class='ident'>flags</span>: <span class='ident'>HashMap</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>, <span class='ident'>FlagArg</span><span class='op'>&gt;</span>,
<span class='kw'>pub</span> <span class='ident'>opts</span>: <span class='ident'>HashMap</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>, <span class='ident'>OptArg</span><span class='op'>&gt;</span>,
<span class='kw'>pub</span> <span class='ident'>positionals</span>: <span class='ident'>HashMap</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>, <span class='ident'>PosArg</span><span class='op'>&gt;</span>,
<span class='kw'>pub</span> <span class='ident'>subcommand</span>: <span class='prelude-ty'>Option</span><span class='op'>&lt;</span>(<span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>, <span class='ident'>Box</span><span class='op'>&lt;</span><span class='ident'>SubCommand</span><span class='op'>&gt;</span>)<span class='op'>&gt;</span>
}
<span class='kw'>impl</span> <span class='ident'>ArgMatches</span> {
<span class='doccomment'>/// Creates a new instance of `ArgMatches`. This ins&#39;t called directly, but</span>
<span class='doccomment'>/// through the `.get_matches()` method of `App`</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// Example:</span>
<span class='doccomment'>/// # Example</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// ```no_run</span>
<span class='doccomment'>/// # use clap::{App, Arg};</span>
<span class='doccomment'>/// let matches = App::new(&quot;myprog&quot;).get_matches();</span>
<span class='doccomment'>/// ```</span>
<span class='kw'>pub</span> <span class='kw'>fn</span> <span class='ident'>new</span>(<span class='ident'>app</span>: <span class='kw-2'>&amp;</span><span class='ident'>App</span>) <span class='op'>-&gt;</span> <span class='ident'>ArgMatches</span> {
<span class='kw'>pub</span> <span class='kw'>fn</span> <span class='ident'>new</span>(<span class='ident'>name</span>: <span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>) <span class='op'>-&gt;</span> <span class='ident'>ArgMatches</span> {
<span class='ident'>ArgMatches</span> {
<span class='ident'>name</span>: <span class='ident'>app</span>.<span class='ident'>name</span>,
<span class='ident'>matches_of</span>: <span class='ident'>name</span>,
<span class='ident'>flags</span>: <span class='ident'>HashMap</span>::<span class='ident'>new</span>(),
<span class='ident'>opts</span>: <span class='ident'>HashMap</span>::<span class='ident'>new</span>(),
<span class='ident'>positionals</span>: <span class='ident'>HashMap</span>::<span class='ident'>new</span>(),
<span class='comment'>// required: vec![],</span>
<span class='comment'>// blacklist: HashSet::new(),</span>
<span class='comment'>// about: app.about,</span>
<span class='comment'>// author: app.author,</span>
<span class='comment'>// version: app.version,</span>
<span class='ident'>subcommand</span>: <span class='prelude-val'>None</span>
}
}
@ -287,7 +341,7 @@
<span class='doccomment'>/// an additional value at runtime). If the option wasn&#39;t present at runtime</span>
<span class='doccomment'>/// it returns `None`</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// Example:</span>
<span class='doccomment'>/// # Example</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// ```no_run</span>
<span class='doccomment'>/// # use clap::{App, Arg};</span>
@ -314,7 +368,7 @@
<span class='doccomment'>/// option or positional arguments (use `.value_of()` instead)</span>
<span class='doccomment'>///</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// Example:</span>
<span class='doccomment'>/// # Example</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// ```no_run</span>
<span class='doccomment'>/// # use clap::{App, Arg};</span>
@ -324,9 +378,14 @@
<span class='doccomment'>/// }</span>
<span class='doccomment'>/// ```</span>
<span class='kw'>pub</span> <span class='kw'>fn</span> <span class='ident'>is_present</span>(<span class='kw-2'>&amp;</span><span class='self'>self</span>, <span class='ident'>name</span>: <span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>) <span class='op'>-&gt;</span> <span class='ident'>bool</span> {
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>(_) <span class='op'>=</span> <span class='self'>self</span>.<span class='ident'>flags</span>.<span class='ident'>get</span>(<span class='ident'>name</span>) {
<span class='kw'>return</span> <span class='boolval'>true</span>;
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>((<span class='ident'>sc_name</span>, _ )) <span class='op'>=</span> <span class='self'>self</span>.<span class='ident'>subcommand</span> {
<span class='kw'>if</span> <span class='ident'>sc_name</span> <span class='op'>==</span> <span class='ident'>name</span> { <span class='kw'>return</span> <span class='boolval'>true</span>; }
}
<span class='kw'>if</span> <span class='self'>self</span>.<span class='ident'>flags</span>.<span class='ident'>contains_key</span>(<span class='ident'>name</span>) <span class='op'>||</span>
<span class='self'>self</span>.<span class='ident'>opts</span>.<span class='ident'>contains_key</span>(<span class='ident'>name</span>) <span class='op'>||</span>
<span class='self'>self</span>.<span class='ident'>positionals</span>.<span class='ident'>contains_key</span>(<span class='ident'>name</span>) {
<span class='kw'>return</span> <span class='boolval'>true</span>;
}
<span class='boolval'>false</span>
}
@ -339,7 +398,7 @@
<span class='doccomment'>/// at all.</span>
<span class='doccomment'>///</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// Example:</span>
<span class='doccomment'>/// # Example</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// ```no_run</span>
<span class='doccomment'>/// # use clap::{App, Arg};</span>
@ -356,6 +415,47 @@
}
<span class='number'>0</span>
}
<span class='doccomment'>/// If a subcommand was found, returns the ArgMatches struct associated with it&#39;s matches</span>
<span class='doccomment'>///</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// # Example</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// ```no_run</span>
<span class='doccomment'>/// # use clap::{App, Arg, SubCommand};</span>
<span class='doccomment'>/// # let matches = App::new(&quot;myapp&quot;).subcommand(SubCommand::new(&quot;test&quot;)).get_matches();</span>
<span class='doccomment'>/// if let Some(matches) = app_matches.subcommand_matches(&quot;test&quot;) {</span>
<span class='doccomment'>/// // Use matches as normal</span>
<span class='doccomment'>/// }</span>
<span class='doccomment'>/// ```</span>
<span class='kw'>pub</span> <span class='kw'>fn</span> <span class='ident'>subcommand_matches</span>(<span class='kw-2'>&amp;</span><span class='self'>self</span>, <span class='ident'>name</span>: <span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>) <span class='op'>-&gt;</span> <span class='prelude-ty'>Option</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='ident'>ArgMatches</span><span class='op'>&gt;</span> {
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>( ( <span class='ident'>sc_name</span>, <span class='kw-2'>ref</span> <span class='ident'>sc</span>)) <span class='op'>=</span> <span class='self'>self</span>.<span class='ident'>subcommand</span> {
<span class='kw'>if</span> <span class='ident'>sc_name</span> <span class='op'>!=</span> <span class='ident'>name</span> { <span class='kw'>return</span> <span class='prelude-val'>None</span>; }
<span class='kw'>return</span> <span class='prelude-val'>Some</span>(<span class='kw-2'>&amp;</span><span class='ident'>sc</span>.<span class='ident'>matches</span>);
}
<span class='prelude-val'>None</span>
}
<span class='doccomment'>/// If a subcommand was found, returns the name associated with it</span>
<span class='doccomment'>///</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// # Example</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// ```no_run</span>
<span class='doccomment'>/// # use clap::{App, Arg, SubCommand};</span>
<span class='doccomment'>/// # let matches = App::new(&quot;myapp&quot;).subcommand(SubCommand::new(&quot;test&quot;)).get_matches();</span>
<span class='doccomment'>/// match app_matches.subcommand_() {</span>
<span class='doccomment'>/// Some(&quot;test&quot;) =&gt; {}, // test was used</span>
<span class='doccomment'>/// Some(&quot;config&quot;) =&gt; {}, // config was used</span>
<span class='doccomment'>/// _ =&gt; {}, // Either no subcommand or one not tested for...</span>
<span class='doccomment'>/// }</span>
<span class='doccomment'>/// ```</span>
<span class='kw'>pub</span> <span class='kw'>fn</span> <span class='ident'>subcommand_name</span>(<span class='kw-2'>&amp;</span><span class='self'>self</span>) <span class='op'>-&gt;</span> <span class='prelude-ty'>Option</span><span class='op'>&lt;</span><span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span><span class='op'>&gt;</span> {
<span class='kw'>if</span> <span class='kw'>let</span> <span class='prelude-val'>Some</span>((<span class='ident'>name</span>, _)) <span class='op'>=</span> <span class='self'>self</span>.<span class='ident'>subcommand</span> {
<span class='kw'>return</span> <span class='prelude-val'>Some</span>(<span class='ident'>name</span>);
}
<span class='prelude-val'>None</span>
}
}
</pre>
</section>
@ -397,13 +497,13 @@
<script>
window.rootPath = "../../";
window.rootPath = "../../../";
window.currentCrate = "clap";
window.playgroundUrl = "";
</script>
<script src="../../jquery.js"></script>
<script src="../../main.js"></script>
<script src="../../../jquery.js"></script>
<script src="../../../main.js"></script>
<script async src="../../search-index.js"></script>
<script async src="../../../search-index.js"></script>
</body>
</html>

View file

@ -9,7 +9,7 @@
<title>lib.rs.html -- source</title>
<link rel="stylesheet" type="text/css" href="../../main.css">
<link rel="stylesheet" type="text/css" href="../../../main.css">
@ -176,10 +176,29 @@
<span id="134">134</span>
<span id="135">135</span>
<span id="136">136</span>
<span id="137">137</span>
<span id="138">138</span>
<span id="139">139</span>
<span id="140">140</span>
<span id="141">141</span>
<span id="142">142</span>
<span id="143">143</span>
<span id="144">144</span>
<span id="145">145</span>
<span id="146">146</span>
<span id="147">147</span>
<span id="148">148</span>
<span id="149">149</span>
<span id="150">150</span>
<span id="151">151</span>
<span id="152">152</span>
<span id="153">153</span>
<span id="154">154</span>
<span id="155">155</span>
</pre><pre class='rust '>
<span class='attribute'>#<span class='op'>!</span>[<span class='ident'>crate_type</span><span class='op'>=</span> <span class='string'>&quot;lib&quot;</span>]</span>
<span class='attribute'>#<span class='op'>!</span>[<span class='ident'>feature</span>(<span class='ident'>collections</span>, <span class='ident'>core</span>, <span class='ident'>libc</span>)]</span>
<span class='attribute'>#<span class='op'>!</span>[<span class='ident'>feature</span>(<span class='ident'>collections</span>, <span class='ident'>core</span>, <span class='ident'>libc</span>, <span class='ident'>exit_status</span>)]</span>
<span class='doccomment'>//! A simply library for parsing command line arguments when writing </span>
<span class='doccomment'>//! command line and console applications.</span>
@ -216,6 +235,11 @@
<span class='doccomment'>//! .short(&quot;d&quot;)</span>
<span class='doccomment'>//! .multiple(true)</span>
<span class='doccomment'>//! .help(&quot;Turn debugging information on&quot;))</span>
<span class='doccomment'>//! .subcomamnd(SubCommand::new(&quot;test&quot;)</span>
<span class='doccomment'>//! .about(&quot;Has test sub functionality&quot;)</span>
<span class='doccomment'>//! .arg(Arg::new(&quot;verbose&quot;)</span>
<span class='doccomment'>//! .short(&quot;v&quot;)</span>
<span class='doccomment'>//! .help(&quot;Display verbose information&quot;)))</span>
<span class='doccomment'>//! .get_matches();</span>
<span class='doccomment'>//!</span>
<span class='doccomment'>//! if let Some(o) = matches.value_of(&quot;output&quot;) {</span>
@ -233,6 +257,14 @@
<span class='doccomment'>//! 3 | _ =&gt; println!(&quot;Don&#39;t be crazy&quot;),</span>
<span class='doccomment'>//! }</span>
<span class='doccomment'>//! </span>
<span class='doccomment'>//! if let Some(ref matches) = matches.subcommand_matches(&quot;test&quot;) {</span>
<span class='doccomment'>//! if matches.is_present(&quot;verbose&quot;) {</span>
<span class='doccomment'>//! println!(&quot;Printing verbose test info...&quot;);</span>
<span class='doccomment'>//! } else {</span>
<span class='doccomment'>//! println!(&quot;Not printing regular test info...&quot;);</span>
<span class='doccomment'>//! }</span>
<span class='doccomment'>//! }</span>
<span class='doccomment'>//!</span>
<span class='doccomment'>//! // more porgram logic goes here...</span>
<span class='doccomment'>//! ```</span>
<span class='doccomment'>//!</span>
@ -245,7 +277,7 @@
<span class='doccomment'>//! Does awesome things</span>
<span class='doccomment'>//! </span>
<span class='doccomment'>//! USAGE:</span>
<span class='doccomment'>//! MyApp [FLAGS] [OPTIONS] [POSITIONAL]</span>
<span class='doccomment'>//! MyApp [FLAGS] [OPTIONS] [POSITIONAL] [SUBCOMMANDS]</span>
<span class='doccomment'>//! </span>
<span class='doccomment'>//! FLAGS:</span>
<span class='doccomment'>//! -d Turn debugging information on</span>
@ -257,16 +289,22 @@
<span class='doccomment'>//!</span>
<span class='doccomment'>//! POSITIONAL ARGUMENTS:</span>
<span class='doccomment'>//! output Sets an optional output file</span>
<span class='doccomment'>//!</span>
<span class='doccomment'>//! SUBCOMMANDS:</span>
<span class='doccomment'>//! help Prints this message</span>
<span class='doccomment'>//! test Has test sub-functionality</span>
<span class='doccomment'>//! ```</span>
<span class='kw'>pub</span> <span class='kw'>use</span> <span class='ident'>argmatches</span>::<span class='ident'>ArgMatches</span>;
<span class='kw'>pub</span> <span class='kw'>use</span> <span class='ident'>arg</span>::<span class='ident'>Arg</span>;
<span class='kw'>pub</span> <span class='kw'>use</span> <span class='ident'>app</span>::<span class='ident'>App</span>;
<span class='kw'>pub</span> <span class='kw'>use</span> <span class='ident'>subcommand</span>::<span class='ident'>SubCommand</span>;
<span class='kw'>mod</span> <span class='ident'>app</span>;
<span class='kw'>mod</span> <span class='ident'>argmatches</span>;
<span class='kw'>mod</span> <span class='ident'>arg</span>;
<span class='kw'>mod</span> <span class='ident'>args</span>;
<span class='kw'>mod</span> <span class='ident'>subcommand</span>;
<span class='attribute'>#[<span class='ident'>cfg</span>(<span class='ident'>test</span>)]</span>
<span class='kw'>mod</span> <span class='ident'>tests</span> {
@ -353,13 +391,13 @@
<script>
window.rootPath = "../../";
window.rootPath = "../../../";
window.currentCrate = "clap";
window.playgroundUrl = "";
</script>
<script src="../../jquery.js"></script>
<script src="../../main.js"></script>
<script src="../../../jquery.js"></script>
<script src="../../../main.js"></script>
<script async src="../../search-index.js"></script>
<script async src="../../../search-index.js"></script>
</body>
</html>

View file

@ -0,0 +1,179 @@
<!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="Source to the Rust file `src/subcommand.rs`.">
<meta name="keywords" content="rust, rustlang, rust-lang">
<title>subcommand.rs.html -- source</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">
</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 source"><pre class="line-numbers"><span id="1"> 1</span>
<span id="2"> 2</span>
<span id="3"> 3</span>
<span id="4"> 4</span>
<span id="5"> 5</span>
<span id="6"> 6</span>
<span id="7"> 7</span>
<span id="8"> 8</span>
<span id="9"> 9</span>
<span id="10">10</span>
<span id="11">11</span>
<span id="12">12</span>
<span id="13">13</span>
<span id="14">14</span>
<span id="15">15</span>
<span id="16">16</span>
<span id="17">17</span>
<span id="18">18</span>
<span id="19">19</span>
<span id="20">20</span>
<span id="21">21</span>
<span id="22">22</span>
<span id="23">23</span>
<span id="24">24</span>
<span id="25">25</span>
<span id="26">26</span>
<span id="27">27</span>
<span id="28">28</span>
<span id="29">29</span>
<span id="30">30</span>
<span id="31">31</span>
<span id="32">32</span>
<span id="33">33</span>
<span id="34">34</span>
<span id="35">35</span>
<span id="36">36</span>
<span id="37">37</span>
<span id="38">38</span>
<span id="39">39</span>
<span id="40">40</span>
<span id="41">41</span>
<span id="42">42</span>
<span id="43">43</span>
</pre><pre class='rust '>
<span class='kw'>use</span> <span class='ident'>app</span>::<span class='ident'>App</span>;
<span class='kw'>use</span> <span class='ident'>argmatches</span>::<span class='ident'>ArgMatches</span>;
<span class='doccomment'>/// The abstract representation of a command line subcommand used by the consumer of the library.</span>
<span class='doccomment'>/// </span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// This struct is used by the library consumer and describes all the valid options of the subcommand for </span>
<span class='doccomment'>/// their program. SubCommands are treated like &quot;sub apps&quot; and contain all the same possibilities (such as</span>
<span class='doccomment'>/// their own arguments and subcommands).</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// # Example</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// ```no_run</span>
<span class='doccomment'>/// # use clap::{App, Arg, SubCommand};</span>
<span class='doccomment'>/// # let matches = App::new(&quot;myprog&quot;)</span>
<span class='doccomment'>/// # .SubCommand(</span>
<span class='doccomment'>/// SubCommand::new(&quot;conifg&quot;)</span>
<span class='doccomment'>/// .about(&quot;Used for configuration&quot;)</span>
<span class='doccomment'>/// .arg(Arg::new(&quot;config_file&quot;)</span>
<span class='doccomment'>/// .help(&quot;The configuration file to use&quot;)</span>
<span class='doccomment'>/// .index(1))</span>
<span class='doccomment'>/// # ).get_matches();</span>
<span class='kw'>pub</span> <span class='kw'>struct</span> <span class='ident'>SubCommand</span> {
<span class='kw'>pub</span> <span class='ident'>name</span>: <span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>,
<span class='kw'>pub</span> <span class='ident'>matches</span>: <span class='ident'>ArgMatches</span>
}
<span class='kw'>impl</span> <span class='ident'>SubCommand</span> {
<span class='doccomment'>/// Creates a new instance of a subcommand requiring a name. Will be displayed</span>
<span class='doccomment'>/// to the user when they print version or help and usage information.</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// # Example</span>
<span class='doccomment'>///</span>
<span class='doccomment'>/// ```no_run</span>
<span class='doccomment'>/// # use clap::{App, Arg, SubCommand};</span>
<span class='doccomment'>/// # let prog = App::new(&quot;myprog&quot;).subcommand(</span>
<span class='doccomment'>/// SubCommand::new(&quot;config&quot;)</span>
<span class='doccomment'>/// # ).get_matches();</span>
<span class='doccomment'>/// ```</span>
<span class='kw'>pub</span> <span class='kw'>fn</span> <span class='ident'>new</span>(<span class='ident'>name</span>: <span class='kw-2'>&amp;</span><span class='lifetime'>&#39;static</span> <span class='ident'>str</span>) <span class='op'>-&gt;</span> <span class='ident'>App</span> {
<span class='ident'>App</span>::<span class='ident'>new</span>(<span class='ident'>name</span>)
}
}
</pre>
</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>

53
examples/myapp.rs Normal file
View file

@ -0,0 +1,53 @@
extern crate clap;
use clap::{App, Arg, SubCommand};
fn main() {
let matches = App::new("MyApp")
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.arg(Arg::new("config")
.short("c")
.long("config")
.help("Sets a custom config file")
.takes_value(true))
.arg(Arg::new("output")
.help("Sets an optional output file")
.index(1))
.arg(Arg::new("debug")
.short("d")
.multiple(true)
.help("Turn debugging information on"))
.subcommand(SubCommand::new("test")
.about("does testing things")
.arg(Arg::new("list")
.short("l")
.help("lists test values")))
.get_matches();
if let Some(o) = matches.value_of("output") {
println!("Value for output: {}", o);
}
if let Some(c) = matches.value_of("config") {
println!("Value for config: {}", c);
}
match matches.occurrences_of("debug") {
0 => println!("Debug mode is off"),
1 => println!("Debug mode is kind of on"),
2 => println!("Debug mode is on"),
3 | _ => println!("Don't be crazy"),
}
if let Some(ref matches) = matches.subcommand_matches("test") {
if matches.is_present("list") {
println!("Printing testing lists...");
} else {
println!("Not printing testing lists...");
}
}
print!("App is running...");
println!("done.");
}

40
main.rs Normal file
View file

@ -0,0 +1,40 @@
extern crate clap;
use clap::{App, Arg};
fn main() {
let matches = App::new("MyApp")
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.arg(Arg::new("config")
.short("c")
.long("config")
.help("Sets a custom config file")
.takes_value(true))
.arg(Arg::new("output")
.help("Sets an optional output file")
.index(1))
.arg(Arg::new("debug")
.short("d")
.multiple(true)
.help("Turn debugging information on"))
.get_matches();
if let Some(o) = matches.value_of("output") {
println!("Value for output: {}", o);
}
if let Some(c) = matches.value_of("config") {
println!("Value for config: {}", c);
}
match matches.occurrences_of("debug") {
0 => println!("Debug mode is off"),
1 => println!("Debug mode is kind of on"),
2 => println!("Debug mode is on"),
3 | _ => println!("Don't be crazy"),
}
print!("App is running...");
println!("done.");
}

View file

@ -180,15 +180,6 @@ impl App {
self.required.insert(a.name);
}
if let Some(i) = a.index {
// self.positionals_name.insert(a.name, PosArg {
// name: a.name,
// index: i,
// required: a.required,
// help: a.help,
// blacklist: a.blacklist,
// requires: a.requires,
// value: None
// });
self.positionals_idx.insert(i, PosArg {
name: a.name,
index: i,
@ -621,7 +612,7 @@ impl App {
}
}
fn get_matches_from(&mut self, it: &mut IntoIter<String>, matches: &mut ArgMatches) -> Option<&'static str> {
fn get_matches_from(&mut self, matches: &mut ArgMatches, it: &mut IntoIter<String>) {
self.create_help_and_version();
// let mut needs_val = false;
@ -688,6 +679,9 @@ impl App {
} else {
// Positional or Subcommand
if let Some(sca) = self.subcommands.get(arg_slice) {
if sca.name == "help" {
self.print_help();
}
subcmd_name = Some(sca.name);
break;
}
@ -753,7 +747,15 @@ impl App {
self.validate_blacklist(&matches);
subcmd_name
if let Some(sc_name) = subcmd_name {
if let Some(ref mut sc) = self.subcommands.get_mut(sc_name) {
let mut new_matches = ArgMatches::new(sc_name);
sc.get_matches_from(&mut new_matches, it);
matches.subcommand = Some((sc_name, Box::new(SubCommand{
name: sc_name,
matches: new_matches})));
}
}
}
pub fn get_matches(mut self) -> ArgMatches {
@ -761,23 +763,7 @@ impl App {
let args = env::args().collect::<Vec<_>>();
let mut it = args.into_iter();
let mut subcmd = self.get_matches_from(&mut it, &mut matches);
while let Some(sc) = subcmd {
if let Some(sca) = self.subcommands.get_mut(sc) {
let mut new_matches = SubCommand {
name: sc,
matches: ArgMatches::new(sc)
};
subcmd = sca.get_matches_from(&mut it, &mut new_matches.matches);
matches.subcommand.insert(sc, new_matches);
// prev_matches = prev_matches.unwrap().subcommand.get_mut(sc).unwrap().matches;
} else {
panic!("Found subcommand \"{}\" but wasn't able to find a valid representation of it to match against", sc);
}
matches = &mut matches.subcommand.get_mut(sc).unwrap().matches;
}
self.get_matches_from(&mut matches, &mut args.into_iter());
matches
}

View file

@ -6,7 +6,7 @@
/// and then evaluates the settings the consumer provided and determines the concret
/// argument struct to use when parsing.
///
/// Example:
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg};

View file

@ -1,7 +1,5 @@
use std::collections::HashMap;
// use std::collections::HashSet;
// use app::App;
use args::{ FlagArg, OptArg, PosArg };
use subcommand::SubCommand;
@ -12,6 +10,8 @@ use subcommand::SubCommand;
/// Fields of `ArgMatches` aren't designed to be used directly, only
/// the methods in order to query information.
///
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg};
/// let matches = App::new("MyApp")
@ -46,25 +46,31 @@ use subcommand::SubCommand;
/// } else {
/// println!("Debug mode kind of on");
/// }
/// }
///
/// // You can get the sub-matches of a particular subcommand (in this case "test")
/// // If "test" had it's own "-l" flag you could check for it's presence accordingly
/// if let Some(ref matches) = matches.subcommand_matches("test") {
/// if matches.is_present("list") {
/// println!("Printing testing lists...");
/// } else {
/// println!("Not printing testing lists...");
/// }
/// }
/// }
pub struct ArgMatches {
pub matches_of: &'static str,
// pub author: Option<&'static str>,
// pub about: Option<&'static str>,
// pub version: Option<&'static str>,
// pub required: Vec<&'static str>,
// pub blacklist: HashSet<&'static str>,
pub flags: HashMap<&'static str, FlagArg>,
pub opts: HashMap<&'static str, OptArg>,
pub positionals: HashMap<&'static str, PosArg>,
pub subcommand: HashMap<&'static str, SubCommand>
pub subcommand: Option<(&'static str, Box<SubCommand>)>
}
impl ArgMatches {
/// Creates a new instance of `ArgMatches`. This ins't called directly, but
/// through the `.get_matches()` method of `App`
///
/// Example:
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg};
@ -76,12 +82,7 @@ impl ArgMatches {
flags: HashMap::new(),
opts: HashMap::new(),
positionals: HashMap::new(),
subcommand: HashMap::new()
// required: vec![],
// blacklist: HashSet::new(),
// about: app.about,
// author: app.author,
// version: app.version,
subcommand: None
}
}
@ -89,7 +90,7 @@ impl ArgMatches {
/// an additional value at runtime). If the option wasn't present at runtime
/// it returns `None`
///
/// Example:
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg};
@ -116,7 +117,7 @@ impl ArgMatches {
/// option or positional arguments (use `.value_of()` instead)
///
///
/// Example:
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg};
@ -126,8 +127,10 @@ impl ArgMatches {
/// }
/// ```
pub fn is_present(&self, name: &'static str) -> bool {
if self.subcommand.contains_key(name) ||
self.flags.contains_key(name) ||
if let Some((sc_name, _ )) = self.subcommand {
if sc_name == name { return true; }
}
if self.flags.contains_key(name) ||
self.opts.contains_key(name) ||
self.positionals.contains_key(name) {
return true;
@ -144,7 +147,7 @@ impl ArgMatches {
/// at all.
///
///
/// Example:
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg};
@ -162,15 +165,44 @@ impl ArgMatches {
0
}
/// If a subcommand was found, returns the ArgMatches struct associated with it's matches
///
///
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg, SubCommand};
/// # let matches = App::new("myapp").subcommand(SubCommand::new("test")).get_matches();
/// if let Some(matches) = app_matches.subcommand_matches("test") {
/// // Use matches as normal
/// }
/// ```
pub fn subcommand_matches(&self, name: &'static str) -> Option<&ArgMatches> {
if let Some(ref sc) = self.subcommand.get(name) {
if let Some( ( sc_name, ref sc)) = self.subcommand {
if sc_name != name { return None; }
return Some(&sc.matches);
}
None
}
/// If a subcommand was found, returns the name associated with it
///
///
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg, SubCommand};
/// # let matches = App::new("myapp").subcommand(SubCommand::new("test")).get_matches();
/// match app_matches.subcommand_() {
/// Some("test") => {}, // test was used
/// Some("config") => {}, // config was used
/// _ => {}, // Either no subcommand or one not tested for...
/// }
/// ```
pub fn subcommand_name(&self) -> Option<&'static str> {
if self.subcommand.is_empty() { return None; }
return Some(self.subcommand.keys().collect::<Vec<_>>()[0]);
if let Some((name, _)) = self.subcommand {
return Some(name);
}
None
}
}

View file

@ -3,7 +3,7 @@
/// or `--` (single word, no spaces). `FlagArg` isn't directly used by the end application
/// writer, only internally to the `clap` library.
///
/// Example:
/// # Example
///
/// ```sh
/// $ myprog -a --some

View file

@ -4,7 +4,7 @@
/// value. `OptArg` isn't directly used by the end application
/// writer, only internally to the `clap` library.
///
/// Example:
/// # Example
///
/// ```sh
/// $ myprog -a some --test other --third=file

View file

@ -2,7 +2,7 @@
/// by a `-` or `--`. `PosArg` isn't directly used by the end application
/// writer, only internally to the `clap` library.
///
/// Example:
/// # Example
///
/// ```sh
/// $ myprog some_file

View file

@ -37,6 +37,11 @@
//! .short("d")
//! .multiple(true)
//! .help("Turn debugging information on"))
//! .subcomamnd(SubCommand::new("test")
//! .about("Has test sub functionality")
//! .arg(Arg::new("verbose")
//! .short("v")
//! .help("Display verbose information")))
//! .get_matches();
//!
//! if let Some(o) = matches.value_of("output") {
@ -54,6 +59,14 @@
//! 3 | _ => println!("Don't be crazy"),
//! }
//!
//! if let Some(ref matches) = matches.subcommand_matches("test") {
//! if matches.is_present("verbose") {
//! println!("Printing verbose test info...");
//! } else {
//! println!("Not printing regular test info...");
//! }
//! }
//!
//! // more porgram logic goes here...
//! ```
//!
@ -66,7 +79,7 @@
//! Does awesome things
//!
//! USAGE:
//! MyApp [FLAGS] [OPTIONS] [POSITIONAL]
//! MyApp [FLAGS] [OPTIONS] [POSITIONAL] [SUBCOMMANDS]
//!
//! FLAGS:
//! -d Turn debugging information on
@ -78,11 +91,16 @@
//!
//! POSITIONAL ARGUMENTS:
//! output Sets an optional output file
//!
//! SUBCOMMANDS:
//! help Prints this message
//! test Has test sub-functionality
//! ```
pub use argmatches::ArgMatches;
pub use arg::Arg;
pub use app::App;
pub use subcommand::SubCommand;
mod app;
mod argmatches;

43
src/subcommand.rs Normal file
View file

@ -0,0 +1,43 @@
use app::App;
use argmatches::ArgMatches;
/// The abstract representation of a command line subcommand used by the consumer of the library.
///
///
/// This struct is used by the library consumer and describes all the valid options of the subcommand for
/// their program. SubCommands are treated like "sub apps" and contain all the same possibilities (such as
/// their own arguments and subcommands).
///
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg, SubCommand};
/// # let matches = App::new("myprog")
/// # .SubCommand(
/// SubCommand::new("conifg")
/// .about("Used for configuration")
/// .arg(Arg::new("config_file")
/// .help("The configuration file to use")
/// .index(1))
/// # ).get_matches();
pub struct SubCommand {
pub name: &'static str,
pub matches: ArgMatches
}
impl SubCommand {
/// Creates a new instance of a subcommand requiring a name. Will be displayed
/// to the user when they print version or help and usage information.
///
/// # Example
///
/// ```no_run
/// # use clap::{App, Arg, SubCommand};
/// # let prog = App::new("myprog").subcommand(
/// SubCommand::new("config")
/// # ).get_matches();
/// ```
pub fn new(name: &'static str) -> App {
App::new(name)
}
}