Skip to content

Commit f2cb42a

Browse files
authored
Merge pull request #5929 from epage/required
docs(tutorial): Explicitly cover required vs optional
2 parents acf9abb + c74cfbe commit f2cb42a

File tree

11 files changed

+154
-39
lines changed

11 files changed

+154
-39
lines changed

Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ path = "examples/tutorial_builder/03_05_default_values.rs"
323323
required-features = ["cargo"]
324324
doc-scrape-examples = true
325325

326+
[[example]]
327+
name = "03_06_required"
328+
path = "examples/tutorial_builder/03_06_required.rs"
329+
required-features = ["cargo"]
330+
doc-scrape-examples = true
331+
326332
[[example]]
327333
name = "04_01_possible"
328334
path = "examples/tutorial_builder/04_01_possible.rs"
@@ -444,6 +450,12 @@ path = "examples/tutorial_derive/03_05_default_values.rs"
444450
required-features = ["derive"]
445451
doc-scrape-examples = true
446452

453+
[[example]]
454+
name = "03_06_optional_derive"
455+
path = "examples/tutorial_derive/03_06_optional.rs"
456+
required-features = ["derive"]
457+
doc-scrape-examples = true
458+
447459
[[example]]
448460
name = "04_01_enum_derive"
449461
path = "examples/tutorial_derive/04_01_enum.rs"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
```console
2+
$ 03_06_required --help
3+
A simple to use, efficient, and full-featured Command Line Argument Parser
4+
5+
Usage: 03_06_required[EXE] <name>
6+
7+
Arguments:
8+
<name>
9+
10+
Options:
11+
-h, --help Print help
12+
-V, --version Print version
13+
14+
$ 03_06_required
15+
? 2
16+
error: the following required arguments were not provided:
17+
<name>
18+
19+
Usage: 03_06_required[EXE] <name>
20+
21+
For more information, try '--help'.
22+
23+
$ 03_06_required bob
24+
name: "bob"
25+
26+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use clap::{command, Arg};
2+
3+
fn main() {
4+
let matches = command!() // requires `cargo` feature
5+
.arg(Arg::new("name").required(true))
6+
.get_matches();
7+
8+
println!(
9+
"name: {:?}",
10+
matches
11+
.get_one::<String>("name")
12+
.expect("clap `required` ensures its present")
13+
);
14+
}

examples/tutorial_derive/03_02_option.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,35 @@
22
$ 03_02_option_derive --help
33
A simple to use, efficient, and full-featured Command Line Argument Parser
44

5-
Usage: 03_02_option_derive[EXE] [OPTIONS]
5+
Usage: 03_02_option_derive[EXE] --name <NAME>
66

77
Options:
88
-n, --name <NAME>
99
-h, --help Print help
1010
-V, --version Print version
1111

1212
$ 03_02_option_derive
13-
name: None
13+
? 2
14+
error: the following required arguments were not provided:
15+
--name <NAME>
16+
17+
Usage: 03_02_option_derive[EXE] --name <NAME>
18+
19+
For more information, try '--help'.
1420

1521
$ 03_02_option_derive --name bob
16-
name: Some("bob")
22+
name: "bob"
1723

1824
$ 03_02_option_derive --name=bob
19-
name: Some("bob")
25+
name: "bob"
2026

2127
$ 03_02_option_derive -n bob
22-
name: Some("bob")
28+
name: "bob"
2329

2430
$ 03_02_option_derive -n=bob
25-
name: Some("bob")
31+
name: "bob"
2632

2733
$ 03_02_option_derive -nbob
28-
name: Some("bob")
34+
name: "bob"
2935

3036
```

examples/tutorial_derive/03_02_option.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use clap::Parser;
44
#[command(version, about, long_about = None)]
55
struct Cli {
66
#[arg(short, long)]
7-
name: Option<String>,
7+
name: String,
88
}
99

1010
fn main() {
1111
let cli = Cli::parse();
1212

13-
println!("name: {:?}", cli.name.as_deref());
13+
println!("name: {:?}", cli.name);
1414
}

examples/tutorial_derive/03_03_positional.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22
$ 03_03_positional_derive --help
33
A simple to use, efficient, and full-featured Command Line Argument Parser
44

5-
Usage: 03_03_positional_derive[EXE] [NAME]
5+
Usage: 03_03_positional_derive[EXE] <NAME>
66

77
Arguments:
8-
[NAME]
8+
<NAME>
99

1010
Options:
1111
-h, --help Print help
1212
-V, --version Print version
1313

1414
$ 03_03_positional_derive
15-
name: None
15+
? 2
16+
error: the following required arguments were not provided:
17+
<NAME>
18+
19+
Usage: 03_03_positional_derive[EXE] <NAME>
20+
21+
For more information, try '--help'.
1622

1723
$ 03_03_positional_derive bob
18-
name: Some("bob")
24+
name: "bob"
1925

2026
```

examples/tutorial_derive/03_03_positional.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use clap::Parser;
33
#[derive(Parser)]
44
#[command(version, about, long_about = None)]
55
struct Cli {
6-
name: Option<String>,
6+
name: String,
77
}
88

99
fn main() {
1010
let cli = Cli::parse();
1111

12-
println!("name: {:?}", cli.name.as_deref());
12+
println!("name: {:?}", cli.name);
1313
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
```console
2+
$ 03_06_optional_derive --help
3+
A simple to use, efficient, and full-featured Command Line Argument Parser
4+
5+
Usage: 03_06_optional_derive[EXE] [NAME]
6+
7+
Arguments:
8+
[NAME]
9+
10+
Options:
11+
-h, --help Print help
12+
-V, --version Print version
13+
14+
$ 03_06_optional_derive
15+
name: None
16+
17+
$ 03_06_optional_derive bob
18+
name: Some("bob")
19+
20+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use clap::Parser;
2+
3+
#[derive(Parser)]
4+
#[command(version, about, long_about = None)]
5+
struct Cli {
6+
name: Option<String>,
7+
}
8+
9+
fn main() {
10+
let cli = Cli::parse();
11+
12+
println!("name: {:?}", cli.name);
13+
}

src/_derive/_tutorial.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@
6161
//! 1. [Positionals](#positionals)
6262
//! 2. [Options](#options)
6363
//! 3. [Flags](#flags)
64-
//! 4. [Subcommands](#subcommands)
64+
//! 4. [Optional](#optional)
6565
//! 5. [Defaults](#defaults)
66+
//! 6. [Subcommands](#subcommands)
6667
//!
6768
//! Arguments are inferred from the fields of your struct.
6869
//!
@@ -85,9 +86,8 @@
8586
//! ### Options
8687
//!
8788
//! You can name your arguments with a flag:
89+
//! - Intent of the value is clearer
8890
//! - Order doesn't matter
89-
//! - They can be optional
90-
//! - Intent is clearer
9191
//!
9292
//! To specify the flags for an argument, you can use [`#[arg(short = 'n')]`][Arg::short] and/or
9393
//! [`#[arg(long = "name")]`][Arg::long] attributes on a field. When no value is given (e.g.
@@ -125,6 +125,26 @@
125125
//!
126126
//! This also shows that any[`Arg`][crate::Args] method may be used as an attribute.
127127
//!
128+
//! ### Optional
129+
//!
130+
//! By default, arguments are assumed to be [`required`][crate::Arg::required].
131+
//! To make an argument optional, wrap the field's type in `Option`:
132+
//! ```rust
133+
#![doc = include_str!("../../examples/tutorial_derive/03_06_optional.rs")]
134+
//! ```
135+
#![doc = include_str!("../../examples/tutorial_derive/03_06_optional.md")]
136+
//!
137+
//! ### Defaults
138+
//!
139+
//! We've previously showed that arguments can be [`required`][crate::Arg::required] or optional.
140+
//! When optional, you work with a `Option` and can `unwrap_or`. Alternatively, you can
141+
//! set [`#[arg(default_value_t)]`][super#arg-attributes].
142+
//!
143+
//! ```rust
144+
#![doc = include_str!("../../examples/tutorial_derive/03_05_default_values.rs")]
145+
//! ```
146+
#![doc = include_str!("../../examples/tutorial_derive/03_05_default_values.md")]
147+
//!
128148
//! ### Subcommands
129149
//!
130150
//! Subcommands are derived with `#[derive(Subcommand)]` and be added via
@@ -143,17 +163,6 @@
143163
//!
144164
#![doc = include_str!("../../examples/tutorial_derive/03_04_subcommands.md")]
145165
//!
146-
//! ### Defaults
147-
//!
148-
//! We've previously showed that arguments can be [`required`][crate::Arg::required] or optional.
149-
//! When optional, you work with a `Option` and can `unwrap_or`. Alternatively, you can
150-
//! set [`#[arg(default_value_t)]`][super#arg-attributes].
151-
//!
152-
//! ```rust
153-
#![doc = include_str!("../../examples/tutorial_derive/03_05_default_values.rs")]
154-
//! ```
155-
#![doc = include_str!("../../examples/tutorial_derive/03_05_default_values.md")]
156-
//!
157166
//! ## Validation
158167
//!
159168
//! 1. [Enumerated values](#enumerated-values)

0 commit comments

Comments
 (0)