diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b1463fe..0805553a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,7 +69,7 @@ fn app() -> clap::App<'static> { #[test] fn verify_app() { - app.debug_assert(); + app().debug_assert(); } ``` diff --git a/Cargo.toml b/Cargo.toml index 4b2630f1..26f8ee99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -250,6 +250,12 @@ name = "04_04_custom" path = "examples/tutorial_builder/04_04_custom.rs" required-features = ["cargo"] +[[example]] +name = "05_01_assert" +path = "examples/tutorial_builder/05_01_assert.rs" +required-features = ["cargo"] +test = true + [[example]] name = "01_quick_derive" path = "examples/tutorial_derive/01_quick.rs" @@ -320,6 +326,12 @@ name = "04_04_custom_derive" path = "examples/tutorial_derive/04_04_custom.rs" required-features = ["derive"] +[[example]] +name = "05_01_assert_derive" +path = "examples/tutorial_derive/05_01_assert.rs" +required-features = ["derive"] +test = true + [[example]] name = "custom-bool" path = "examples/derive_ref/custom-bool.rs" diff --git a/examples/tutorial_builder/05_01_assert.rs b/examples/tutorial_builder/05_01_assert.rs new file mode 100644 index 00000000..034367e7 --- /dev/null +++ b/examples/tutorial_builder/05_01_assert.rs @@ -0,0 +1,24 @@ +use clap::{app_from_crate, arg}; + +fn main() { + let matches = app().get_matches(); + + // Note, it's safe to call unwrap() because the arg is required + let port: usize = matches + .value_of_t("PORT") + .expect("'PORT' is required and parsing will fail if its missing"); + println!("PORT = {}", port); +} + +fn app() -> clap::App<'static> { + app_from_crate!().arg( + arg!() + .help("Network port to use") + .validator(|s| s.parse::()), + ) +} + +#[test] +fn verify_app() { + app().debug_assert(); +} diff --git a/examples/tutorial_builder/README.md b/examples/tutorial_builder/README.md index c46dba73..4e1661a1 100644 --- a/examples/tutorial_builder/README.md +++ b/examples/tutorial_builder/README.md @@ -15,7 +15,8 @@ 2. [Validated values](#validated-values) 3. [Argument Relations](#relations-relations) 4. [Custom Validation](#custom-validation) -5. [Contributing](#contributing) +5. [Tips](#tips) +6. [Contributing](#contributing) ## Quick Start @@ -549,6 +550,10 @@ Version: 2.2.3 Doing work using input input.txt and config config.toml ``` +## Tips + +- Proactively check for bad `App` configurations by calling `App::debug_assert` ([example](05_01_assert.rs)) + ## Contributing New example code: diff --git a/examples/tutorial_derive/05_01_assert.rs b/examples/tutorial_derive/05_01_assert.rs new file mode 100644 index 00000000..6640bfdb --- /dev/null +++ b/examples/tutorial_derive/05_01_assert.rs @@ -0,0 +1,21 @@ +use clap::Parser; + +#[derive(Parser)] +#[clap(author, version, about)] +struct Cli { + /// Network port to use + #[clap(parse(try_from_str))] + port: usize, +} + +fn main() { + let cli = Cli::parse(); + + println!("PORT = {}", cli.port); +} + +#[test] +fn verify_app() { + use clap::IntoApp; + Cli::into_app().debug_assert() +} diff --git a/examples/tutorial_derive/README.md b/examples/tutorial_derive/README.md index dd8e8db5..ead1662e 100644 --- a/examples/tutorial_derive/README.md +++ b/examples/tutorial_derive/README.md @@ -15,7 +15,8 @@ 2. [Validated values](#validated-values) 3. [Argument Relations](#relations-relations) 4. [Custom Validation](#custom-validation) -5. [Contributing](#contributing) +5. [Tips](#tips) +6. [Contributing](#contributing) ## Quick Start @@ -517,6 +518,10 @@ Version: 2.2.3 Doing work using input input.txt and config config.toml ``` +## Tips + +- Proactively check for bad `App` configurations by calling `App::debug_assert` ([example](05_01_assert.rs)) + ## Contributing New example code: