mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 14:22:34 +00:00
docs(tutorial): Provide better default_value_t example
This commit is contained in:
parent
acb0fb7809
commit
3cadb8f255
4 changed files with 21 additions and 17 deletions
|
@ -2,19 +2,19 @@
|
||||||
$ 03_05_default_values --help
|
$ 03_05_default_values --help
|
||||||
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: 03_05_default_values[EXE] [NAME]
|
Usage: 03_05_default_values[EXE] [PORT]
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
[NAME] [default: alice]
|
[PORT] [default: 2020]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-h, --help Print help information
|
-h, --help Print help information
|
||||||
-V, --version Print version information
|
-V, --version Print version information
|
||||||
|
|
||||||
$ 03_05_default_values
|
$ 03_05_default_values
|
||||||
name: "alice"
|
port: 2020
|
||||||
|
|
||||||
$ 03_05_default_values bob
|
$ 03_05_default_values 22
|
||||||
name: "bob"
|
port: 22
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
@ -1,14 +1,18 @@
|
||||||
use clap::{arg, command};
|
use clap::{arg, command, value_parser};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let matches = command!() // requires `cargo` feature
|
let matches = command!() // requires `cargo` feature
|
||||||
.arg(arg!([NAME]).default_value("alice"))
|
.arg(
|
||||||
|
arg!([PORT])
|
||||||
|
.value_parser(value_parser!(u16))
|
||||||
|
.default_value("2020"),
|
||||||
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"name: {:?}",
|
"port: {:?}",
|
||||||
matches
|
matches
|
||||||
.get_one::<String>("NAME")
|
.get_one::<u16>("PORT")
|
||||||
.expect("default ensures there is always a value")
|
.expect("default ensures there is always a value")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,19 +2,19 @@
|
||||||
$ 03_05_default_values_derive --help
|
$ 03_05_default_values_derive --help
|
||||||
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: 03_05_default_values_derive[EXE] [NAME]
|
Usage: 03_05_default_values_derive[EXE] [PORT]
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
[NAME] [default: alice]
|
[PORT] [default: 2020]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-h, --help Print help information
|
-h, --help Print help information
|
||||||
-V, --version Print version information
|
-V, --version Print version information
|
||||||
|
|
||||||
$ 03_05_default_values_derive
|
$ 03_05_default_values_derive
|
||||||
name: "alice"
|
port: 2020
|
||||||
|
|
||||||
$ 03_05_default_values_derive bob
|
$ 03_05_default_values_derive 22
|
||||||
name: "bob"
|
port: 22
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
@ -3,12 +3,12 @@ use clap::Parser;
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(author, version, about, long_about = None)]
|
#[command(author, version, about, long_about = None)]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
#[arg(default_value_t = String::from("alice"))]
|
#[arg(default_value_t = 2020)]
|
||||||
name: String,
|
port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
|
|
||||||
println!("name: {:?}", cli.name);
|
println!("port: {:?}", cli.port);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue