Skip to content

Commit

Permalink
docs(tutorial): Explicitly cover required vs optional
Browse files Browse the repository at this point in the history
Inspired by #5928
  • Loading branch information
epage committed Feb 24, 2025
1 parent 08a9b96 commit c74cfbe
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 19 deletions.
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ path = "examples/tutorial_builder/03_05_default_values.rs"
required-features = ["cargo"]
doc-scrape-examples = true

[[example]]
name = "03_06_required"
path = "examples/tutorial_builder/03_06_required.rs"
required-features = ["cargo"]
doc-scrape-examples = true

[[example]]
name = "04_01_possible"
path = "examples/tutorial_builder/04_01_possible.rs"
Expand Down Expand Up @@ -444,6 +450,12 @@ path = "examples/tutorial_derive/03_05_default_values.rs"
required-features = ["derive"]
doc-scrape-examples = true

[[example]]
name = "03_06_optional_derive"
path = "examples/tutorial_derive/03_06_optional.rs"
required-features = ["derive"]
doc-scrape-examples = true

[[example]]
name = "04_01_enum_derive"
path = "examples/tutorial_derive/04_01_enum.rs"
Expand Down
26 changes: 26 additions & 0 deletions examples/tutorial_builder/03_06_required.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
```console
$ 03_06_required --help
A simple to use, efficient, and full-featured Command Line Argument Parser

Usage: 03_06_required[EXE] <name>

Arguments:
<name>

Options:
-h, --help Print help
-V, --version Print version

$ 03_06_required
? 2
error: the following required arguments were not provided:
<name>

Usage: 03_06_required[EXE] <name>

For more information, try '--help'.

$ 03_06_required bob
name: "bob"

```
14 changes: 14 additions & 0 deletions examples/tutorial_builder/03_06_required.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use clap::{command, Arg};

fn main() {
let matches = command!() // requires `cargo` feature
.arg(Arg::new("name").required(true))
.get_matches();

println!(
"name: {:?}",
matches
.get_one::<String>("name")
.expect("clap `required` ensures its present")
);
}
20 changes: 13 additions & 7 deletions examples/tutorial_derive/03_02_option.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@
$ 03_02_option_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser

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

Options:
-n, --name <NAME>
-h, --help Print help
-V, --version Print version

$ 03_02_option_derive
name: None
? 2
error: the following required arguments were not provided:
--name <NAME>

Usage: 03_02_option_derive[EXE] --name <NAME>

For more information, try '--help'.

$ 03_02_option_derive --name bob
name: Some("bob")
name: "bob"

$ 03_02_option_derive --name=bob
name: Some("bob")
name: "bob"

$ 03_02_option_derive -n bob
name: Some("bob")
name: "bob"

$ 03_02_option_derive -n=bob
name: Some("bob")
name: "bob"

$ 03_02_option_derive -nbob
name: Some("bob")
name: "bob"

```
4 changes: 2 additions & 2 deletions examples/tutorial_derive/03_02_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use clap::Parser;
#[command(version, about, long_about = None)]
struct Cli {
#[arg(short, long)]
name: Option<String>,
name: String,
}

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

println!("name: {:?}", cli.name.as_deref());
println!("name: {:?}", cli.name);
}
14 changes: 10 additions & 4 deletions examples/tutorial_derive/03_03_positional.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
$ 03_03_positional_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser

Usage: 03_03_positional_derive[EXE] [NAME]
Usage: 03_03_positional_derive[EXE] <NAME>

Arguments:
[NAME]
<NAME>

Options:
-h, --help Print help
-V, --version Print version

$ 03_03_positional_derive
name: None
? 2
error: the following required arguments were not provided:
<NAME>

Usage: 03_03_positional_derive[EXE] <NAME>

For more information, try '--help'.

$ 03_03_positional_derive bob
name: Some("bob")
name: "bob"

```
4 changes: 2 additions & 2 deletions examples/tutorial_derive/03_03_positional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
name: Option<String>,
name: String,
}

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

println!("name: {:?}", cli.name.as_deref());
println!("name: {:?}", cli.name);
}
20 changes: 20 additions & 0 deletions examples/tutorial_derive/03_06_optional.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
```console
$ 03_06_optional_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser

Usage: 03_06_optional_derive[EXE] [NAME]

Arguments:
[NAME]

Options:
-h, --help Print help
-V, --version Print version

$ 03_06_optional_derive
name: None

$ 03_06_optional_derive bob
name: Some("bob")

```
13 changes: 13 additions & 0 deletions examples/tutorial_derive/03_06_optional.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use clap::Parser;

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
name: Option<String>,
}

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

println!("name: {:?}", cli.name);
}
14 changes: 12 additions & 2 deletions src/_derive/_tutorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@
//! 1. [Positionals](#positionals)
//! 2. [Options](#options)
//! 3. [Flags](#flags)
//! 4. [Defaults](#defaults)
//! 5. [Subcommands](#subcommands)
//! 4. [Optional](#optional)
//! 5. [Defaults](#defaults)
//! 6. [Subcommands](#subcommands)
//!
//! Arguments are inferred from the fields of your struct.
//!
Expand Down Expand Up @@ -124,6 +125,15 @@
//!
//! This also shows that any[`Arg`][crate::Args] method may be used as an attribute.
//!
//! ### Optional
//!
//! By default, arguments are assumed to be [`required`][crate::Arg::required].
//! To make an argument optional, wrap the field's type in `Option`:
//! ```rust
#![doc = include_str!("../../examples/tutorial_derive/03_06_optional.rs")]
//! ```
#![doc = include_str!("../../examples/tutorial_derive/03_06_optional.md")]
//!
//! ### Defaults
//!
//! We've previously showed that arguments can be [`required`][crate::Arg::required] or optional.
Expand Down
14 changes: 12 additions & 2 deletions src/_tutorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@
//! 1. [Positionals](#positionals)
//! 2. [Options](#options)
//! 3. [Flags](#flags)
//! 4. [Defaults](#defaults)
//! 5. [Subcommands](#subcommands)
//! 4. [Required](#required)
//! 5. [Defaults](#defaults)
//! 6. [Subcommands](#subcommands)
//!
//!
//! ### Positionals
Expand Down Expand Up @@ -116,6 +117,15 @@
//! ```
#![doc = include_str!("../examples/tutorial_builder/03_01_flag_count.md")]
//!
//! ### Required
//!
//! By default, an [`Arg`] is optional which can be changed with
//! [`required`][crate::Arg::required].
//! ```rust
#![doc = include_str!("../examples/tutorial_builder/03_06_required.rs")]
//! ```
#![doc = include_str!("../examples/tutorial_builder/03_06_required.md")]
//!
//! ### Defaults
//!
//! We've previously showed that arguments can be [`required`][crate::Arg::required] or optional.
Expand Down

0 comments on commit c74cfbe

Please sign in to comment.