From 5f6d4a3dadd436899923f27ea6ac62b26198ad91 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 11 Sep 2023 21:02:11 -0500 Subject: [PATCH 1/2] docs(tutorial): Split out into a module --- .../{_tutorial.rs => _tutorial/mod.rs} | 72 +++++++++--------- src/{_tutorial.rs => _tutorial/mod.rs} | 74 +++++++++---------- 2 files changed, 73 insertions(+), 73 deletions(-) rename src/_derive/{_tutorial.rs => _tutorial/mod.rs} (72%) rename src/{_tutorial.rs => _tutorial/mod.rs} (69%) diff --git a/src/_derive/_tutorial.rs b/src/_derive/_tutorial/mod.rs similarity index 72% rename from src/_derive/_tutorial.rs rename to src/_derive/_tutorial/mod.rs index 5120918a980..737f0798ee3 100644 --- a/src/_derive/_tutorial.rs +++ b/src/_derive/_tutorial/mod.rs @@ -40,34 +40,34 @@ //! ``` //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/01_quick.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/01_quick.rs")] //! ``` //! -#![doc = include_str!("../../examples/tutorial_derive/01_quick.md")] +#![doc = include_str!("../../../examples/tutorial_derive/01_quick.md")] //! //! ## Configuring the Parser //! //! You use derive [`Parser`][crate::Parser] to start building a parser. //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/02_apps.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/02_apps.rs")] //! ``` //! -#![doc = include_str!("../../examples/tutorial_derive/02_apps.md")] +#![doc = include_str!("../../../examples/tutorial_derive/02_apps.md")] //! //! You can use [`#[command(author, version, about)]` attribute defaults][super#command-attributes] on the struct to fill these fields in from your `Cargo.toml` file. //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/02_crate.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/02_crate.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/02_crate.md")] +#![doc = include_str!("../../../examples/tutorial_derive/02_crate.md")] //! //! You can use `#[command]` attributes on the struct to change the application level behavior of clap. Any [`Command`][crate::Command] builder function can be used as an attribute, like [`Command::next_line_help`]. //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/02_app_settings.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/02_app_settings.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/02_app_settings.md")] +#![doc = include_str!("../../../examples/tutorial_derive/02_app_settings.md")] //! //! ## Adding Arguments //! @@ -78,16 +78,16 @@ //! You can have users specify values by their position on the command-line: //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/03_03_positional.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/03_03_positional.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/03_03_positional.md")] +#![doc = include_str!("../../../examples/tutorial_derive/03_03_positional.md")] //! //! Note that the [default `ArgAction` is `Set`][super#arg-types]. To //! accept multiple values, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append] via `Vec`: //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/03_03_positional_mult.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/03_03_positional_mult.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/03_03_positional_mult.md")] +#![doc = include_str!("../../../examples/tutorial_derive/03_03_positional_mult.md")] //! //! ### Options //! @@ -101,34 +101,34 @@ //! `#[arg(short)]`), the flag is inferred from the field's name. //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/03_02_option.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/03_02_option.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/03_02_option.md")] +#![doc = include_str!("../../../examples/tutorial_derive/03_02_option.md")] //! //! Note that the [default `ArgAction` is `Set`][super#arg-types]. To //! accept multiple occurrences, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append] via `Vec`: //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/03_02_option_mult.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/03_02_option_mult.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/03_02_option_mult.md")] +#![doc = include_str!("../../../examples/tutorial_derive/03_02_option_mult.md")] //! //! ### Flags //! //! Flags can also be switches that can be on/off: //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/03_01_flag_bool.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_bool.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/03_01_flag_bool.md")] +#![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_bool.md")] //! //! Note that the [default `ArgAction` for a `bool` field is //! `SetTrue`][super#arg-types]. To accept multiple flags, override the [action][Arg::action] with //! [`Count`][crate::ArgAction::Count]: //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/03_01_flag_count.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_count.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/03_01_flag_count.md")] +#![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_count.md")] //! //! This also shows that any[`Arg`][crate::Args] method may be used as an attribute. //! @@ -140,15 +140,15 @@ //! and even its own subcommands. //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/03_04_subcommands.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/03_04_subcommands.rs")] //! ``` //! We used a struct-variant to define the `add` subcommand. //! Alternatively, you can use a struct for your subcommand's arguments: //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/03_04_subcommands_alt.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/03_04_subcommands_alt.rs")] //! ``` //! -#![doc = include_str!("../../examples/tutorial_derive/03_04_subcommands.md")] +#![doc = include_str!("../../../examples/tutorial_derive/03_04_subcommands.md")] //! //! ### Defaults //! @@ -157,9 +157,9 @@ //! set [`#[arg(default_value_t)]`][super#arg-attributes]. //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/03_05_default_values.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/03_05_default_values.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/03_05_default_values.md")] +#![doc = include_str!("../../../examples/tutorial_derive/03_05_default_values.md")] //! //! ## Validation //! @@ -177,25 +177,25 @@ //! of the mistake, and what the possible valid values are //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/04_01_enum.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/04_01_enum.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/04_01_enum.md")] +#![doc = include_str!("../../../examples/tutorial_derive/04_01_enum.md")] //! //! ### Validated values //! //! More generally, you can validate and parse into any data type with [`Arg::value_parser`]. //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/04_02_parse.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/04_02_parse.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/04_02_parse.md")] +#![doc = include_str!("../../../examples/tutorial_derive/04_02_parse.md")] //! //! A [custom parser][TypedValueParser] can be used to improve the error messages or provide additional validation: //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/04_02_validate.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/04_02_validate.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/04_02_validate.md")] +#![doc = include_str!("../../../examples/tutorial_derive/04_02_validate.md")] //! //! See [`Arg::value_parser`][crate::Arg::value_parser] for more details. //! @@ -216,18 +216,18 @@ //! [`ArgGroup::id`][crate::ArgGroup::id] being the struct's name. //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/04_03_relations.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/04_03_relations.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/04_03_relations.md")] +#![doc = include_str!("../../../examples/tutorial_derive/04_03_relations.md")] //! //! ### Custom Validation //! //! As a last resort, you can create custom errors with the basics of clap's formatting. //! //! ```rust -#![doc = include_str!("../../examples/tutorial_derive/04_04_custom.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/04_04_custom.rs")] //! ``` -#![doc = include_str!("../../examples/tutorial_derive/04_04_custom.md")] +#![doc = include_str!("../../../examples/tutorial_derive/04_04_custom.md")] //! //! ## Testing //! @@ -235,7 +235,7 @@ //! subcommand, you should have a test that calls //! [`Command::debug_assert`][crate::Command::debug_assert]: //! ```rust,no_run -#![doc = include_str!("../../examples/tutorial_derive/05_01_assert.rs")] +#![doc = include_str!("../../../examples/tutorial_derive/05_01_assert.rs")] //! ``` //! //! ## Next Steps diff --git a/src/_tutorial.rs b/src/_tutorial/mod.rs similarity index 69% rename from src/_tutorial.rs rename to src/_tutorial/mod.rs index 58e527cbc28..b7a604e4c73 100644 --- a/src/_tutorial.rs +++ b/src/_tutorial/mod.rs @@ -39,36 +39,36 @@ //! ``` //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/01_quick.rs")] +#![doc = include_str!("../../examples/tutorial_builder/01_quick.rs")] //! ``` //! -#![doc = include_str!("../examples/tutorial_builder/01_quick.md")] +#![doc = include_str!("../../examples/tutorial_builder/01_quick.md")] //! //! ## Configuring the Parser //! //! You use [`Command`][crate::Command] to start building a parser. //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/02_apps.rs")] +#![doc = include_str!("../../examples/tutorial_builder/02_apps.rs")] //! ``` //! -#![doc = include_str!("../examples/tutorial_builder/02_apps.md")] +#![doc = include_str!("../../examples/tutorial_builder/02_apps.md")] //! //! You can use [`command!()`][crate::command!] to fill these fields in from your `Cargo.toml` //! file. **This requires the [`cargo` feature flag][crate::_features].** //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/02_crate.rs")] +#![doc = include_str!("../../examples/tutorial_builder/02_crate.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/02_crate.md")] +#![doc = include_str!("../../examples/tutorial_builder/02_crate.md")] //! //! You can use [`Command`][crate::Command] methods to change the application level behavior of //! clap, like [`Command::next_line_help`]. //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/02_app_settings.rs")] +#![doc = include_str!("../../examples/tutorial_builder/02_app_settings.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/02_app_settings.md")] +#![doc = include_str!("../../examples/tutorial_builder/02_app_settings.md")] //! //! ## Adding Arguments //! @@ -77,16 +77,16 @@ //! You can have users specify values by their position on the command-line: //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/03_03_positional.rs")] +#![doc = include_str!("../../examples/tutorial_builder/03_03_positional.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/03_03_positional.md")] +#![doc = include_str!("../../examples/tutorial_builder/03_03_positional.md")] //! //! Note that the default [`ArgAction`][crate::ArgAction] is [`Set`][crate::ArgAction::Set]. To //! accept multiple values, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append]: //! ```rust -#![doc = include_str!("../examples/tutorial_builder/03_03_positional_mult.rs")] +#![doc = include_str!("../../examples/tutorial_builder/03_03_positional_mult.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/03_03_positional_mult.md")] +#![doc = include_str!("../../examples/tutorial_builder/03_03_positional_mult.md")] //! //! ### Options //! @@ -96,32 +96,32 @@ //! - Intent is clearer //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/03_02_option.rs")] +#![doc = include_str!("../../examples/tutorial_builder/03_02_option.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/03_02_option.md")] +#![doc = include_str!("../../examples/tutorial_builder/03_02_option.md")] //! //! Note that the default [`ArgAction`][crate::ArgAction] is [`Set`][crate::ArgAction::Set]. To //! accept multiple occurrences, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append]: //! ```rust -#![doc = include_str!("../examples/tutorial_builder/03_02_option_mult.rs")] +#![doc = include_str!("../../examples/tutorial_builder/03_02_option_mult.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/03_02_option_mult.md")] +#![doc = include_str!("../../examples/tutorial_builder/03_02_option_mult.md")] //! //! ### Flags //! //! Flags can also be switches that can be on/off: //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/03_01_flag_bool.rs")] +#![doc = include_str!("../../examples/tutorial_builder/03_01_flag_bool.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/03_01_flag_bool.md")] +#![doc = include_str!("../../examples/tutorial_builder/03_01_flag_bool.md")] //! //! To accept multiple flags, use [`Count`][crate::ArgAction::Count]: //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/03_01_flag_count.rs")] +#![doc = include_str!("../../examples/tutorial_builder/03_01_flag_count.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/03_01_flag_count.md")] +#![doc = include_str!("../../examples/tutorial_builder/03_01_flag_count.md")] //! //! ### Subcommands //! @@ -130,9 +130,9 @@ //! own version, author(s), Args, and even its own subcommands. //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/03_04_subcommands.rs")] +#![doc = include_str!("../../examples/tutorial_builder/03_04_subcommands.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/03_04_subcommands.md")] +#![doc = include_str!("../../examples/tutorial_builder/03_04_subcommands.md")] //! //! ### Defaults //! @@ -141,9 +141,9 @@ //! [`Arg::default_value`][crate::Arg::default_value]. //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/03_05_default_values.rs")] +#![doc = include_str!("../../examples/tutorial_builder/03_05_default_values.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/03_05_default_values.md")] +#![doc = include_str!("../../examples/tutorial_builder/03_05_default_values.md")] //! //! ## Validation //! @@ -160,34 +160,34 @@ //! of the mistake, and what the possible valid values are //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/04_01_possible.rs")] +#![doc = include_str!("../../examples/tutorial_builder/04_01_possible.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/04_01_possible.md")] +#![doc = include_str!("../../examples/tutorial_builder/04_01_possible.md")] //! //! When enabling the [`derive` feature][crate::_features], you can use //! [`ValueEnum`][crate::ValueEnum] to take care of the boiler plate for you, giving the same //! results. //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/04_01_enum.rs")] +#![doc = include_str!("../../examples/tutorial_builder/04_01_enum.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/04_01_enum.md")] +#![doc = include_str!("../../examples/tutorial_builder/04_01_enum.md")] //! //! ### Validated values //! //! More generally, you can validate and parse into any data type with [`Arg::value_parser`]. //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/04_02_parse.rs")] +#![doc = include_str!("../../examples/tutorial_builder/04_02_parse.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/04_02_parse.md")] +#![doc = include_str!("../../examples/tutorial_builder/04_02_parse.md")] //! //! A [custom parser][TypedValueParser] can be used to improve the error messages or provide additional validation: //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/04_02_validate.rs")] +#![doc = include_str!("../../examples/tutorial_builder/04_02_validate.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/04_02_validate.md")] +#![doc = include_str!("../../examples/tutorial_builder/04_02_validate.md")] //! //! See [`Arg::value_parser`][crate::Arg::value_parser] for more details. //! @@ -205,18 +205,18 @@ //! they conflict with each other. //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/04_03_relations.rs")] +#![doc = include_str!("../../examples/tutorial_builder/04_03_relations.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/04_03_relations.md")] +#![doc = include_str!("../../examples/tutorial_builder/04_03_relations.md")] //! //! ### Custom Validation //! //! As a last resort, you can create custom errors with the basics of clap's formatting. //! //! ```rust -#![doc = include_str!("../examples/tutorial_builder/04_04_custom.rs")] +#![doc = include_str!("../../examples/tutorial_builder/04_04_custom.rs")] //! ``` -#![doc = include_str!("../examples/tutorial_builder/04_04_custom.md")] +#![doc = include_str!("../../examples/tutorial_builder/04_04_custom.md")] //! //! ## Testing //! @@ -224,7 +224,7 @@ //! subcommand, you should have a test that calls //! [`Command::debug_assert`][crate::Command::debug_assert]: //! ```rust,no_run -#![doc = include_str!("../examples/tutorial_builder/05_01_assert.rs")] +#![doc = include_str!("../../examples/tutorial_builder/05_01_assert.rs")] //! ``` //! //! ## Next Steps From 32586c7b6335f437e6f01a089e7664f9c88ddc09 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 12 Sep 2023 09:24:27 -0500 Subject: [PATCH 2/2] docs(tutorial): Split into separate modules per section Unsure how much to read into this but some refer to the tutorial a a big wall of ... and maybe breaking it down will help? --- src/_derive/_tutorial/chapter_0.rs | 25 +++ src/_derive/_tutorial/chapter_1.rs | 29 ++++ src/_derive/_tutorial/chapter_2.rs | 103 +++++++++++ src/_derive/_tutorial/chapter_3.rs | 78 +++++++++ src/_derive/_tutorial/chapter_4.rs | 15 ++ src/_derive/_tutorial/chapter_5.rs | 14 ++ src/_derive/_tutorial/mod.rs | 265 +++-------------------------- src/_tutorial/chapter_0.rs | 24 +++ src/_tutorial/chapter_1.rs | 31 ++++ src/_tutorial/chapter_2.rs | 87 ++++++++++ src/_tutorial/chapter_3.rs | 84 +++++++++ src/_tutorial/chapter_4.rs | 15 ++ src/_tutorial/chapter_5.rs | 12 ++ src/_tutorial/mod.rs | 252 +++------------------------ src/lib.rs | 4 +- 15 files changed, 565 insertions(+), 473 deletions(-) create mode 100644 src/_derive/_tutorial/chapter_0.rs create mode 100644 src/_derive/_tutorial/chapter_1.rs create mode 100644 src/_derive/_tutorial/chapter_2.rs create mode 100644 src/_derive/_tutorial/chapter_3.rs create mode 100644 src/_derive/_tutorial/chapter_4.rs create mode 100644 src/_derive/_tutorial/chapter_5.rs create mode 100644 src/_tutorial/chapter_0.rs create mode 100644 src/_tutorial/chapter_1.rs create mode 100644 src/_tutorial/chapter_2.rs create mode 100644 src/_tutorial/chapter_3.rs create mode 100644 src/_tutorial/chapter_4.rs create mode 100644 src/_tutorial/chapter_5.rs diff --git a/src/_derive/_tutorial/chapter_0.rs b/src/_derive/_tutorial/chapter_0.rs new file mode 100644 index 00000000000..d6806949c81 --- /dev/null +++ b/src/_derive/_tutorial/chapter_0.rs @@ -0,0 +1,25 @@ +//! ## Quick Start +//! +//! You can create an application declaratively with a `struct` and some +//! attributes. +//! +//! First, ensure `clap` is available with the [`derive` feature flag][crate::_features]: +//! ```console +//! $ cargo add clap --features derive +//! ``` +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/01_quick.rs")] +//! ``` +//! +#![doc = include_str!("../../../examples/tutorial_derive/01_quick.md")] +//! +//! See also +//! - [FAQ: When should I use the builder vs derive APIs?][crate::_faq#when-should-i-use-the-builder-vs-derive-apis] +//! - The [cookbook][crate::_cookbook] for more application-focused examples + +#![allow(unused_imports)] +use crate::builder::*; + +pub use super::chapter_1 as next; +pub use crate::_tutorial as table_of_contents; diff --git a/src/_derive/_tutorial/chapter_1.rs b/src/_derive/_tutorial/chapter_1.rs new file mode 100644 index 00000000000..b58ae6e0c2c --- /dev/null +++ b/src/_derive/_tutorial/chapter_1.rs @@ -0,0 +1,29 @@ +//! ## Configuring the Parser +//! +//! You use derive [`Parser`][crate::Parser] to start building a parser. +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/02_apps.rs")] +//! ``` +//! +#![doc = include_str!("../../../examples/tutorial_derive/02_apps.md")] +//! +//! You can use [`#[command(author, version, about)]` attribute defaults][super#command-attributes] on the struct to fill these fields in from your `Cargo.toml` file. +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/02_crate.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/02_crate.md")] +//! +//! You can use `#[command]` attributes on the struct to change the application level behavior of clap. Any [`Command`][crate::Command] builder function can be used as an attribute, like [`Command::next_line_help`]. +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/02_app_settings.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/02_app_settings.md")] +#![allow(unused_imports)] +use crate::builder::*; + +pub use super::chapter_0 as previous; +pub use super::chapter_2 as next; +pub use crate::_tutorial as table_of_contents; diff --git a/src/_derive/_tutorial/chapter_2.rs b/src/_derive/_tutorial/chapter_2.rs new file mode 100644 index 00000000000..95d02f8513b --- /dev/null +++ b/src/_derive/_tutorial/chapter_2.rs @@ -0,0 +1,103 @@ +//! ## Adding Arguments +//! +//! 1. [Positionals](#positionals) +//! 2. [Options](#options) +//! 3. [Flags](#flags) +//! 4. [Subcommands](#subcommands) +//! 5. [Defaults](#defaults) +//! +//! Arguments are inferred from the fields of your struct. +//! +//! ### Positionals +//! +//! You can have users specify values by their position on the command-line: +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/03_03_positional.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/03_03_positional.md")] +//! +//! Note that the [default `ArgAction` is `Set`][super#arg-types]. To +//! accept multiple values, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append] via `Vec`: +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/03_03_positional_mult.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/03_03_positional_mult.md")] +//! +//! ### Options +//! +//! You can name your arguments with a flag: +//! - Order doesn't matter +//! - They can be optional +//! - Intent is clearer +//! +//! To specify the flags for an argument, you can use [`#[arg(short = 'n')]`][Arg::short] and/or +//! [`#[arg(long = "name")]`][Arg::long] attributes on a field. When no value is given (e.g. +//! `#[arg(short)]`), the flag is inferred from the field's name. +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/03_02_option.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/03_02_option.md")] +//! +//! Note that the [default `ArgAction` is `Set`][super#arg-types]. To +//! accept multiple occurrences, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append] via `Vec`: +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/03_02_option_mult.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/03_02_option_mult.md")] +//! +//! ### Flags +//! +//! Flags can also be switches that can be on/off: +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_bool.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_bool.md")] +//! +//! Note that the [default `ArgAction` for a `bool` field is +//! `SetTrue`][super#arg-types]. To accept multiple flags, override the [action][Arg::action] with +//! [`Count`][crate::ArgAction::Count]: +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_count.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_count.md")] +//! +//! This also shows that any[`Arg`][crate::Args] method may be used as an attribute. +//! +//! ### Subcommands +//! +//! Subcommands are derived with `#[derive(Subcommand)]` and be added via +//! [`#[command(subcommand)]` attribute][super#command-attributes] on the field using that type. +//! Each instance of a [Subcommand][crate::Subcommand] can have its own version, author(s), Args, +//! and even its own subcommands. +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/03_04_subcommands.rs")] +//! ``` +//! We used a struct-variant to define the `add` subcommand. +//! Alternatively, you can use a struct for your subcommand's arguments: +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/03_04_subcommands_alt.rs")] +//! ``` +//! +#![doc = include_str!("../../../examples/tutorial_derive/03_04_subcommands.md")] +//! +//! ### Defaults +//! +//! We've previously showed that arguments can be [`required`][crate::Arg::required] or optional. +//! When optional, you work with a `Option` and can `unwrap_or`. Alternatively, you can +//! set [`#[arg(default_value_t)]`][super#arg-attributes]. +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/03_05_default_values.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/03_05_default_values.md")] +#![allow(unused_imports)] +use crate::builder::*; + +pub use super::chapter_1 as previous; +pub use super::chapter_3 as next; +pub use crate::_tutorial as table_of_contents; diff --git a/src/_derive/_tutorial/chapter_3.rs b/src/_derive/_tutorial/chapter_3.rs new file mode 100644 index 00000000000..16004fcdcdd --- /dev/null +++ b/src/_derive/_tutorial/chapter_3.rs @@ -0,0 +1,78 @@ +//! ## Validation +//! +//! 1. [Enumerated values](#enumerated-values) +//! 2. [Validated values](#validated-values) +//! 3. [Argument Relations](#argument-relations) +//! 4. [Custom Validation](#custom-validation) +//! +//! An appropriate default parser/validator will be selected for the field's type. See +//! [`value_parser!`][crate::value_parser!] for more details. +//! +//! ### Enumerated values +//! +//! For example, if you have arguments of specific values you want to test for, you can derive +//! [`ValueEnum`][super#valueenum-attributes] +//! (any [`PossibleValue`] builder function can be used as a `#[value]` attribute on enum variants). +//! +//! This allows you specify the valid values for that argument. If the user does not use one of +//! those specific values, they will receive a graceful exit with error message informing them +//! of the mistake, and what the possible valid values are +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/04_01_enum.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/04_01_enum.md")] +//! +//! ### Validated values +//! +//! More generally, you can validate and parse into any data type with [`Arg::value_parser`]. +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/04_02_parse.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/04_02_parse.md")] +//! +//! A [custom parser][TypedValueParser] can be used to improve the error messages or provide additional validation: +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/04_02_validate.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/04_02_validate.md")] +//! +//! See [`Arg::value_parser`][crate::Arg::value_parser] for more details. +//! +//! ### Argument Relations +//! +//! You can declare dependencies or conflicts between [`Arg`][crate::Arg]s or even +//! [`ArgGroup`][crate::ArgGroup]s. +//! +//! [`ArgGroup`][crate::ArgGroup]s make it easier to declare relations instead of having to list +//! each individually, or when you want a rule to apply "any but not all" arguments. +//! +//! Perhaps the most common use of [`ArgGroup`][crate::ArgGroup]s is to require one and *only* one +//! argument to be present out of a given set. Imagine that you had multiple arguments, and you +//! want one of them to be required, but making all of them required isn't feasible because perhaps +//! they conflict with each other. +//! +//! [`ArgGroup`][crate::ArgGroup]s are automatically created for a `struct` with its +//! [`ArgGroup::id`][crate::ArgGroup::id] being the struct's name. +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/04_03_relations.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/04_03_relations.md")] +//! +//! ### Custom Validation +//! +//! As a last resort, you can create custom errors with the basics of clap's formatting. +//! +//! ```rust +#![doc = include_str!("../../../examples/tutorial_derive/04_04_custom.rs")] +//! ``` +#![doc = include_str!("../../../examples/tutorial_derive/04_04_custom.md")] +#![allow(unused_imports)] +use crate::builder::*; + +pub use super::chapter_2 as previous; +pub use super::chapter_4 as next; +pub use crate::_tutorial as table_of_contents; diff --git a/src/_derive/_tutorial/chapter_4.rs b/src/_derive/_tutorial/chapter_4.rs new file mode 100644 index 00000000000..86c5368e9af --- /dev/null +++ b/src/_derive/_tutorial/chapter_4.rs @@ -0,0 +1,15 @@ +//! ## Testing +//! +//! clap reports most development errors as `debug_assert!`s. Rather than checking every +//! subcommand, you should have a test that calls +//! [`Command::debug_assert`][crate::Command::debug_assert]: +//! ```rust,no_run +#![doc = include_str!("../../../examples/tutorial_derive/05_01_assert.rs")] +//! ``` + +#![allow(unused_imports)] +use crate::builder::*; + +pub use super::chapter_3 as previous; +pub use super::chapter_5 as next; +pub use crate::_tutorial as table_of_contents; diff --git a/src/_derive/_tutorial/chapter_5.rs b/src/_derive/_tutorial/chapter_5.rs new file mode 100644 index 00000000000..c5863928721 --- /dev/null +++ b/src/_derive/_tutorial/chapter_5.rs @@ -0,0 +1,14 @@ +//! ## Next Steps +//! +//! - [Cookbook][crate::_cookbook] for application-focused examples +//! - Explore more features in the [Derive reference][super] +//! - See also [`Command`], [`Arg`], [`ArgGroup`], and [`PossibleValue`] builder functions which +//! can be used as attributes +//! +//! For support, see [Discussions](https://github.com/clap-rs/clap/discussions) + +#![allow(unused_imports)] +use crate::builder::*; + +pub use super::chapter_4 as previous; +pub use crate::_tutorial as table_of_contents; diff --git a/src/_derive/_tutorial/mod.rs b/src/_derive/_tutorial/mod.rs index 737f0798ee3..f48a64e8bda 100644 --- a/src/_derive/_tutorial/mod.rs +++ b/src/_derive/_tutorial/mod.rs @@ -9,247 +9,28 @@ //! # Documentation: Derive Tutorial //! -//! 1. [Quick Start](#quick-start) -//! 2. [Configuring the Parser](#configuring-the-parser) -//! 3. [Adding Arguments](#adding-arguments) -//! 1. [Positionals](#positionals) -//! 2. [Options](#options) -//! 3. [Flags](#flags) -//! 4. [Subcommands](#subcommands) -//! 5. [Defaults](#defaults) -//! 4. Validation -//! 1. [Enumerated values](#enumerated-values) -//! 2. [Validated values](#validated-values) -//! 3. [Argument Relations](#argument-relations) -//! 4. [Custom Validation](#custom-validation) -//! 5. [Testing](#testing) -//! 6. [Next Steps](#next-steps) -//! -//! See also -//! - [FAQ: When should I use the builder vs derive APIs?][crate::_faq#when-should-i-use-the-builder-vs-derive-apis] -//! - The [cookbook][crate::_cookbook] for more application-focused examples -//! -//! ## Quick Start -//! -//! You can create an application declaratively with a `struct` and some -//! attributes. -//! -//! First, ensure `clap` is available with the [`derive` feature flag][crate::_features]: -//! ```console -//! $ cargo add clap --features derive -//! ``` -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/01_quick.rs")] -//! ``` -//! -#![doc = include_str!("../../../examples/tutorial_derive/01_quick.md")] -//! -//! ## Configuring the Parser -//! -//! You use derive [`Parser`][crate::Parser] to start building a parser. -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/02_apps.rs")] -//! ``` -//! -#![doc = include_str!("../../../examples/tutorial_derive/02_apps.md")] -//! -//! You can use [`#[command(author, version, about)]` attribute defaults][super#command-attributes] on the struct to fill these fields in from your `Cargo.toml` file. -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/02_crate.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/02_crate.md")] -//! -//! You can use `#[command]` attributes on the struct to change the application level behavior of clap. Any [`Command`][crate::Command] builder function can be used as an attribute, like [`Command::next_line_help`]. -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/02_app_settings.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/02_app_settings.md")] -//! -//! ## Adding Arguments -//! -//! Arguments are inferred from the fields of your struct. -//! -//! ### Positionals -//! -//! You can have users specify values by their position on the command-line: -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/03_03_positional.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/03_03_positional.md")] -//! -//! Note that the [default `ArgAction` is `Set`][super#arg-types]. To -//! accept multiple values, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append] via `Vec`: -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/03_03_positional_mult.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/03_03_positional_mult.md")] -//! -//! ### Options -//! -//! You can name your arguments with a flag: -//! - Order doesn't matter -//! - They can be optional -//! - Intent is clearer -//! -//! To specify the flags for an argument, you can use [`#[arg(short = 'n')]`][Arg::short] and/or -//! [`#[arg(long = "name")]`][Arg::long] attributes on a field. When no value is given (e.g. -//! `#[arg(short)]`), the flag is inferred from the field's name. -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/03_02_option.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/03_02_option.md")] -//! -//! Note that the [default `ArgAction` is `Set`][super#arg-types]. To -//! accept multiple occurrences, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append] via `Vec`: -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/03_02_option_mult.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/03_02_option_mult.md")] -//! -//! ### Flags -//! -//! Flags can also be switches that can be on/off: -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_bool.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_bool.md")] -//! -//! Note that the [default `ArgAction` for a `bool` field is -//! `SetTrue`][super#arg-types]. To accept multiple flags, override the [action][Arg::action] with -//! [`Count`][crate::ArgAction::Count]: -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_count.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_count.md")] -//! -//! This also shows that any[`Arg`][crate::Args] method may be used as an attribute. -//! -//! ### Subcommands -//! -//! Subcommands are derived with `#[derive(Subcommand)]` and be added via -//! [`#[command(subcommand)]` attribute][super#command-attributes] on the field using that type. -//! Each instance of a [Subcommand][crate::Subcommand] can have its own version, author(s), Args, -//! and even its own subcommands. -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/03_04_subcommands.rs")] -//! ``` -//! We used a struct-variant to define the `add` subcommand. -//! Alternatively, you can use a struct for your subcommand's arguments: -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/03_04_subcommands_alt.rs")] -//! ``` -//! -#![doc = include_str!("../../../examples/tutorial_derive/03_04_subcommands.md")] -//! -//! ### Defaults -//! -//! We've previously showed that arguments can be [`required`][crate::Arg::required] or optional. -//! When optional, you work with a `Option` and can `unwrap_or`. Alternatively, you can -//! set [`#[arg(default_value_t)]`][super#arg-attributes]. -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/03_05_default_values.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/03_05_default_values.md")] -//! -//! ## Validation -//! -//! An appropriate default parser/validator will be selected for the field's type. See -//! [`value_parser!`][crate::value_parser!] for more details. -//! -//! ### Enumerated values -//! -//! For example, if you have arguments of specific values you want to test for, you can derive -//! [`ValueEnum`][super#valueenum-attributes] -//! (any [`PossibleValue`] builder function can be used as a `#[value]` attribute on enum variants). -//! -//! This allows you specify the valid values for that argument. If the user does not use one of -//! those specific values, they will receive a graceful exit with error message informing them -//! of the mistake, and what the possible valid values are -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/04_01_enum.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/04_01_enum.md")] -//! -//! ### Validated values -//! -//! More generally, you can validate and parse into any data type with [`Arg::value_parser`]. -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/04_02_parse.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/04_02_parse.md")] -//! -//! A [custom parser][TypedValueParser] can be used to improve the error messages or provide additional validation: -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/04_02_validate.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/04_02_validate.md")] -//! -//! See [`Arg::value_parser`][crate::Arg::value_parser] for more details. -//! -//! ### Argument Relations -//! -//! You can declare dependencies or conflicts between [`Arg`][crate::Arg]s or even -//! [`ArgGroup`][crate::ArgGroup]s. -//! -//! [`ArgGroup`][crate::ArgGroup]s make it easier to declare relations instead of having to list -//! each individually, or when you want a rule to apply "any but not all" arguments. -//! -//! Perhaps the most common use of [`ArgGroup`][crate::ArgGroup]s is to require one and *only* one -//! argument to be present out of a given set. Imagine that you had multiple arguments, and you -//! want one of them to be required, but making all of them required isn't feasible because perhaps -//! they conflict with each other. -//! -//! [`ArgGroup`][crate::ArgGroup]s are automatically created for a `struct` with its -//! [`ArgGroup::id`][crate::ArgGroup::id] being the struct's name. -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/04_03_relations.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/04_03_relations.md")] -//! -//! ### Custom Validation -//! -//! As a last resort, you can create custom errors with the basics of clap's formatting. -//! -//! ```rust -#![doc = include_str!("../../../examples/tutorial_derive/04_04_custom.rs")] -//! ``` -#![doc = include_str!("../../../examples/tutorial_derive/04_04_custom.md")] -//! -//! ## Testing -//! -//! clap reports most development errors as `debug_assert!`s. Rather than checking every -//! subcommand, you should have a test that calls -//! [`Command::debug_assert`][crate::Command::debug_assert]: -//! ```rust,no_run -#![doc = include_str!("../../../examples/tutorial_derive/05_01_assert.rs")] -//! ``` -//! -//! ## Next Steps -//! -//! - [Cookbook][crate::_cookbook] for application-focused examples -//! - Explore more features in the [Derive reference][super] -//! - See also [`Command`], [`Arg`], [`ArgGroup`], and [`PossibleValue`] builder functions which -//! can be used as attributes -//! -//! For support, see [Discussions](https://github.com/clap-rs/clap/discussions) +//! 1. [Quick Start][chapter_0] +//! 2. [Configuring the Parser][chapter_1] +//! 3. [Adding Arguments][chapter_2] +//! 1. [Positionals][chapter_2#positionals] +//! 2. [Options][chapter_2#options] +//! 3. [Flags][chapter_2#flags] +//! 4. [Subcommands][chapter_2#subcommands] +//! 5. [Defaults][chapter_2#defaults] +//! 4. [Validation][chapter_3] +//! 1. [Enumerated values][chapter_3#enumerated-values] +//! 2. [Validated values][chapter_3#validated-values] +//! 3. [Argument Relations][chapter_3#argument-relations] +//! 4. [Custom Validation][chapter_3#custom-validation] +//! 5. [Testing][chapter_4] +//! 6. [Next Steps][chapter_5] #![allow(unused_imports)] -use crate::builder::Arg; -use crate::builder::ArgGroup; -use crate::builder::Command; -use crate::builder::PossibleValue; -use crate::builder::TypedValueParser; +use crate::builder::*; + +pub mod chapter_0; +pub mod chapter_1; +pub mod chapter_2; +pub mod chapter_3; +pub mod chapter_4; +pub mod chapter_5; diff --git a/src/_tutorial/chapter_0.rs b/src/_tutorial/chapter_0.rs new file mode 100644 index 00000000000..6999f08b796 --- /dev/null +++ b/src/_tutorial/chapter_0.rs @@ -0,0 +1,24 @@ +//! ## Quick Start +//! +//! You can create an application with several arguments using usage strings. +//! +//! First, ensure `clap` is available: +//! ```console +//! $ cargo add clap +//! ``` +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/01_quick.rs")] +//! ``` +//! +#![doc = include_str!("../../examples/tutorial_builder/01_quick.md")] +//! +//! See also +//! - [FAQ: When should I use the builder vs derive APIs?][crate::_faq#when-should-i-use-the-builder-vs-derive-apis] +//! - The [cookbook][crate::_cookbook] for more application-focused examples + +#![allow(unused_imports)] +use crate::builder::*; + +pub use super::chapter_1 as next; +pub use crate::_tutorial as table_of_contents; diff --git a/src/_tutorial/chapter_1.rs b/src/_tutorial/chapter_1.rs new file mode 100644 index 00000000000..fbafaf722c3 --- /dev/null +++ b/src/_tutorial/chapter_1.rs @@ -0,0 +1,31 @@ +//! ## Configuring the Parser +//! +//! You use [`Command`][crate::Command] to start building a parser. +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/02_apps.rs")] +//! ``` +//! +#![doc = include_str!("../../examples/tutorial_builder/02_apps.md")] +//! +//! You can use [`command!()`][crate::command!] to fill these fields in from your `Cargo.toml` +//! file. **This requires the [`cargo` feature flag][crate::_features].** +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/02_crate.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/02_crate.md")] +//! +//! You can use [`Command`][crate::Command] methods to change the application level behavior of +//! clap, like [`Command::next_line_help`]. +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/02_app_settings.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/02_app_settings.md")] +#![allow(unused_imports)] +use crate::builder::*; + +pub use super::chapter_0 as previous; +pub use super::chapter_2 as next; +pub use crate::_tutorial as table_of_contents; diff --git a/src/_tutorial/chapter_2.rs b/src/_tutorial/chapter_2.rs new file mode 100644 index 00000000000..607b4e4e68c --- /dev/null +++ b/src/_tutorial/chapter_2.rs @@ -0,0 +1,87 @@ +//! ## Adding Arguments +//! +//! 1. [Positionals](#positionals) +//! 2. [Options](#options) +//! 3. [Flags](#flags) +//! 4. [Subcommands](#subcommands) +//! 5. [Defaults](#defaults) +//! +//! +//! ### Positionals +//! +//! You can have users specify values by their position on the command-line: +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/03_03_positional.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/03_03_positional.md")] +//! +//! Note that the default [`ArgAction`][crate::ArgAction] is [`Set`][crate::ArgAction::Set]. To +//! accept multiple values, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append]: +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/03_03_positional_mult.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/03_03_positional_mult.md")] +//! +//! ### Options +//! +//! You can name your arguments with a flag: +//! - Order doesn't matter +//! - They can be optional +//! - Intent is clearer +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/03_02_option.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/03_02_option.md")] +//! +//! Note that the default [`ArgAction`][crate::ArgAction] is [`Set`][crate::ArgAction::Set]. To +//! accept multiple occurrences, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append]: +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/03_02_option_mult.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/03_02_option_mult.md")] +//! +//! ### Flags +//! +//! Flags can also be switches that can be on/off: +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/03_01_flag_bool.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/03_01_flag_bool.md")] +//! +//! To accept multiple flags, use [`Count`][crate::ArgAction::Count]: +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/03_01_flag_count.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/03_01_flag_count.md")] +//! +//! ### Subcommands +//! +//! Subcommands are defined as [`Command`][crate::Command]s that get added via +//! [`Command::subcommand`][crate::Command::subcommand]. Each instance of a Subcommand can have its +//! own version, author(s), Args, and even its own subcommands. +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/03_04_subcommands.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/03_04_subcommands.md")] +//! +//! ### Defaults +//! +//! We've previously showed that arguments can be [`required`][crate::Arg::required] or optional. +//! When optional, you work with a `Option` and can `unwrap_or`. Alternatively, you can set +//! [`Arg::default_value`][crate::Arg::default_value]. +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/03_05_default_values.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/03_05_default_values.md")] +#![allow(unused_imports)] +use crate::builder::*; + +pub use super::chapter_1 as previous; +pub use super::chapter_3 as next; +pub use crate::_tutorial as table_of_contents; diff --git a/src/_tutorial/chapter_3.rs b/src/_tutorial/chapter_3.rs new file mode 100644 index 00000000000..6dc7db2db77 --- /dev/null +++ b/src/_tutorial/chapter_3.rs @@ -0,0 +1,84 @@ +//! ## Validation +//! +//! 1. [Enumerated values](#enumerated-values) +//! 2. [Validated values](#validated-values) +//! 3. [Argument Relations](#argument-relations) +//! 4. [Custom Validation](#custom-validation) +//! +//! An appropriate default parser/validator will be selected for the field's type. See +//! [`value_parser!`][crate::value_parser!] for more details. +//! +//! ### Enumerated values +//! +//! If you have arguments of specific values you want to test for, you can use the +//! [`PossibleValuesParser`][crate::builder::PossibleValuesParser] or [`Arg::value_parser(["val1", +//! ...])`][crate::Arg::value_parser] for short. +//! +//! This allows you specify the valid values for that argument. If the user does not use one of +//! those specific values, they will receive a graceful exit with error message informing them +//! of the mistake, and what the possible valid values are +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/04_01_possible.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/04_01_possible.md")] +//! +//! When enabling the [`derive` feature][crate::_features], you can use +//! [`ValueEnum`][crate::ValueEnum] to take care of the boiler plate for you, giving the same +//! results. +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/04_01_enum.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/04_01_enum.md")] +//! +//! ### Validated values +//! +//! More generally, you can validate and parse into any data type with [`Arg::value_parser`]. +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/04_02_parse.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/04_02_parse.md")] +//! +//! A [custom parser][TypedValueParser] can be used to improve the error messages or provide additional validation: +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/04_02_validate.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/04_02_validate.md")] +//! +//! See [`Arg::value_parser`][crate::Arg::value_parser] for more details. +//! +//! ### Argument Relations +//! +//! You can declare dependencies or conflicts between [`Arg`][crate::Arg]s or even +//! [`ArgGroup`][crate::ArgGroup]s. +//! +//! [`ArgGroup`][crate::ArgGroup]s make it easier to declare relations instead of having to list +//! each individually, or when you want a rule to apply "any but not all" arguments. +//! +//! Perhaps the most common use of [`ArgGroup`][crate::ArgGroup]s is to require one and *only* one +//! argument to be present out of a given set. Imagine that you had multiple arguments, and you +//! want one of them to be required, but making all of them required isn't feasible because perhaps +//! they conflict with each other. +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/04_03_relations.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/04_03_relations.md")] +//! +//! ### Custom Validation +//! +//! As a last resort, you can create custom errors with the basics of clap's formatting. +//! +//! ```rust +#![doc = include_str!("../../examples/tutorial_builder/04_04_custom.rs")] +//! ``` +#![doc = include_str!("../../examples/tutorial_builder/04_04_custom.md")] +#![allow(unused_imports)] +use crate::builder::*; + +pub use super::chapter_2 as previous; +pub use super::chapter_4 as next; +pub use crate::_tutorial as table_of_contents; diff --git a/src/_tutorial/chapter_4.rs b/src/_tutorial/chapter_4.rs new file mode 100644 index 00000000000..d778fb385ea --- /dev/null +++ b/src/_tutorial/chapter_4.rs @@ -0,0 +1,15 @@ +//! ## Testing +//! +//! clap reports most development errors as `debug_assert!`s. Rather than checking every +//! subcommand, you should have a test that calls +//! [`Command::debug_assert`][crate::Command::debug_assert]: +//! ```rust,no_run +#![doc = include_str!("../../examples/tutorial_builder/05_01_assert.rs")] +//! ``` + +#![allow(unused_imports)] +use crate::builder::*; + +pub use super::chapter_3 as previous; +pub use super::chapter_5 as next; +pub use crate::_tutorial as table_of_contents; diff --git a/src/_tutorial/chapter_5.rs b/src/_tutorial/chapter_5.rs new file mode 100644 index 00000000000..a34a91442d7 --- /dev/null +++ b/src/_tutorial/chapter_5.rs @@ -0,0 +1,12 @@ +//! ## Next Steps +//! +//! - [Cookbook][crate::_cookbook] for application-focused examples +//! - Explore more features in the [API reference][super] +//! +//! For support, see [Discussions](https://github.com/clap-rs/clap/discussions) + +#![allow(unused_imports)] +use crate::builder::*; + +pub use super::chapter_4 as previous; +pub use crate::_tutorial as table_of_contents; diff --git a/src/_tutorial/mod.rs b/src/_tutorial/mod.rs index b7a604e4c73..16832302db0 100644 --- a/src/_tutorial/mod.rs +++ b/src/_tutorial/mod.rs @@ -9,234 +9,28 @@ //! # Documentation: Builder Tutorial //! -//! 1. [Quick Start](#quick-start) -//! 2. [Configuring the Parser](#configuring-the-parser) -//! 3. [Adding Arguments](#adding-arguments) -//! 1. [Positionals](#positionals) -//! 2. [Options](#options) -//! 3. [Flags](#flags) -//! 4. [Subcommands](#subcommands) -//! 5. [Defaults](#defaults) -//! 4. Validation -//! 1. [Enumerated values](#enumerated-values) -//! 2. [Validated values](#validated-values) -//! 3. [Argument Relations](#argument-relations) -//! 4. [Custom Validation](#custom-validation) -//! 5. [Testing](#testing) -//! 6. [Next Steps](#next-steps) -//! -//! See also -//! - [FAQ: When should I use the builder vs derive APIs?][crate::_faq#when-should-i-use-the-builder-vs-derive-apis] -//! - The [cookbook][crate::_cookbook] for more application-focused examples -//! -//! ## Quick Start -//! -//! You can create an application with several arguments using usage strings. -//! -//! First, ensure `clap` is available: -//! ```console -//! $ cargo add clap -//! ``` -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/01_quick.rs")] -//! ``` -//! -#![doc = include_str!("../../examples/tutorial_builder/01_quick.md")] -//! -//! ## Configuring the Parser -//! -//! You use [`Command`][crate::Command] to start building a parser. -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/02_apps.rs")] -//! ``` -//! -#![doc = include_str!("../../examples/tutorial_builder/02_apps.md")] -//! -//! You can use [`command!()`][crate::command!] to fill these fields in from your `Cargo.toml` -//! file. **This requires the [`cargo` feature flag][crate::_features].** -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/02_crate.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/02_crate.md")] -//! -//! You can use [`Command`][crate::Command] methods to change the application level behavior of -//! clap, like [`Command::next_line_help`]. -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/02_app_settings.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/02_app_settings.md")] -//! -//! ## Adding Arguments -//! -//! ### Positionals -//! -//! You can have users specify values by their position on the command-line: -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/03_03_positional.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/03_03_positional.md")] -//! -//! Note that the default [`ArgAction`][crate::ArgAction] is [`Set`][crate::ArgAction::Set]. To -//! accept multiple values, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append]: -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/03_03_positional_mult.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/03_03_positional_mult.md")] -//! -//! ### Options -//! -//! You can name your arguments with a flag: -//! - Order doesn't matter -//! - They can be optional -//! - Intent is clearer -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/03_02_option.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/03_02_option.md")] -//! -//! Note that the default [`ArgAction`][crate::ArgAction] is [`Set`][crate::ArgAction::Set]. To -//! accept multiple occurrences, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append]: -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/03_02_option_mult.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/03_02_option_mult.md")] -//! -//! ### Flags -//! -//! Flags can also be switches that can be on/off: -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/03_01_flag_bool.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/03_01_flag_bool.md")] -//! -//! To accept multiple flags, use [`Count`][crate::ArgAction::Count]: -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/03_01_flag_count.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/03_01_flag_count.md")] -//! -//! ### Subcommands -//! -//! Subcommands are defined as [`Command`][crate::Command]s that get added via -//! [`Command::subcommand`][crate::Command::subcommand]. Each instance of a Subcommand can have its -//! own version, author(s), Args, and even its own subcommands. -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/03_04_subcommands.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/03_04_subcommands.md")] -//! -//! ### Defaults -//! -//! We've previously showed that arguments can be [`required`][crate::Arg::required] or optional. -//! When optional, you work with a `Option` and can `unwrap_or`. Alternatively, you can set -//! [`Arg::default_value`][crate::Arg::default_value]. -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/03_05_default_values.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/03_05_default_values.md")] -//! -//! ## Validation -//! -//! By default, arguments are assumed to be `String`s and only UTF-8 validation is performed. -//! -//! ### Enumerated values -//! -//! If you have arguments of specific values you want to test for, you can use the -//! [`PossibleValuesParser`][crate::builder::PossibleValuesParser] or [`Arg::value_parser(["val1", -//! ...])`][crate::Arg::value_parser] for short. -//! -//! This allows you specify the valid values for that argument. If the user does not use one of -//! those specific values, they will receive a graceful exit with error message informing them -//! of the mistake, and what the possible valid values are -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/04_01_possible.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/04_01_possible.md")] -//! -//! When enabling the [`derive` feature][crate::_features], you can use -//! [`ValueEnum`][crate::ValueEnum] to take care of the boiler plate for you, giving the same -//! results. -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/04_01_enum.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/04_01_enum.md")] -//! -//! ### Validated values -//! -//! More generally, you can validate and parse into any data type with [`Arg::value_parser`]. -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/04_02_parse.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/04_02_parse.md")] -//! -//! A [custom parser][TypedValueParser] can be used to improve the error messages or provide additional validation: -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/04_02_validate.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/04_02_validate.md")] -//! -//! See [`Arg::value_parser`][crate::Arg::value_parser] for more details. -//! -//! ### Argument Relations -//! -//! You can declare dependencies or conflicts between [`Arg`][crate::Arg]s or even -//! [`ArgGroup`][crate::ArgGroup]s. -//! -//! [`ArgGroup`][crate::ArgGroup]s make it easier to declare relations instead of having to list -//! each individually, or when you want a rule to apply "any but not all" arguments. -//! -//! Perhaps the most common use of [`ArgGroup`][crate::ArgGroup]s is to require one and *only* one -//! argument to be present out of a given set. Imagine that you had multiple arguments, and you -//! want one of them to be required, but making all of them required isn't feasible because perhaps -//! they conflict with each other. -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/04_03_relations.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/04_03_relations.md")] -//! -//! ### Custom Validation -//! -//! As a last resort, you can create custom errors with the basics of clap's formatting. -//! -//! ```rust -#![doc = include_str!("../../examples/tutorial_builder/04_04_custom.rs")] -//! ``` -#![doc = include_str!("../../examples/tutorial_builder/04_04_custom.md")] -//! -//! ## Testing -//! -//! clap reports most development errors as `debug_assert!`s. Rather than checking every -//! subcommand, you should have a test that calls -//! [`Command::debug_assert`][crate::Command::debug_assert]: -//! ```rust,no_run -#![doc = include_str!("../../examples/tutorial_builder/05_01_assert.rs")] -//! ``` -//! -//! ## Next Steps -//! -//! - [Cookbook][crate::_cookbook] for application-focused examples -//! - Explore more features in the [API reference][super] -//! -//! For support, see [Discussions](https://github.com/clap-rs/clap/discussions) +//! 1. [Quick Start][chapter_0] +//! 2. [Configuring the Parser][chapter_1] +//! 3. [Adding Arguments][chapter_2] +//! 1. [Positionals][chapter_2#positionals] +//! 2. [Options][chapter_2#options] +//! 3. [Flags][chapter_2#flags] +//! 4. [Subcommands][chapter_2#subcommands] +//! 5. [Defaults][chapter_2#defaults] +//! 4. [Validation][chapter_3] +//! 1. [Enumerated values][chapter_3#enumerated-values] +//! 2. [Validated values][chapter_3#validated-values] +//! 3. [Argument Relations][chapter_3#argument-relations] +//! 4. [Custom Validation][chapter_3#custom-validation] +//! 5. [Testing][chapter_4] +//! 6. [Next Steps][chapter_5] #![allow(unused_imports)] -use crate::builder::Arg; -use crate::builder::ArgGroup; -use crate::builder::Command; -use crate::builder::PossibleValue; -use crate::builder::TypedValueParser; +use crate::builder::*; + +pub mod chapter_0; +pub mod chapter_1; +pub mod chapter_2; +pub mod chapter_3; +pub mod chapter_4; +pub mod chapter_5; diff --git a/src/lib.rs b/src/lib.rs index 8b8b0d36959..6d0aab09dba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,8 +6,8 @@ //! > **Command Line Argument Parser for Rust** //! //! Quick Links: -//! - Derive [tutorial][_derive::_tutorial] and [reference][_derive] -//! - Builder [tutorial][_tutorial] and [reference](index.html) +//! - Derive [tutorial][_derive::_tutorial::chapter_0] and [reference][_derive] +//! - Builder [tutorial][_tutorial::chapter_0] and [reference](index.html) //! - [Cookbook][_cookbook] //! - [FAQ][_faq] //! - [Discussions](https://github.com/clap-rs/clap/discussions)