-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(tutorial): Explicitly cover required vs optional
Inspired by #5928
- Loading branch information
Showing
11 changed files
with
136 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters