mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 14:54:15 +00:00
docs(tutorial): Focus on actions, not macros
This commit is contained in:
parent
6aa4f90609
commit
78b2b44b95
7 changed files with 20 additions and 15 deletions
|
@ -1,8 +1,13 @@
|
||||||
use clap::{arg, command, ArgAction};
|
use clap::{command, Arg, ArgAction};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let matches = command!() // requires `cargo` feature
|
let matches = command!() // requires `cargo` feature
|
||||||
.arg(arg!(-v - -verbose).action(ArgAction::Count))
|
.arg(
|
||||||
|
Arg::new("verbose")
|
||||||
|
.short('v')
|
||||||
|
.long("verbose")
|
||||||
|
.action(ArgAction::Count),
|
||||||
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
|
|
|
@ -7,7 +7,7 @@ USAGE:
|
||||||
03_02_option[EXE] [OPTIONS]
|
03_02_option[EXE] [OPTIONS]
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
-n, --name <NAME>
|
-n, --name <name>
|
||||||
-h, --help Print help information
|
-h, --help Print help information
|
||||||
-V, --version Print version information
|
-V, --version Print version information
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use clap::{arg, command};
|
use clap::{command, Arg};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let matches = command!() // requires `cargo` feature
|
let matches = command!() // requires `cargo` feature
|
||||||
.arg(arg!(-n --name <NAME>).required(false))
|
.arg(Arg::new("name").short('n').long("name"))
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
println!("name: {:?}", matches.get_one::<String>("name"));
|
println!("name: {:?}", matches.get_one::<String>("name"));
|
||||||
|
|
|
@ -4,19 +4,19 @@ clap [..]
|
||||||
A simple to use, efficient, and full-featured Command Line Argument Parser
|
A simple to use, efficient, and full-featured Command Line Argument Parser
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
03_03_positional[EXE] [NAME]
|
03_03_positional[EXE] [name]
|
||||||
|
|
||||||
ARGS:
|
ARGS:
|
||||||
<NAME>
|
<name>
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
-h, --help Print help information
|
-h, --help Print help information
|
||||||
-V, --version Print version information
|
-V, --version Print version information
|
||||||
|
|
||||||
$ 03_03_positional
|
$ 03_03_positional
|
||||||
NAME: None
|
name: None
|
||||||
|
|
||||||
$ 03_03_positional bob
|
$ 03_03_positional bob
|
||||||
NAME: Some("bob")
|
name: Some("bob")
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use clap::{arg, command};
|
use clap::{command, Arg};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let matches = command!() // requires `cargo` feature
|
let matches = command!() // requires `cargo` feature
|
||||||
.arg(arg!([NAME]))
|
.arg(Arg::new("name"))
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
println!("NAME: {:?}", matches.get_one::<String>("NAME"));
|
println!("name: {:?}", matches.get_one::<String>("name"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,9 +14,9 @@ OPTIONS:
|
||||||
-V, --version Print version information
|
-V, --version Print version information
|
||||||
|
|
||||||
$ 03_05_default_values
|
$ 03_05_default_values
|
||||||
NAME: "alice"
|
name: "alice"
|
||||||
|
|
||||||
$ 03_05_default_values bob
|
$ 03_05_default_values bob
|
||||||
NAME: "bob"
|
name: "bob"
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
@ -6,7 +6,7 @@ fn main() {
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"NAME: {:?}",
|
"name: {:?}",
|
||||||
matches
|
matches
|
||||||
.get_one::<String>("NAME")
|
.get_one::<String>("NAME")
|
||||||
.expect("default ensures there is always a value")
|
.expect("default ensures there is always a value")
|
||||||
|
|
Loading…
Reference in a new issue