]>
git.proxmox.com Git - rustc.git/blob - vendor/clap-3.2.20/src/_tutorial.rs
4 // - Please update the corresponding section in the derive tutorial
5 // - Building: They must be added to `Cargo.toml` with the appropriate `required-features`.
6 // - Testing: Ensure there is a markdown file with [trycmd](https://docs.rs/trycmd) syntax
8 // See also the general CONTRIBUTING
10 //! # Documentation: Builder Tutorial
12 //! 1. [Quick Start](#quick-start)
13 //! 2. [Configuring the Parser](#configuring-the-parser)
14 //! 3. [Adding Arguments](#adding-arguments)
15 //! 1. [Positionals](#positionals)
16 //! 2. [Options](#options)
17 //! 3. [Flags](#flags)
18 //! 4. [Subcommands](#subcommands)
19 //! 5. [Defaults](#defaults)
21 //! 1. [Enumerated values](#enumerated-values)
22 //! 2. [Validated values](#validated-values)
23 //! 3. [Argument Relations](#argument-relations)
24 //! 4. [Custom Validation](#custom-validation)
25 //! 5. [Testing](#testing)
28 //! - [FAQ: When should I use the builder vs derive APIs?][crate::_faq#when-should-i-use-the-builder-vs-derive-apis]
29 //! - The [cookbook][crate::_cookbook] for more application-focused examples
33 //! You can create an application with several arguments using usage strings.
36 #![doc = include_str!("../examples/tutorial_builder/01_quick.rs")]
39 #![doc = include_str!("../examples/tutorial_builder/01_quick.md")]
41 //! ## Configuring the Parser
43 //! You use [`Command`][crate::Command] to start building a parser.
46 #![doc = include_str!("../examples/tutorial_builder/02_apps.rs")]
49 #![doc = include_str!("../examples/tutorial_builder/02_apps.md")]
51 //! You can use [`command!()`][crate::command!] to fill these fields in from your `Cargo.toml`
52 //! file. **This requires the [`cargo` feature flag][crate::_features].**
55 #![doc = include_str!("../examples/tutorial_builder/02_crate.rs")]
57 #![doc = include_str!("../examples/tutorial_builder/02_crate.md")]
59 //! You can use [`Command`][crate::Command] methods to change the application level behavior of
63 #![doc = include_str!("../examples/tutorial_builder/02_app_settings.rs")]
65 #![doc = include_str!("../examples/tutorial_builder/02_app_settings.md")]
67 //! ## Adding Arguments
71 //! You can have users specify values by their position on the command-line:
74 #![doc = include_str!("../examples/tutorial_builder/03_03_positional.rs")]
76 #![doc = include_str!("../examples/tutorial_builder/03_03_positional.md")]
80 //! You can name your arguments with a flag:
81 //! - Order doesn't matter
82 //! - They can be optional
83 //! - Intent is clearer
86 #![doc = include_str!("../examples/tutorial_builder/03_02_option.rs")]
88 #![doc = include_str!("../examples/tutorial_builder/03_02_option.md")]
92 //! Flags can also be switches that can be on/off:
95 #![doc = include_str!("../examples/tutorial_builder/03_01_flag_bool.rs")]
97 #![doc = include_str!("../examples/tutorial_builder/03_01_flag_bool.md")]
102 #![doc = include_str!("../examples/tutorial_builder/03_01_flag_count.rs")]
104 #![doc = include_str!("../examples/tutorial_builder/03_01_flag_count.md")]
108 //! Subcommands are defined as [`Command`][crate::Command]s that get added via
109 //! [`Command::subcommand`][crate::Command::subcommand]. Each instance of a Subcommand can have its
110 //! own version, author(s), Args, and even its own subcommands.
113 #![doc = include_str!("../examples/tutorial_builder/03_04_subcommands.rs")]
115 #![doc = include_str!("../examples/tutorial_builder/03_04_subcommands.md")]
119 //! We've previously showed that arguments can be [`required`][crate::Arg::required] or optional.
120 //! When optional, you work with a `Option` and can `unwrap_or`. Alternatively, you can set
121 //! [`Arg::default_value`][crate::Arg::default_value].
124 #![doc = include_str!("../examples/tutorial_builder/03_05_default_values.rs")]
126 #![doc = include_str!("../examples/tutorial_builder/03_05_default_values.md")]
130 //! ### Enumerated values
132 //! If you have arguments of specific values you want to test for, you can use the
133 //! [`PossibleValuesParser`][crate::builder::PossibleValuesParser] or [`Arg::value_parser(["val1",
134 //! ...])`][crate::Arg::value_parser] for short.
136 //! This allows you specify the valid values for that argument. If the user does not use one of
137 //! those specific values, they will receive a graceful exit with error message informing them
138 //! of the mistake, and what the possible valid values are
141 #![doc = include_str!("../examples/tutorial_builder/04_01_possible.rs")]
143 #![doc = include_str!("../examples/tutorial_builder/04_01_possible.md")]
145 //! When enabling the [`derive` feature][crate::_features], you can use
146 //! [`ValueEnum`][crate::ValueEnum] to take care of the boiler plate for you, giving the same
150 #![doc = include_str!("../examples/tutorial_builder/04_01_enum.rs")]
152 #![doc = include_str!("../examples/tutorial_builder/04_01_enum.md")]
154 //! ### Validated values
156 //! More generally, you can validate and parse into any data type.
159 #![doc = include_str!("../examples/tutorial_builder/04_02_parse.rs")]
161 #![doc = include_str!("../examples/tutorial_builder/04_02_parse.md")]
163 //! A custom parser can be used to improve the error messages or provide additional validation:
166 #![doc = include_str!("../examples/tutorial_builder/04_02_validate.rs")]
168 #![doc = include_str!("../examples/tutorial_builder/04_02_validate.md")]
170 //! ### Argument Relations
172 //! You can declare dependencies or conflicts between [`Arg`][crate::Arg]s or even
173 //! [`ArgGroup`][crate::ArgGroup]s.
175 //! [`ArgGroup`][crate::ArgGroup]s make it easier to declare relations instead of having to list
176 //! each individually, or when you want a rule to apply "any but not all" arguments.
178 //! Perhaps the most common use of [`ArgGroup`][crate::ArgGroup]s is to require one and *only* one
179 //! argument to be present out of a given set. Imagine that you had multiple arguments, and you
180 //! want one of them to be required, but making all of them required isn't feasible because perhaps
181 //! they conflict with each other.
184 #![doc = include_str!("../examples/tutorial_builder/04_03_relations.rs")]
186 #![doc = include_str!("../examples/tutorial_builder/04_03_relations.md")]
188 //! ### Custom Validation
190 //! As a last resort, you can create custom errors with the basics of clap's formatting.
193 #![doc = include_str!("../examples/tutorial_builder/04_04_custom.rs")]
195 #![doc = include_str!("../examples/tutorial_builder/04_04_custom.md")]
199 //! clap reports most development errors as `debug_assert!`s. Rather than checking every
200 //! subcommand, you should have a test that calls
201 //! [`Command::debug_assert`][crate::App::debug_assert]:
203 #![doc = include_str!("../examples/tutorial_builder/05_01_assert.rs")]