Skip to content

Commit ab41d7f

Browse files
committed
docs(YAML): adds examples for using YAML to build a CLI
1 parent 1a3c729 commit ab41d7f

File tree

4 files changed

+153
-2
lines changed

4 files changed

+153
-2
lines changed

examples/17_yaml.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// In order to use YAML to define your CLI you must compile clap with the "yaml" feature becasue
2+
// it's **not** included by default.
3+
//
4+
// In order to do this, ensure your Cargo.toml looks like one of the following:
5+
//
6+
// [dependencies.clap]
7+
// features = ["yaml"]
8+
//
9+
// __OR__
10+
//
11+
// [dependencies]
12+
// clap = { features = ["yaml"] }
13+
14+
15+
// Using yaml requires calling a clap macro `load_yaml!()` so we must use the '#[macro_use]'
16+
// directive
17+
#[macro_use]
18+
extern crate clap;
19+
20+
use clap::App;
21+
22+
fn main() {
23+
// To load a yaml file containing our CLI definition such as the example '17_yaml.yml' we can
24+
// use the convenience macro which loads the file at compile relative to the current file
25+
// similiar to how modules are found.
26+
//
27+
// Then we pass that yaml object to App to build the CLI.
28+
//
29+
// Finally we call get_matches() to start the parsing process. We use the matches just as we
30+
// normally would
31+
let yml = load_yaml!("17_yaml.yml");
32+
let m = App::from_yaml(yml).get_matches();
33+
34+
// Because the example 17_yaml.yml is rather large we'll just look a single arg so you can
35+
// see that it works...
36+
if let Some(mode) = m.value_of("mode") {
37+
match mode {
38+
"fast" => println!("We're really going now!"),
39+
"slow" => println!("Awwww, too slow :(")
40+
_ => unreachable!()
41+
}
42+
} else {
43+
println!("--mode <MODE> wasn't used...");
44+
}
45+
}

examples/17_yaml.yml

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
name: yml_app
2+
version: 1.0
3+
about: an example using a .yml file to build a CLI
4+
author: Kevin K. <[email protected]>
5+
6+
# AppSettings can be defined as a list and are **not** ascii case sensitive
7+
settings:
8+
- ArgRequiredElseHelp
9+
10+
# All Args must be defined in the 'args:' list where the name of the arg, is the
11+
# key to a Hash object
12+
args:
13+
# The name of this argument, is 'opt' which will be used to access the value
14+
# later in your Rust code
15+
- opt:
16+
help: example option argument from yaml
17+
short: o
18+
long: option
19+
multiple: true
20+
takes_value: true
21+
- pos:
22+
help: example positional argument from yaml
23+
index: 1
24+
# A list of possible values can be defined as a list
25+
possible_values:
26+
- fast
27+
- slow
28+
- flag:
29+
help: demo flag argument
30+
short: F
31+
multiple: true
32+
global: true
33+
# Conflicts, mutual overrides, and requirements can all be defined as a
34+
# list, where the key is the name of the other argument
35+
conflicts_with:
36+
- opt
37+
requires:
38+
- pos
39+
- mode:
40+
long: mode
41+
help: shows an option with specific values
42+
# possible_values can also be defined in this list format
43+
possible_values: [ vi, emacs ]
44+
- mvals:
45+
long: mult-vals
46+
help: demos an option which has two named values
47+
# value names can be described in a list, where the help will be shown
48+
# --mult-vals <one> <two>
49+
value_names:
50+
- one
51+
- two
52+
- minvals:
53+
long: min-vals
54+
multiple: true
55+
help: you must supply at least two values to satisfy me
56+
min_values: 2
57+
- maxvals:
58+
long: max-vals
59+
multiple: true
60+
help: you can only supply a max of 3 values for me!
61+
max_values: 3
62+
63+
# All subcommands must be listed in the 'subcommand:' object, where the key to
64+
# the list is the name of the subcommand, and all settings for that command are
65+
# are part of a Hash object
66+
subcommands:
67+
# The nae of this subcommand will be 'subcmd' which can be accessed in your
68+
# Rust code later
69+
- subcmd:
70+
about: demos subcommands from yaml
71+
version: 0.1
72+
author: Kevin K. <[email protected]>
73+
# Subcommand args are exactly like App args
74+
args:
75+
- scopt:
76+
short: B
77+
multiple: true
78+
help: example subcommand option
79+
takes_value: true
80+
- scpos1:
81+
help: example subcommand positional
82+
index: 1
83+
84+
# ArgGroups are supported as well, and must be sepcified in the 'arg_groups:'
85+
# object of this file
86+
arg_groups:
87+
# the name of the ArgGoup is specified here
88+
- min-max-vals:
89+
# All args and groups that are a part of this group are set here
90+
args:
91+
- minvals
92+
- maxvals
93+
# setting conflicts is done the same manner as setting 'args:'
94+
#
95+
# to make this group required, you could set 'required: true' but for
96+
# this example we won't do that.

src/app/app.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,16 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
148148
}
149149
}
150150

151-
/// Creates a new instace of `App` from a .yml (YAML) file.
151+
/// Creates a new instace of `App` from a .yml (YAML) file. The YAML file must be properly
152+
/// formatted or this function will panic!(). A full example of supported YAML objects can be
153+
/// found in `examples/17_yaml.rs` and `examples/17_yaml.yml`.
154+
///
155+
/// In order to use this function you must compile with the `features = ["yaml"]` in your
156+
/// settings for `[dependencies.clap]` table of your `Cargo.toml`
157+
///
158+
/// Note, due to how the YAML objects are built there is a convienience macro for loading the
159+
/// YAML file (relative to the current file, like modules work). That YAML object can then be
160+
/// passed to this function.
152161
///
153162
/// # Example
154163
///

tests/app.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ name: claptests
22
version: 1.0
33
about: tests clap library
44
author: Kevin K. <[email protected]>
5-
settings: ArgRequiredElseHelp
5+
settings:
6+
- ArgRequiredElseHelp
67
args:
78
- opt:
89
short: o

0 commit comments

Comments
 (0)