603 lines
No EOL
46 KiB
HTML
603 lines
No EOL
46 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<meta name="generator" content="rustdoc">
|
||
<meta name="description" content="API documentation for the Rust `clap` crate.">
|
||
<meta name="keywords" content="rust, rustlang, rust-lang, clap">
|
||
|
||
<title>clap - Rust</title>
|
||
|
||
<link rel="stylesheet" type="text/css" href="../rustdoc.css">
|
||
<link rel="stylesheet" type="text/css" href="../main.css">
|
||
|
||
|
||
|
||
|
||
</head>
|
||
<body class="rustdoc">
|
||
<!--[if lte IE 8]>
|
||
<div class="warning">
|
||
This old browser is unsupported and will most likely display funky
|
||
things.
|
||
</div>
|
||
<![endif]-->
|
||
|
||
|
||
|
||
<nav class="sidebar">
|
||
|
||
<p class='location'></p><script>window.sidebarCurrent = {name: 'clap', ty: 'mod', relpath: '../'};</script>
|
||
</nav>
|
||
|
||
<nav class="sub">
|
||
<form class="search-form js-only">
|
||
<div class="search-container">
|
||
<input class="search-input" name="search"
|
||
autocomplete="off"
|
||
placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
|
||
type="search">
|
||
</div>
|
||
</form>
|
||
</nav>
|
||
|
||
<section id='main' class="content mod">
|
||
<h1 class='fqn'><span class='in-band'>Crate <a class='mod' href=''>clap</a></span><span class='out-of-band'><span id='render-detail'>
|
||
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
|
||
[<span class='inner'>−</span>]
|
||
</a>
|
||
</span><a id='src-0' class='srclink' href='../src/clap/lib.rs.html#6-438' title='goto source code'>[src]</a></span></h1>
|
||
<div class='docblock'><p>A simple to use, efficient, and full featured library for parsing command line arguments and subcommands when writing console, or terminal applications.</p>
|
||
|
||
<h2 id='about' class='section-header'><a href='#about'>About</a></h2>
|
||
<p><code>clap</code> is used to parse <em>and validate</em> the string of command line arguments provided by the user at runtime. You provide the list of valid possibilities, and <code>clap</code> handles the rest. This means you focus on your <em>applications</em> functionality, and less on the parsing and validating of arguments.</p>
|
||
|
||
<p><code>clap</code> also provides the traditional version and help switches (or flags) 'for free' meaning automatically with no configuration. It does this by checking list of valid possibilities you supplied and adding only the ones you haven't already defined. If you are using subcommands, <code>clap</code> will also auto-generate a <code>help</code> subcommand for you in addition to the traditional flags.</p>
|
||
|
||
<p>Once <code>clap</code> parses the user provided string of arguments, it returns the matches along with any applicable values. If the user made an error or typo, <code>clap</code> informs them of the mistake and exits gracefully (or returns a <code>Result</code> type and allows you to perform any clean up prior to exit). Because of this, you can make reasonable assumptions in your code about the validity of the arguments.</p>
|
||
|
||
<h2 id='faq' class='section-header'><a href='#faq'>FAQ</a></h2>
|
||
<p>For a full FAQ and more in depth details, see <a href="https://github.com/kbknapp/clap-rs/wiki/FAQ">the wiki page</a></p>
|
||
|
||
<h3 id='comparisons' class='section-header'><a href='#comparisons'>Comparisons</a></h3>
|
||
<p>First, let me say that these comparisons are highly subjective, and not meant in a critical or harsh manner. All the argument parsing libraries out there (to include <code>clap</code>) have their own strengths and weaknesses. Sometimes it just comes down to personal taste when all other factors are equal. When in doubt, try them all and pick one that you enjoy :) There's plenty of room in the Rust community for multiple implementations!</p>
|
||
|
||
<h4 id='how-does-clap-compare-to-a-hrefhttpsgithubcomrust-lang-nurserygetoptsgetoptsa' class='section-header'><a href='#how-does-clap-compare-to-a-hrefhttpsgithubcomrust-lang-nurserygetoptsgetoptsa'>How does <code>clap</code> compare to <a href="https://github.com/rust-lang-nursery/getopts">getopts</a>?</a></h4>
|
||
<p><code>getopts</code> is a very basic, fairly minimalist argument parsing library. This isn't a bad thing, sometimes you don't need tons of features, you just want to parse some simple arguments, and have some help text generated for you based on valid arguments you specify. The downside to this approach is that you must manually implement most of the common features (such as checking to display help messages, usage strings, etc.). If you want a highly custom argument parser, and don't mind writing the majority of the functionality yourself, <code>getopts</code> is an excellent base.</p>
|
||
|
||
<p><code>getopts</code> also doesn't allocate much, or at all. This gives it a very small performance boost. Although, as you start implementing additional features, that boost quickly disappears.</p>
|
||
|
||
<p>Personally, I find many, many uses of <code>getopts</code> are manually implementing features that <code>clap</code> provides by default. Using <code>clap</code> simplifies your codebase allowing you to focus on your application, and not argument parsing.</p>
|
||
|
||
<h4 id='how-does-clap-compare-to-a-hrefhttpsgithubcomdocoptdocoptrsdocoptrsa' class='section-header'><a href='#how-does-clap-compare-to-a-hrefhttpsgithubcomdocoptdocoptrsdocoptrsa'>How does <code>clap</code> compare to <a href="https://github.com/docopt/docopt.rs">docopt.rs</a>?</a></h4>
|
||
<p>I first want to say I'm a big a fan of BurntSushi's work, the creator of <code>Docopt.rs</code>. I aspire to produce the quality of libraries that this man does! When it comes to comparing these two libraries they are very different. <code>docopt</code> tasks you with writing a help message, and then it parsers that message for you to determine all valid arguments and their use. Some people LOVE this approach, others do not. If you're willing to write a detailed help message, it's nice that you can stick that in your program and have <code>docopt</code> do the rest. On the downside, it's far less flexible.</p>
|
||
|
||
<p><code>docopt</code> is also excellent at translating arguments into Rust types automatically. There is even a syntax extension which will do all this for you, if you're willing to use a nightly compiler (use of a stable compiler requires you to somewhat manually translate from arguments to Rust types). To use BurntSushi's words, <code>docopt</code> is also a sort of black box. You get what you get, and it's hard to tweak implementation or customize the experience for your use case.</p>
|
||
|
||
<p>Because <code>docopt</code> is doing a ton of work to parse your help messages and determine what you were trying to communicate as valid arguments, it's also one of the more heavy weight parsers performance-wise. For most applications this isn't a concern and this isn't to say <code>docopt</code> is slow, in fact from it. This is just something to keep in mind while comparing.</p>
|
||
|
||
<h4 id='all-else-being-equal-what-are-some-reasons-to-use-clap' class='section-header'><a href='#all-else-being-equal-what-are-some-reasons-to-use-clap'>All else being equal, what are some reasons to use <code>clap</code>?</a></h4>
|
||
<p><code>clap</code> is as fast, and as lightweight as possible while still giving all the features you'd expect from a modern argument parser. In fact, for the amount and type of features <code>clap</code> offers it remains about as fast as <code>getopts</code>. If you use <code>clap</code> when just need some simple arguments parsed, you'll find its a walk in the park. <code>clap</code> also makes it possible to represent extremely complex, and advanced requirements, without too much thought. <code>clap</code> aims to be intuitive, easy to use, and fully capable for wide variety use cases and needs.</p>
|
||
|
||
<h2 id='quick-example' class='section-header'><a href='#quick-example'>Quick Example</a></h2>
|
||
<p>The following examples show a quick example of some of the very basic functionality of <code>clap</code>. For more advanced usage, such as requirements, conflicts, groups, multiple values and occurrences see the <a href="http://kbknapp.github.io/clap-rs/clap/index.html">documentation</a>, <a href="examples">examples/</a> directory of this repository or the <a href="https://www.youtube.com/playlist?list=PLza5oFLQGTl0Bc_EU_pBNcX-rhVqDTRxv">video tutorials</a> (which are quite outdated by now).</p>
|
||
|
||
<p><strong>NOTE:</strong> All these examples are functionally the same, but show three different styles in which to use <code>clap</code></p>
|
||
|
||
<p>The following example is show a method that allows more advanced configuration options (not shown in this small example), or even dynamically generating arguments when desired. The downside is it's more verbose.</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='comment'>// (Full example with detailed comments in examples/01b_quick_example.rs)</span>
|
||
<span class='comment'>//</span>
|
||
<span class='comment'>// This example demonstrates clap's full 'builder pattern' style of creating arguments which is</span>
|
||
<span class='comment'>// more verbose, but allows easier editing, and at times more advanced options, or the possibility</span>
|
||
<span class='comment'>// to generate arguments dynamically.</span>
|
||
<span class='kw'>extern</span> <span class='kw'>crate</span> <span class='ident'>clap</span>;
|
||
<span class='kw'>use</span> <span class='ident'>clap</span>::{<span class='ident'>Arg</span>, <span class='ident'>App</span>, <span class='ident'>SubCommand</span>};
|
||
|
||
<span class='kw'>fn</span> <span class='ident'>main</span>() {
|
||
<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'>"My Super Program"</span>)
|
||
.<span class='ident'>version</span>(<span class='string'>"1.0"</span>)
|
||
.<span class='ident'>author</span>(<span class='string'>"Kevin K. <kbknapp@gmail.com>"</span>)
|
||
.<span class='ident'>about</span>(<span class='string'>"Does awesome things"</span>)
|
||
.<span class='ident'>arg</span>(<span class='ident'>Arg</span>::<span class='ident'>with_name</span>(<span class='string'>"config"</span>)
|
||
.<span class='ident'>short</span>(<span class='string'>"c"</span>)
|
||
.<span class='ident'>long</span>(<span class='string'>"config"</span>)
|
||
.<span class='ident'>value_name</span>(<span class='string'>"FILE"</span>)
|
||
.<span class='ident'>help</span>(<span class='string'>"Sets a custom config file"</span>)
|
||
.<span class='ident'>takes_value</span>(<span class='boolval'>true</span>))
|
||
.<span class='ident'>arg</span>(<span class='ident'>Arg</span>::<span class='ident'>with_name</span>(<span class='string'>"INPUT"</span>)
|
||
.<span class='ident'>help</span>(<span class='string'>"Sets the input file to use"</span>)
|
||
.<span class='ident'>required</span>(<span class='boolval'>true</span>)
|
||
.<span class='ident'>index</span>(<span class='number'>1</span>))
|
||
.<span class='ident'>arg</span>(<span class='ident'>Arg</span>::<span class='ident'>with_name</span>(<span class='string'>"v"</span>)
|
||
.<span class='ident'>short</span>(<span class='string'>"v"</span>)
|
||
.<span class='ident'>multiple</span>(<span class='boolval'>true</span>)
|
||
.<span class='ident'>help</span>(<span class='string'>"Sets the level of verbosity"</span>))
|
||
.<span class='ident'>subcommand</span>(<span class='ident'>SubCommand</span>::<span class='ident'>with_name</span>(<span class='string'>"test"</span>)
|
||
.<span class='ident'>about</span>(<span class='string'>"controls testing features"</span>)
|
||
.<span class='ident'>version</span>(<span class='string'>"1.3"</span>)
|
||
.<span class='ident'>author</span>(<span class='string'>"Someone E. <someone_else@other.com>"</span>)
|
||
.<span class='ident'>arg</span>(<span class='ident'>Arg</span>::<span class='ident'>with_name</span>(<span class='string'>"debug"</span>)
|
||
.<span class='ident'>short</span>(<span class='string'>"d"</span>)
|
||
.<span class='ident'>help</span>(<span class='string'>"print debug information verbosely"</span>)))
|
||
.<span class='ident'>get_matches</span>();
|
||
|
||
<span class='comment'>// Gets a value for config if supplied by user, or defaults to "default.conf"</span>
|
||
<span class='kw'>let</span> <span class='ident'>config</span> <span class='op'>=</span> <span class='ident'>matches</span>.<span class='ident'>value_of</span>(<span class='string'>"config"</span>).<span class='ident'>unwrap_or</span>(<span class='string'>"default.conf"</span>);
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Value for config: {}"</span>, <span class='ident'>config</span>);
|
||
|
||
<span class='comment'>// Calling .unwrap() is safe here because "INPUT" is required (if "INPUT" wasn't</span>
|
||
<span class='comment'>// required we could have used an 'if let' to conditionally get the value)</span>
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Using input file: {}"</span>, <span class='ident'>matches</span>.<span class='ident'>value_of</span>(<span class='string'>"INPUT"</span>).<span class='ident'>unwrap</span>());
|
||
|
||
<span class='comment'>// Vary the output based on how many times the user used the "verbose" flag</span>
|
||
<span class='comment'>// (i.e. 'myprog -v -v -v' or 'myprog -vvv' vs 'myprog -v'</span>
|
||
<span class='kw'>match</span> <span class='ident'>matches</span>.<span class='ident'>occurrences_of</span>(<span class='string'>"v"</span>) {
|
||
<span class='number'>0</span> <span class='op'>=></span> <span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"No verbose info"</span>),
|
||
<span class='number'>1</span> <span class='op'>=></span> <span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Some verbose info"</span>),
|
||
<span class='number'>2</span> <span class='op'>=></span> <span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Tons of verbose info"</span>),
|
||
<span class='number'>3</span> <span class='op'>|</span> _ <span class='op'>=></span> <span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Don't be crazy"</span>),
|
||
}
|
||
|
||
<span class='comment'>// You can handle information about subcommands by requesting their matches by name</span>
|
||
<span class='comment'>// (as below), requesting just the name used, or both at the same time</span>
|
||
<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'>matches</span>.<span class='ident'>subcommand_matches</span>(<span class='string'>"test"</span>) {
|
||
<span class='kw'>if</span> <span class='ident'>matches</span>.<span class='ident'>is_present</span>(<span class='string'>"debug"</span>) {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Printing debug info..."</span>);
|
||
} <span class='kw'>else</span> {
|
||
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Printing normally..."</span>);
|
||
}
|
||
}
|
||
|
||
<span class='comment'>// more program logic goes here...</span>
|
||
}</pre>
|
||
|
||
<p>The following example is functionally the same as the one above, but shows a far less verbose method but sacrifices some of the advanced configuration options (not shown in this small example).</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='comment'>// (Full example with detailed comments in examples/01a_quick_example.rs)</span>
|
||
<span class='comment'>//</span>
|
||
<span class='comment'>// This example demonstrates clap's "usage strings" method of creating arguments which is less</span>
|
||
<span class='comment'>// less verbose</span>
|
||
<span class='kw'>extern</span> <span class='kw'>crate</span> <span class='ident'>clap</span>;
|
||
<span class='kw'>use</span> <span class='ident'>clap</span>::{<span class='ident'>Arg</span>, <span class='ident'>App</span>, <span class='ident'>SubCommand</span>};
|
||
|
||
<span class='kw'>fn</span> <span class='ident'>main</span>() {
|
||
<span class='kw'>let</span> <span class='ident'>matches</span> <span class='op'>=</span> <span class='ident'>App</span>::<span class='ident'>new</span>(<span class='string'>"myapp"</span>)
|
||
.<span class='ident'>version</span>(<span class='string'>"1.0"</span>)
|
||
.<span class='ident'>author</span>(<span class='string'>"Kevin K. <kbknapp@gmail.com>"</span>)
|
||
.<span class='ident'>about</span>(<span class='string'>"Does awesome things"</span>)
|
||
.<span class='ident'>args_from_usage</span>(
|
||
<span class='string'>"-c, --config=[FILE] 'Sets a custom config file'
|
||
<INPUT> 'Sets the input file to use'
|
||
-v... 'Sets the level of verbosity'"</span>)
|
||
.<span class='ident'>subcommand</span>(<span class='ident'>SubCommand</span>::<span class='ident'>with_name</span>(<span class='string'>"test"</span>)
|
||
.<span class='ident'>about</span>(<span class='string'>"controls testing features"</span>)
|
||
.<span class='ident'>version</span>(<span class='string'>"1.3"</span>)
|
||
.<span class='ident'>author</span>(<span class='string'>"Someone E. <someone_else@other.com>"</span>)
|
||
.<span class='ident'>arg_from_usage</span>(<span class='string'>"-d, --debug 'Print debug information'"</span>))
|
||
.<span class='ident'>get_matches</span>();
|
||
|
||
<span class='comment'>// Same as previous example...</span>
|
||
}</pre>
|
||
|
||
<p>The following combines the previous two examples by using the less verbose <code>from_usage</code> methods and the performance of the Builder Pattern.</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='comment'>// (Full example with detailed comments in examples/01c_quick_example.rs)</span>
|
||
<span class='comment'>// Must be compiled with `--features unstable`</span>
|
||
<span class='comment'>//</span>
|
||
<span class='comment'>// This example demonstrates clap's "usage strings" method of creating arguments which is less</span>
|
||
<span class='comment'>// less verbose</span>
|
||
<span class='attribute'>#[<span class='ident'>macro_use</span>]</span>
|
||
<span class='kw'>extern</span> <span class='kw'>crate</span> <span class='ident'>clap</span>;
|
||
|
||
<span class='kw'>fn</span> <span class='ident'>main</span>() {
|
||
<span class='kw'>let</span> <span class='ident'>matches</span> <span class='op'>=</span> <span class='macro'>clap_app</span><span class='macro'>!</span>(<span class='ident'>myapp</span> <span class='op'>=></span>
|
||
(<span class='ident'>version</span>: <span class='string'>"1.0"</span>)
|
||
(<span class='ident'>author</span>: <span class='string'>"Kevin K. <kbknapp@gmail.com>"</span>)
|
||
(<span class='ident'>about</span>: <span class='string'>"Does awesome things"</span>)
|
||
(<span class='kw-2'>@</span><span class='ident'>arg</span> <span class='ident'>config</span>: <span class='op'>-</span><span class='ident'>c</span> <span class='op'>-</span><span class='op'>-</span><span class='ident'>config</span> <span class='op'>+</span><span class='ident'>takes_value</span> <span class='string'>"Sets a custom config file"</span>)
|
||
(<span class='kw-2'>@</span><span class='ident'>arg</span> <span class='ident'>INPUT</span>: <span class='op'>+</span><span class='ident'>required</span> <span class='string'>"Sets the input file to use"</span>)
|
||
(<span class='kw-2'>@</span><span class='ident'>arg</span> <span class='ident'>verbose</span>: <span class='op'>-</span><span class='ident'>v</span> ... <span class='string'>"Sets the level of verbosity"</span>)
|
||
(<span class='kw-2'>@</span><span class='ident'>subcommand</span> <span class='ident'>test</span> <span class='op'>=></span>
|
||
(<span class='ident'>about</span>: <span class='string'>"controls testing features"</span>)
|
||
(<span class='ident'>version</span>: <span class='string'>"1.3"</span>)
|
||
(<span class='ident'>author</span>: <span class='string'>"Someone E. <someone_else@other.com>"</span>)
|
||
(<span class='kw-2'>@</span><span class='ident'>arg</span> <span class='ident'>verbose</span>: <span class='op'>-</span><span class='ident'>d</span> <span class='op'>-</span><span class='op'>-</span><span class='ident'>debug</span> <span class='string'>"Print debug information"</span>)
|
||
)
|
||
).<span class='ident'>get_matches</span>();
|
||
|
||
<span class='comment'>// Same as previous examples...</span>
|
||
}</pre>
|
||
|
||
<p>This final method shows how you can use a YAML file to build your CLI and keep your Rust source tidy or support multiple localized translations by having different YAML files for each localization. First, create the <code>cli.yml</code> file to hold your CLI options, but it could be called anything we like (we'll use the same both examples above to keep it functionally equivalent):</p>
|
||
|
||
<pre><code class="language-yaml">name: myapp
|
||
version: 1.0
|
||
author: Kevin K. <kbknapp@gmail.com>
|
||
about: Does awesome things
|
||
args:
|
||
- config:
|
||
short: c
|
||
long: config
|
||
value_name: FILE
|
||
help: Sets a custom config file
|
||
takes_value: true
|
||
- INPUT:
|
||
help: Sets the input file to use
|
||
required: true
|
||
index: 1
|
||
- verbose:
|
||
short: v
|
||
multiple: true
|
||
help: Sets the level of verbosity
|
||
subcommands:
|
||
- test:
|
||
about: controls testing features
|
||
version: 1.3
|
||
author: Someone E. <someone_else@other.com>
|
||
args:
|
||
- debug:
|
||
short: d
|
||
help: print debug information
|
||
</code></pre>
|
||
|
||
<p>Now we create our <code>main.rs</code> file just like we would have with the previous two examples:</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='comment'>// (Full example with detailed comments in examples/17_yaml.rs)</span>
|
||
<span class='comment'>//</span>
|
||
<span class='comment'>// This example demonstrates clap's building from YAML style of creating arguments which is far</span>
|
||
<span class='comment'>// more clean, but takes a very small performance hit compared to the other two methods.</span>
|
||
<span class='attribute'>#[<span class='ident'>macro_use</span>]</span>
|
||
<span class='kw'>extern</span> <span class='kw'>crate</span> <span class='ident'>clap</span>;
|
||
<span class='kw'>use</span> <span class='ident'>clap</span>::<span class='ident'>App</span>;
|
||
|
||
<span class='kw'>fn</span> <span class='ident'>main</span>() {
|
||
<span class='comment'>// The YAML file is found relative to the current file, similar to how modules are found</span>
|
||
<span class='kw'>let</span> <span class='ident'>yaml</span> <span class='op'>=</span> <span class='macro'>load_yaml</span><span class='macro'>!</span>(<span class='string'>"cli.yml"</span>);
|
||
<span class='kw'>let</span> <span class='ident'>matches</span> <span class='op'>=</span> <span class='ident'>App</span>::<span class='ident'>from_yaml</span>(<span class='ident'>yaml</span>).<span class='ident'>get_matches</span>();
|
||
|
||
<span class='comment'>// Same as previous examples...</span>
|
||
}</pre>
|
||
|
||
<p><strong>NOTE</strong>: The YAML and macro builder options require adding a special <code>features</code> flag when compiling <code>clap</code> because they are not compiled by default. Simply change your <code>clap = "2"</code> to <code>clap = {version = "2", features = ["yaml"]}</code> for YAML, or <code>features = ["unstable"]</code> for the macro builder, in your <code>Cargo.toml</code>.</p>
|
||
|
||
<p>If you were to compile any of the above programs and run them with the flag <code>--help</code> or <code>-h</code> (or <code>help</code> subcommand, since we defined <code>test</code> as a subcommand) the following would be output</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
$ <span class='ident'>myprog</span> <span class='op'>-</span><span class='op'>-</span><span class='ident'>help</span>
|
||
<span class='ident'>My</span> <span class='ident'>Super</span> <span class='ident'>Program</span> <span class='number'>1.0</span>
|
||
<span class='ident'>Kevin</span> <span class='ident'>K</span>. <span class='op'><</span><span class='ident'>kbknapp</span><span class='kw-2'>@</span><span class='ident'>gmail</span>.<span class='ident'>com</span><span class='op'>></span>
|
||
<span class='ident'>Does</span> <span class='ident'>awesome</span> <span class='ident'>things</span>
|
||
|
||
<span class='ident'>USAGE</span>:
|
||
<span class='ident'>MyApp</span> [<span class='ident'>FLAGS</span>] [<span class='ident'>OPTIONS</span>] <span class='op'><</span><span class='ident'>INPUT</span><span class='op'>></span> [<span class='ident'>SUBCOMMAND</span>]
|
||
|
||
<span class='ident'>FLAGS</span>:
|
||
<span class='op'>-</span><span class='ident'>h</span>, <span class='op'>-</span><span class='op'>-</span><span class='ident'>help</span> <span class='ident'>Prints</span> <span class='ident'>this</span> <span class='ident'>message</span>
|
||
<span class='op'>-</span><span class='ident'>v</span> <span class='ident'>Sets</span> <span class='ident'>the</span> <span class='ident'>level</span> <span class='ident'>of</span> <span class='ident'>verbosity</span>
|
||
<span class='op'>-</span><span class='ident'>V</span>, <span class='op'>-</span><span class='op'>-</span><span class='ident'>version</span> <span class='ident'>Prints</span> <span class='ident'>version</span> <span class='ident'>information</span>
|
||
|
||
<span class='ident'>OPTIONS</span>:
|
||
<span class='op'>-</span><span class='ident'>c</span>, <span class='op'>-</span><span class='op'>-</span><span class='ident'>config</span> <span class='op'><</span><span class='ident'>FILE</span><span class='op'>></span> <span class='ident'>Sets</span> <span class='ident'>a</span> <span class='ident'>custom</span> <span class='ident'>config</span> <span class='ident'>file</span>
|
||
|
||
<span class='ident'>ARGS</span>:
|
||
<span class='ident'>INPUT</span> <span class='ident'>The</span> <span class='ident'>input</span> <span class='ident'>file</span> <span class='ident'>to</span> <span class='kw'>use</span>
|
||
|
||
<span class='ident'>SUBCOMMANDS</span>:
|
||
<span class='ident'>help</span> <span class='ident'>Prints</span> <span class='ident'>this</span> <span class='ident'>message</span>
|
||
<span class='ident'>test</span> <span class='ident'>Controls</span> <span class='ident'>testing</span> <span class='ident'>features</span></pre>
|
||
|
||
<p><strong>NOTE:</strong> You could also run <code>myapp test --help</code> to see similar output and options for the <code>test</code> subcommand.</p>
|
||
|
||
<h2 id='try-it' class='section-header'><a href='#try-it'>Try it!</a></h2>
|
||
<h3 id='pre-built-test' class='section-header'><a href='#pre-built-test'>Pre-Built Test</a></h3>
|
||
<p>To try out the pre-built example, use the following steps:</p>
|
||
|
||
<ul>
|
||
<li>Clone the repository <code>$ git clone https://github.com/kbknapp/clap-rs && cd clap-rs/clap-tests</code></li>
|
||
<li>Compile the example <code>$ cargo build --release</code></li>
|
||
<li>Run the help info <code>$ ./target/release/claptests --help</code></li>
|
||
<li>Play with the arguments!</li>
|
||
</ul>
|
||
|
||
<h3 id='byob-build-your-own-binary' class='section-header'><a href='#byob-build-your-own-binary'>BYOB (Build Your Own Binary)</a></h3>
|
||
<p>To test out <code>clap</code>'s default auto-generated help/version follow these steps:
|
||
* Create a new cargo project <code>$ cargo new fake --bin && cd fake</code>
|
||
* Add <code>clap</code> to your <code>Cargo.toml</code>
|
||
*
|
||
<code>toml [dependencies] clap = "2"</code></p>
|
||
|
||
<ul>
|
||
<li>Add the following to your <code>src/main.rs</code></li>
|
||
</ul>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='kw'>extern</span> <span class='kw'>crate</span> <span class='ident'>clap</span>;
|
||
<span class='kw'>use</span> <span class='ident'>clap</span>::<span class='ident'>App</span>;
|
||
|
||
<span class='kw'>fn</span> <span class='ident'>main</span>() {
|
||
<span class='ident'>App</span>::<span class='ident'>new</span>(<span class='string'>"fake"</span>).<span class='ident'>version</span>(<span class='string'>"v1.0-beta"</span>).<span class='ident'>get_matches</span>();
|
||
}</pre>
|
||
|
||
<ul>
|
||
<li>Build your program <code>$ cargo build --release</code></li>
|
||
<li>Run with help or version <code>$ ./target/release/fake --help</code> or <code>$ ./target/release/fake --version</code></li>
|
||
</ul>
|
||
|
||
<h2 id='usage' class='section-header'><a href='#usage'>Usage</a></h2>
|
||
<p>For full usage, add <code>clap</code> as a dependency in your <code>Cargo.toml</code> file to use from crates.io:</p>
|
||
|
||
<pre><code class="language-toml"> [dependencies]
|
||
clap = "2"
|
||
</code></pre>
|
||
|
||
<p>Or track the latest on the master branch at github:</p>
|
||
|
||
<pre><code class="language-toml">[dependencies.clap]
|
||
git = "https://github.com/kbknapp/clap-rs.git"
|
||
</code></pre>
|
||
|
||
<p>Add <code>extern crate clap;</code> to your crate root.</p>
|
||
|
||
<p>Define a list of valid arguments for your program (see the <a href="https://kbknapp.github.io/clap-rs/index.html">documentation</a> or <a href="examples">examples/</a> directory of this repo)</p>
|
||
|
||
<p>Then run <code>cargo build</code> or <code>cargo update && cargo build</code> for your project.</p>
|
||
|
||
<h3 id='optional-dependencies--features' class='section-header'><a href='#optional-dependencies--features'>Optional Dependencies / Features</a></h3>
|
||
<p>If you'd like to keep your dependency list to <strong>only</strong> <code>clap</code>, you can disable any features that require an additional dependency. To do this, add this to your <code>Cargo.toml</code>:</p>
|
||
|
||
<pre><code class="language-toml">[dependencies.clap]
|
||
version = "2"
|
||
default-features = false
|
||
</code></pre>
|
||
|
||
<p>You can also selectively enable only the features you'd like to include, by adding:</p>
|
||
|
||
<pre><code class="language-toml">[dependencies.clap]
|
||
version = "2"
|
||
default-features = false
|
||
|
||
# Cherry-pick the features you'd like to use
|
||
features = [ "suggestions", "color" ]
|
||
</code></pre>
|
||
|
||
<p>The following is a list of optional <code>clap</code> features:</p>
|
||
|
||
<ul>
|
||
<li><strong>"suggestions"</strong>: Turns on the <code>Did you mean '--myoption' ?</code> feature for when users make typos.</li>
|
||
<li><strong>"color"</strong>: Turns on colored error messages. This feature only works on non-Windows OSs.</li>
|
||
<li><strong>"lints"</strong>: This is <strong>not</strong> included by default and should only be used while developing to run basic lints against changes. This can only be used on Rust nightly.</li>
|
||
<li><strong>"debug"</strong>: This is <strong>not</strong> included by default and should only be used while developing to display debugging information.</li>
|
||
<li><strong>"yaml"</strong>: This is <strong>not</strong> included by default. Enables building CLIs from YAML documents.</li>
|
||
<li><strong>"unstable"</strong>: This is <strong>not</strong> included by default. Enables unstable features, unstable refers to whether or not they may change, not performance stability.</li>
|
||
</ul>
|
||
|
||
<h3 id='more-information' class='section-header'><a href='#more-information'>More Information</a></h3>
|
||
<p>You can find complete documentation on the <a href="http://kbknapp.github.io/clap-rs/clap/index.html">github-pages site</a> for this project.</p>
|
||
|
||
<p>You can also find usage examples in the <a href="examples">examples/</a> directory of this repo.</p>
|
||
|
||
<h4 id='video-tutorials' class='section-header'><a href='#video-tutorials'>Video Tutorials</a></h4>
|
||
<p>There's also the video tutorial series <a href="https://www.youtube.com/playlist?list=PLza5oFLQGTl0Bc_EU_pBNcX-rhVqDTRxv">Argument Parsing with Rust</a> that I've been working on.</p>
|
||
|
||
<p><em>Note</em>: Two new videos have just been added (<a href="https://youtu.be/xc6VdedFrG0">08 From Usage</a>, and <a href="https://youtu.be/mZn3C1DnD90">09 Typed Values</a>), if you're already familiar with <code>clap</code> but want to know more about these two details you can check out those videos without watching the previous few.</p>
|
||
|
||
<p><em>Note</em>: Apologies for the resolution of the first video, it will be updated to a better resolution soon. The other videos have a proper resolution.</p>
|
||
|
||
<h3 id='running-the-tests' class='section-header'><a href='#running-the-tests'>Running the tests</a></h3>
|
||
<p>If contributing, you can run the tests as follows (assuming you're in the <code>clap-rs</code> directory)</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
$ <span class='ident'>cargo</span> <span class='ident'>test</span> <span class='op'>&&</span> <span class='ident'>make</span> <span class='op'>-</span><span class='ident'>C</span> <span class='ident'>clap</span><span class='op'>-</span><span class='ident'>tests</span> <span class='ident'>test</span>
|
||
$ <span class='ident'>cargo</span> <span class='ident'>test</span> <span class='op'>-</span><span class='op'>-</span><span class='ident'>features</span> <span class='ident'>yaml</span>
|
||
|
||
$ <span class='ident'>cargo</span> <span class='ident'>build</span> <span class='op'>-</span><span class='op'>-</span><span class='ident'>features</span> <span class='ident'>lints</span></pre>
|
||
|
||
<h2 id='license' class='section-header'><a href='#license'>License</a></h2>
|
||
<p><code>clap</code> is licensed under the MIT license. Please read the <a href="LICENSE-MIT">LICENSE-MIT</a> file in this repository for more information.</p>
|
||
</div><h2 id='macros' class='section-header'><a href="#macros">Macros</a></h2>
|
||
<table>
|
||
<tr class=' module-item'>
|
||
<td><a class='macro' href='macro._clap_count_exprs!.html'
|
||
title='clap::_clap_count_exprs!'>_clap_count_exprs!</a></td>
|
||
<td class='docblock short'>
|
||
<p>Counts the number of comma-delimited expressions passed to it. The result is a compile-time
|
||
evaluable expression, suitable for use as a static array size, or the value of a <code>const</code>.</p>
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class='macro' href='macro.arg_enum!.html'
|
||
title='clap::arg_enum!'>arg_enum!</a></td>
|
||
<td class='docblock short'>
|
||
<p>Convenience macro to generate more complete enums with variants to be used as a type when
|
||
parsing arguments. This enum also provides a <code>variants()</code> function which can be used to
|
||
retrieve a <code>Vec<&'static str></code> of the variant names, as well as implementing <code>FromStr</code> and
|
||
<code>Display</code> automatically.</p>
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class='macro' href='macro.crate_version!.html'
|
||
title='clap::crate_version!'>crate_version!</a></td>
|
||
<td class='docblock short'>
|
||
<p>Allows you to pull the version from your Cargo.toml at compile time as
|
||
MAJOR.MINOR.PATCH_PKGVERSION_PRE</p>
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class='macro' href='macro.value_t!.html'
|
||
title='clap::value_t!'>value_t!</a></td>
|
||
<td class='docblock short'>
|
||
<p>Convenience macro getting a typed value <code>T</code> where <code>T</code> implements <code>std::str::FromStr</code> from an
|
||
argument value. This macro returns a <code>Result<T,String></code> which allows you as the developer to
|
||
decide what you'd like to do on a failed parse. There are two types of errors, parse failures
|
||
and those where the argument wasn't present (such as a non-required argument). You can use
|
||
it to get a single value, or a iterator as with the <code>ArgMatches::values_of</code></p>
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class='macro' href='macro.value_t_or_exit!.html'
|
||
title='clap::value_t_or_exit!'>value_t_or_exit!</a></td>
|
||
<td class='docblock short'>
|
||
<p>Convenience macro getting a typed value <code>T</code> where <code>T</code> implements <code>std::str::FromStr</code> or
|
||
exiting upon error instead of returning a <code>Result</code></p>
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class='macro' href='macro.values_t!.html'
|
||
title='clap::values_t!'>values_t!</a></td>
|
||
<td class='docblock short'>
|
||
<p>Convenience macro getting a typed value <code>Vec<T></code> where <code>T</code> implements <code>std::str::FromStr</code> This
|
||
macro returns a <code>clap::Result<Vec<T>></code> (<code>Result<Vec<T>, clap::Error></code>) which allows you as the
|
||
developer to decide what you'd like to do on a failed parse.</p>
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class='macro' href='macro.values_t_or_exit!.html'
|
||
title='clap::values_t_or_exit!'>values_t_or_exit!</a></td>
|
||
<td class='docblock short'>
|
||
<p>Convenience macro getting a typed value <code>Vec<T></code> where <code>T</code> implements <code>std::str::FromStr</code> or
|
||
exiting upon error.</p>
|
||
</td>
|
||
</tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
|
||
<table>
|
||
<tr class=' module-item'>
|
||
<td><a class='struct' href='struct.App.html'
|
||
title='clap::App'>App</a></td>
|
||
<td class='docblock short'>
|
||
<p>Used to create a representation of a command line program and all possible command line
|
||
arguments. Application settings are set using the "builder pattern" with the
|
||
<code>.get_matches()</code> family of methods being the terminal methods that starts the runtime-parsing
|
||
process. These methods then return information about the user supplied arguments (or lack there
|
||
of).</p>
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class='struct' href='struct.Arg.html'
|
||
title='clap::Arg'>Arg</a></td>
|
||
<td class='docblock short'>
|
||
<p>The abstract representation of a command line argument. Used to set all the options and
|
||
relationships that define a valid argument for the program.</p>
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class='struct' href='struct.ArgGroup.html'
|
||
title='clap::ArgGroup'>ArgGroup</a></td>
|
||
<td class='docblock short'>
|
||
<p><code>ArgGroup</code>s are a family of related arguments and way for you to express, "Any of these
|
||
arguments". By placing arguments in a logical group, you can create easier requirement and
|
||
exclusion rules instead of having to list each argument individually, or when you want a rule
|
||
to apply "any but not all" arguments.</p>
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class='struct' href='struct.ArgMatches.html'
|
||
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 by
|
||
the user. New instances of this struct are obtained by using the <code>App::get_matches</code> family of
|
||
methods.</p>
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class='struct' href='struct.Error.html'
|
||
title='clap::Error'>Error</a></td>
|
||
<td class='docblock short'>
|
||
<p>Command Line Argument Parser Error</p>
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><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.</p>
|
||
</td>
|
||
</tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
|
||
<table>
|
||
<tr class=' module-item'>
|
||
<td><a class='enum' href='enum.AppSettings.html'
|
||
title='clap::AppSettings'>AppSettings</a></td>
|
||
<td class='docblock short'>
|
||
<p>Application level settings, which affect how <code>App</code> operates</p>
|
||
</td>
|
||
</tr>
|
||
<tr class=' module-item'>
|
||
<td><a class='enum' href='enum.ErrorKind.html'
|
||
title='clap::ErrorKind'>ErrorKind</a></td>
|
||
<td class='docblock short'>
|
||
<p>Command line argument parser kind of error</p>
|
||
</td>
|
||
</tr></table><h2 id='types' class='section-header'><a href="#types">Type Definitions</a></h2>
|
||
<table>
|
||
<tr class=' module-item'>
|
||
<td><a class='type' href='type.Result.html'
|
||
title='clap::Result'>Result</a></td>
|
||
<td class='docblock short'>
|
||
<p>Short hand for result type</p>
|
||
</td>
|
||
</tr></table></section>
|
||
<section id='search' class="content hidden"></section>
|
||
|
||
<section class="footer"></section>
|
||
|
||
<aside id="help" class="hidden">
|
||
<div>
|
||
<h1 class="hidden">Help</h1>
|
||
|
||
<div class="shortcuts">
|
||
<h2>Keyboard Shortcuts</h2>
|
||
|
||
<dl>
|
||
<dt>?</dt>
|
||
<dd>Show this help dialog</dd>
|
||
<dt>S</dt>
|
||
<dd>Focus the search field</dd>
|
||
<dt>⇤</dt>
|
||
<dd>Move up in search results</dd>
|
||
<dt>⇥</dt>
|
||
<dd>Move down in search results</dd>
|
||
<dt>⏎</dt>
|
||
<dd>Go to active search result</dd>
|
||
</dl>
|
||
</div>
|
||
|
||
<div class="infos">
|
||
<h2>Search Tricks</h2>
|
||
|
||
<p>
|
||
Prefix searches with a type followed by a colon (e.g.
|
||
<code>fn:</code>) to restrict the search to a given type.
|
||
</p>
|
||
|
||
<p>
|
||
Accepted types are: <code>fn</code>, <code>mod</code>,
|
||
<code>struct</code>, <code>enum</code>,
|
||
<code>trait</code>, <code>type</code>, <code>macro</code>,
|
||
and <code>const</code>.
|
||
</p>
|
||
|
||
<p>
|
||
Search functions by type signature (e.g.
|
||
<code>vec -> usize</code> or <code>* -> vec</code>)
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</aside>
|
||
|
||
|
||
|
||
<script>
|
||
window.rootPath = "../";
|
||
window.currentCrate = "clap";
|
||
window.playgroundUrl = "";
|
||
</script>
|
||
<script src="../jquery.js"></script>
|
||
<script src="../main.js"></script>
|
||
|
||
<script defer src="../search-index.js"></script>
|
||
</body>
|
||
</html> |