[go: up one dir, main page]

clap/_derive/
mod.rs

1//! # Documentation: Derive Reference
2//!
3//! 1. [Overview](#overview)
4//! 2. [Attributes](#attributes)
5//!     1. [Terminology](#terminology)
6//!     2. [Command Attributes](#command-attributes)
7//!     2. [ArgGroup Attributes](#arggroup-attributes)
8//!     3. [Arg Attributes](#arg-attributes)
9//!     4. [ValueEnum Attributes](#valueenum-attributes)
10//!     5. [Possible Value Attributes](#possible-value-attributes)
11//! 3. [Field Types](#field-types)
12//! 4. [Doc Comments](#doc-comments)
13//! 5. [Mixing Builder and Derive APIs](#mixing-builder-and-derive-apis)
14//! 6. [Tips](#tips)
15//!
16//! ## Overview
17//!
18//! To derive `clap` types, you need to enable the [`derive` feature flag][crate::_features].
19//!
20//! Example:
21//! ```rust
22#![doc = include_str!("../../examples/demo.rs")]
23//! ```
24//!
25//! Let's start by breaking down the anatomy of the derive attributes:
26//! ```rust
27//! use clap::{Parser, Args, Subcommand, ValueEnum};
28//!
29//! /// Doc comment
30//! #[derive(Parser)]
31//! #[command(CMD ATTRIBUTE)]
32//! #[group(GROUP ATTRIBUTE)]
33//! struct Cli {
34//!     /// Doc comment
35//!     #[arg(ARG ATTRIBUTE)]
36//!     field: UserType,
37//!
38//!     #[arg(value_enum, ARG ATTRIBUTE...)]
39//!     field: EnumValues,
40//!
41//!     #[command(flatten)]
42//!     delegate: Struct,
43//!
44//!     #[command(subcommand)]
45//!     command: Command,
46//! }
47//!
48//! /// Doc comment
49//! #[derive(Args)]
50//! #[command(PARENT CMD ATTRIBUTE)]
51//! #[group(GROUP ATTRIBUTE)]
52//! struct Struct {
53//!     /// Doc comment
54//!     #[command(ARG ATTRIBUTE)]
55//!     field: UserType,
56//! }
57//!
58//! /// Doc comment
59//! #[derive(Subcommand)]
60//! #[command(PARENT CMD ATTRIBUTE)]
61//! enum Command {
62//!     /// Doc comment
63//!     #[command(CMD ATTRIBUTE)]
64//!     Variant1(Struct),
65//!
66//!     /// Doc comment
67//!     #[command(CMD ATTRIBUTE)]
68//!     Variant2 {
69//!         /// Doc comment
70//!         #[arg(ARG ATTRIBUTE)]
71//!         field: UserType,
72//!     }
73//! }
74//!
75//! /// Doc comment
76//! #[derive(ValueEnum)]
77//! #[value(VALUE ENUM ATTRIBUTE)]
78//! enum EnumValues {
79//!     /// Doc comment
80//!     #[value(POSSIBLE VALUE ATTRIBUTE)]
81//!     Variant1,
82//! }
83//!
84//! fn main() {
85//!     let cli = Cli::parse();
86//! }
87//! ```
88//!
89//! Traits:
90//! - [`Parser`][crate::Parser] parses arguments into a `struct` (arguments) or `enum` (subcommands).
91//!   - [`Args`][crate::Args] allows defining a set of re-usable arguments that get merged into their parent container.
92//!   - [`Subcommand`][crate::Subcommand] defines available subcommands.
93//!   - Subcommand arguments can be defined in a struct-variant or automatically flattened with a tuple-variant.
94//! - [`ValueEnum`][crate::ValueEnum] allows parsing a value directly into an `enum`, erroring on unsupported values.
95//!   - The derive doesn't work on enums that contain non-unit variants, unless they are skipped
96//!
97//! *See also the [derive tutorial][crate::_derive::_tutorial] and [cookbook][crate::_cookbook]*
98//!
99//! ## Attributes
100//!
101//! ### Terminology
102//!
103//! **Raw attributes** are forwarded directly to the underlying [`clap` builder][crate::builder].  Any
104//! [`Command`][crate::Command], [`Arg`][crate::Arg], or [`PossibleValue`][crate::builder::PossibleValue] method can be used as an attribute.
105//!
106//! Raw attributes come in two different syntaxes:
107//! ```rust,ignore
108//! #[arg(
109//!     global = true, // name = arg form, neat for one-arg methods
110//!     required_if_eq("out", "file") // name(arg1, arg2, ...) form.
111//! )]
112//! ```
113//!
114//! - `method = arg` can only be used for methods which take only one argument.
115//! - `method(arg1, arg2)` can be used with any method.
116//!
117//! As long as `method_name` is not one of the magical methods it will be
118//! translated into a mere method call.
119//!
120//! **Magic attributes** have post-processing done to them, whether that is
121//! - Providing of defaults
122//! - Special behavior is triggered off of it
123//!
124//! Magic attributes are more constrained in the syntax they support, usually just
125//! `<attr> = <value>` though some use `<attr>(<value>)` instead.  See the specific
126//! magic attributes documentation for details.  This allows users to access the
127//! raw behavior of an attribute via `<attr>(<value>)` syntax.
128//!
129//! <div class="warning">
130//!
131//! **NOTE:** Some attributes are inferred from [Arg Types](#arg-types) and [Doc
132//! Comments](#doc-comments).  Explicit attributes take precedence over inferred
133//! attributes.
134//!
135//! </div>
136//!
137//! ### Command Attributes
138//!
139//! These correspond to a [`Command`][crate::Command] which is used for both top-level parsers and
140//! when defining subcommands.
141//!
142//! **Raw attributes:**  Any [`Command` method][crate::Command] can also be used as an attribute,
143//! see [Terminology](#terminology) for syntax.
144//! - e.g. `#[command(arg_required_else_help(true))]` would translate to `cmd.arg_required_else_help(true)`
145//!
146//! **Magic attributes:**
147//! - `name  = <expr>`: [`Command::name`][crate::Command::name]
148//!   - When not present: [package `name`](https://doc.rust-lang.org/cargo/reference/manifest.html#the-name-field) (if on [`Parser`][crate::Parser] container), variant name (if on [`Subcommand`][crate::Subcommand] variant)
149//! - `version [= <expr>]`: [`Command::version`][crate::Command::version]
150//!   - When not present: no version set
151//!   - Without `<expr>`: defaults to [crate `version`](https://doc.rust-lang.org/cargo/reference/manifest.html#the-version-field)
152//! - `author [= <expr>]`: [`Command::author`][crate::Command::author]
153//!   - When not present: no author set
154//!   - Without `<expr>`: defaults to [crate `authors`](https://doc.rust-lang.org/cargo/reference/manifest.html#the-authors-field)
155//!   - **NOTE:** A custom [`help_template`][crate::Command::help_template] is needed for author to show up.
156//! - `about [= <expr>]`: [`Command::about`][crate::Command::about]
157//!   - When not present: [Doc comment summary](#doc-comments)
158//!   - Without `<expr>`: [crate `description`](https://doc.rust-lang.org/cargo/reference/manifest.html#the-description-field) ([`Parser`][crate::Parser] container)
159//!     - **TIP:** When a doc comment is also present, you most likely want to add
160//!       `#[arg(long_about = None)]` to clear the doc comment so only [`about`][crate::Command::about]
161//!       gets shown with both `-h` and `--help`.
162//! - `long_about[ = <expr>]`: [`Command::long_about`][crate::Command::long_about]
163//!   - When not present: [Doc comment](#doc-comments) if there is a blank line, else nothing
164//!   - When present without a value: [Doc comment](#doc-comments)
165//! - `verbatim_doc_comment`: Minimizes pre-processing when converting doc comments to [`about`][crate::Command::about] / [`long_about`][crate::Command::long_about]
166//! - `next_display_order`: [`Command::next_display_order`][crate::Command::next_display_order]
167//! - `next_help_heading`: [`Command::next_help_heading`][crate::Command::next_help_heading]
168//!   - When `flatten`ing [`Args`][crate::Args], this is scoped to just the args in this struct and any struct `flatten`ed into it
169//! - `rename_all = <string_literal>`: Override default field / variant name case conversion for [`Command::name`][crate::Command::name] / [`Arg::id`][crate::Arg::id]
170//!   - When not present: `"kebab-case"`
171//!   - Available values: `"camelCase"`, `"kebab-case"`, `"PascalCase"`, `"SCREAMING_SNAKE_CASE"`, `"snake_case"`, `"lower"`, `"UPPER"`, `"verbatim"`
172//! - `rename_all_env = <string_literal>`: Override default field name case conversion for env variables for  [`Arg::env`][crate::Arg::env]
173//!   - When not present: `"SCREAMING_SNAKE_CASE"`
174//!   - Available values: `"camelCase"`, `"kebab-case"`, `"PascalCase"`, `"SCREAMING_SNAKE_CASE"`, `"snake_case"`, `"lower"`, `"UPPER"`, `"verbatim"`
175//!
176//! And for [`Subcommand`][crate::Subcommand] variants:
177//! - `skip`: Ignore this variant
178//! - `flatten`: Delegates to the variant for more subcommands (must implement
179//!   [`Subcommand`][crate::Subcommand])
180//! - `subcommand`: Nest subcommands under the current set of subcommands (must implement
181//!   [`Subcommand`][crate::Subcommand])
182//! - `external_subcommand`: [`Command::allow_external_subcommand(true)`][crate::Command::allow_external_subcommands]
183//!   - Variant must be either `Variant(Vec<String>)` or `Variant(Vec<OsString>)`
184//!
185//! And for [`Args`][crate::Args] fields:
186//! - `flatten`: Delegates to the field for more arguments (must implement [`Args`][crate::Args])
187//!   - Only [`next_help_heading`][crate::Command::next_help_heading] can be used with `flatten`.  See
188//!     [clap-rs/clap#3269](https://github.com/clap-rs/clap/issues/3269) for why
189//!     arg attributes are not generally supported.
190//!   - **Tip:** Though we do apply a flattened [`Args`][crate::Args]'s Parent Command Attributes, this
191//!     makes reuse harder. Generally prefer putting the cmd attributes on the
192//!     [`Parser`][crate::Parser] or on the flattened field.
193//! - `subcommand`: Delegates definition of subcommands to the field (must implement
194//!   [`Subcommand`][crate::Subcommand])
195//!   - When `Option<T>`, the subcommand becomes optional
196//!
197//! See [Configuring the Parser][_tutorial#configuring-the-parser] and
198//! [Subcommands][_tutorial#subcommands] from the tutorial.
199//!
200//! ### ArgGroup Attributes
201//!
202//! These correspond to the [`ArgGroup`][crate::ArgGroup] which is implicitly created for each
203//! `Args` derive.
204//!
205//! **Raw attributes:**  Any [`ArgGroup` method][crate::ArgGroup] can also be used as an attribute, see [Terminology](#terminology) for syntax.
206//! - e.g. `#[group(required = true)]` would translate to `arg_group.required(true)`
207//!
208//! **Magic attributes**:
209//! - `id = <expr>`: [`ArgGroup::id`][crate::ArgGroup::id]
210//!   - When not present: struct's name is used
211//! - `skip [= <expr>]`: Ignore this field, filling in with `<expr>`
212//!   - Without `<expr>`: fills the field with `Default::default()`
213//!
214//! Note:
215//! - For `struct`s, [`multiple = true`][crate::ArgGroup::multiple] is implied
216//! - `enum` support is tracked at [#2621](https://github.com/clap-rs/clap/issues/2621)
217//!
218//! See [Argument Relations][_tutorial#argument-relations] from the tutorial.
219//!
220//! ### Arg Attributes
221//!
222//! These correspond to a [`Arg`][crate::Arg].
223//! The default state for a field without attributes is to be a positional argument with [behavior
224//! inferred from the field type](#arg-types).
225//! `#[arg(...)]` attributes allow overriding or extending those defaults.
226//!
227//! **Raw attributes:**  Any [`Arg` method][crate::Arg] can also be used as an attribute, see [Terminology](#terminology) for syntax.
228//! - e.g. `#[arg(num_args(..=3))]` would translate to `arg.num_args(..=3)`
229//!
230//! **Magic attributes**:
231//! - `id = <expr>`: [`Arg::id`][crate::Arg::id]
232//!   - When not present: field's name is used
233//! - `value_parser [= <expr>]`: [`Arg::value_parser`][crate::Arg::value_parser]
234//!   - When not present: will auto-select an implementation based on the field type using
235//!     [`value_parser!`][crate::value_parser!]
236//! - `action [= <expr>]`: [`Arg::action`][crate::Arg::action]
237//!   - When not present: will auto-select an action based on the field type
238//! - `help = <expr>`: [`Arg::help`][crate::Arg::help]
239//!   - When not present: [Doc comment summary](#doc-comments)
240//! - `long_help[ = <expr>]`: [`Arg::long_help`][crate::Arg::long_help]
241//!   - When not present: [Doc comment](#doc-comments) if there is a blank line, else nothing
242//!   - When present without a value: [Doc comment](#doc-comments)
243//! - `verbatim_doc_comment`: Minimizes pre-processing when converting doc comments to [`help`][crate::Arg::help] / [`long_help`][crate::Arg::long_help]
244//! - `short [= <char>]`: [`Arg::short`][crate::Arg::short]
245//!   - When not present: no short set
246//!   - Without `<char>`: defaults to first character in the case-converted field name
247//! - `long [= <str>]`: [`Arg::long`][crate::Arg::long]
248//!   - When not present: no long set
249//!   - Without `<str>`: defaults to the case-converted field name
250//! - `env [= <str>]`: [`Arg::env`][crate::Arg::env] (needs [`env` feature][crate::_features] enabled)
251//!   - When not present: no env set
252//!   - Without `<str>`: defaults to the case-converted field name
253//! - `from_global`: Read a [`Arg::global`][crate::Arg::global] argument (raw attribute), regardless of what subcommand you are in
254//! - `value_enum`: Parse the value using the [`ValueEnum`][crate::ValueEnum]
255//! - `skip [= <expr>]`: Ignore this field, filling in with `<expr>`
256//!   - Without `<expr>`: fills the field with `Default::default()`
257//! - `default_value = <str>`: [`Arg::default_value`][crate::Arg::default_value] and [`Arg::required(false)`][crate::Arg::required]
258//! - `default_value_t [= <expr>]`: [`Arg::default_value`][crate::Arg::default_value] and [`Arg::required(false)`][crate::Arg::required]
259//!   - Requires `std::fmt::Display` that roundtrips correctly with the
260//!     [`Arg::value_parser`][crate::Arg::value_parser] or `#[arg(value_enum)]`
261//!   - Without `<expr>`, relies on `Default::default()`
262//! - `default_values_t = <expr>`: [`Arg::default_values`][crate::Arg::default_values] and [`Arg::required(false)`][crate::Arg::required]
263//!   - Requires field arg to be of type `Vec<T>` and `T` to implement `std::fmt::Display` or `#[arg(value_enum)]`
264//!   - `<expr>` must implement `IntoIterator<T>`
265//! - `default_value_os_t [= <expr>]`: [`Arg::default_value_os`][crate::Arg::default_value_os] and [`Arg::required(false)`][crate::Arg::required]
266//!   - Requires `std::convert::Into<OsString>` or `#[arg(value_enum)]`
267//!   - Without `<expr>`, relies on `Default::default()`
268//! - `default_values_os_t = <expr>`: [`Arg::default_values_os`][crate::Arg::default_values_os] and [`Arg::required(false)`][crate::Arg::required]
269//!   - Requires field arg to be of type `Vec<T>` and `T` to implement `std::convert::Into<OsString>` or `#[arg(value_enum)]`
270//!   - `<expr>` must implement `IntoIterator<T>`
271//!
272//! See [Adding Arguments][_tutorial#adding-arguments] and [Validation][_tutorial#validation] from the
273//! tutorial.
274//!
275//! ### ValueEnum Attributes
276//!
277//! - `rename_all = <string_literal>`: Override default field / variant name case conversion for [`PossibleValue::new`][crate::builder::PossibleValue]
278//!   - When not present: `"kebab-case"`
279//!   - Available values: `"camelCase"`, `"kebab-case"`, `"PascalCase"`, `"SCREAMING_SNAKE_CASE"`, `"snake_case"`, `"lower"`, `"UPPER"`, `"verbatim"`
280//!
281//! See [Enumerated values][_tutorial#enumerated-values] from the tutorial.
282//!
283//! ### Possible Value Attributes
284//!
285//! These correspond to a [`PossibleValue`][crate::builder::PossibleValue].
286//!
287//! **Raw attributes:**  Any [`PossibleValue` method][crate::builder::PossibleValue] can also be used as an attribute, see [Terminology](#terminology) for syntax.
288//! - e.g. `#[value(alias("foo"))]` would translate to `pv.alias("foo")`
289//!
290//! **Magic attributes**:
291//! - `name = <expr>`: [`PossibleValue::new`][crate::builder::PossibleValue::new]
292//!   - When not present: case-converted field name is used
293//! - `help = <expr>`: [`PossibleValue::help`][crate::builder::PossibleValue::help]
294//!   - When not present: [Doc comment summary](#doc-comments)
295//! - `skip`: Ignore this variant
296//!
297//! ## Field Types
298//!
299//! `clap` assumes some intent based on the type used.
300//!
301//! ### Subcommand Types
302//!
303//! | Type                  | Effect              | Implies                                                   |
304//! |-----------------------|---------------------|-----------------------------------------------------------|
305//! | `Option<T>`           | optional subcommand |                                                           |
306//! | `T`                   | required subcommand | `.subcommand_required(true).arg_required_else_help(true)` |
307//!
308//! ### Arg Types
309//!
310//! | Type                  | Effect                                               | Implies                                                     | Notes |
311//! |-----------------------|------------------------------------------------------|-------------------------------------------------------------|-------|
312//! | `()`                  | user-defined                                         | `.action(ArgAction::Set).required(false)`                   |       |
313//! | `bool`                | flag                                                 | `.action(ArgAction::SetTrue)`                               |       |
314//! | `Option<T>`           | optional argument                                    | `.action(ArgAction::Set).required(false)`                   |       |
315//! | `Option<Option<T>>`   | optional value for optional argument                 | `.action(ArgAction::Set).required(false).num_args(0..=1)`   |       |
316//! | `T`                   | required argument                                    | `.action(ArgAction::Set).required(!has_default)`            |       |
317//! | `Vec<T>`              | `0..` occurrences of argument                        | `.action(ArgAction::Append).required(false)`  |       |
318//! | `Option<Vec<T>>`      | `0..` occurrences of argument                        | `.action(ArgAction::Append).required(false)`  |       |
319//! | `Vec<Vec<T>>`         | `0..` occurrences of argument, grouped by occurrence | `.action(ArgAction::Append).required(false)`  | requires `unstable-v5` |
320//! | `Option<Vec<Vec<T>>>` | `0..` occurrences of argument, grouped by occurrence | `.action(ArgAction::Append).required(false)`  | requires `unstable-v5` |
321//!
322//! In addition, [`.value_parser(value_parser!(T))`][crate::value_parser!] is called for each
323//! field in the absence of a [`#[arg(value_parser)]` attribute](#arg-attributes).
324//!
325//! Notes:
326//! - For custom type behavior, you can override the implied attributes/settings and/or set additional ones
327//!   - To force any inferred type (like `Vec<T>`) to be treated as `T`, you can refer to the type
328//!     by another means, like using `std::vec::Vec` instead of `Vec`.  For improving this, see
329//!     [#4626](https://github.com/clap-rs/clap/issues/4626).
330//! - `Option<Vec<T>>` and `Option<Vec<Vec<T>>` will be `None` instead of `vec![]` if no arguments are provided.
331//!   - This gives the user some flexibility in designing their argument, like with `num_args(0..)`
332//! - `Vec<Vec<T>>` will need [`Arg::num_args`][crate::Arg::num_args] set to be meaningful
333//!
334//! ## Doc Comments
335//!
336//! In clap, help messages for the whole binary can be specified
337//! via [`Command::about`][crate::Command::about] and [`Command::long_about`][crate::Command::long_about] while help messages
338//! for individual arguments can be specified via [`Arg::help`][crate::Arg::help] and [`Arg::long_help`][crate::Arg::long_help].
339//!
340//! `long_*` variants are used when user calls the program with
341//! `--help` and "short" variants are used with `-h` flag.
342//!
343//! ```rust
344//! # use clap::Parser;
345//!
346//! #[derive(Parser)]
347//! #[command(about = "I am a program and I work, just pass `-h`", long_about = None)]
348//! struct Foo {
349//!     #[arg(short, help = "Pass `-h` and you'll see me!")]
350//!     bar: String,
351//! }
352//! ```
353//!
354//! For convenience, doc comments can be used instead of raw methods
355//! (this example works exactly like the one above):
356//!
357//! ```rust
358//! # use clap::Parser;
359//!
360//! #[derive(Parser)]
361//! /// I am a program and I work, just pass `-h`
362//! struct Foo {
363//!     /// Pass `-h` and you'll see me!
364//!     bar: String,
365//! }
366//! ```
367//!
368//! <div class="warning">
369//!
370//! **NOTE:** Attributes have priority over doc comments!
371//!
372//! **Top level doc comments always generate `Command::about/long_about` calls!**
373//! If you really want to use the `Command::about/long_about` methods (you likely don't),
374//! use the `about` / `long_about` attributes to override the calls generated from
375//! the doc comment.  To clear `long_about`, you can use
376//! `#[command(long_about = None)]`.
377//!
378//! </div>
379//!
380//! ### Pre-processing
381//!
382//! ```rust
383//! # use clap::Parser;
384//! #[derive(Parser)]
385//! /// Hi there, I'm Robo!
386//! ///
387//! /// I like beeping, stumbling, eating your electricity,
388//! /// and making records of you singing in a shower.
389//! /// Pay up, or I'll upload it to youtube!
390//! struct Robo {
391//!     /// Call my brother SkyNet.
392//!     ///
393//!     /// I am artificial superintelligence. I won't rest
394//!     /// until I'll have destroyed humanity. Enjoy your
395//!     /// pathetic existence, you mere mortals.
396//!     #[arg(long, action)]
397//!     kill_all_humans: bool,
398//! }
399//! ```
400//!
401//! A doc comment consists of three parts:
402//! - Short summary
403//! - A blank line (whitespace only)
404//! - Detailed description, all the rest
405//!
406//! The summary corresponds with `Command::about` / `Arg::help`.  When a blank line is
407//! present, the whole doc comment will be passed to `Command::long_about` /
408//! `Arg::long_help`.  Or in other words, a doc may result in just a `Command::about` /
409//! `Arg::help` or `Command::about` / `Arg::help` and `Command::long_about` /
410//! `Arg::long_help`
411//!
412//! In addition, when `verbatim_doc_comment` is not present, `clap` applies some preprocessing, including:
413//!
414//! - Strip leading and trailing whitespace from every line, if present.
415//!
416//! - Strip leading and trailing blank lines, if present.
417//!
418//! - Interpret each group of non-empty lines as a word-wrapped paragraph.
419//!
420//!   We replace newlines within paragraphs with spaces to allow the output
421//!   to be re-wrapped to the terminal width.
422//!
423//! - Strip any excess blank lines so that there is exactly one per paragraph break.
424//!
425//! - If the first paragraph ends in exactly one period,
426//!   remove the trailing period (i.e. strip trailing periods but not trailing ellipses).
427//!
428//! Sometimes you don't want this preprocessing to apply, for example the comment contains
429//! some ASCII art or markdown tables, you would need to preserve LFs along with
430//! blank lines and the leading/trailing whitespace. When you pass use the
431//! `verbatim_doc_comment` magic attribute, you  preserve
432//! them.
433//!
434//! **Note:** Keep in mind that `verbatim_doc_comment` will *still*
435//! - Remove one leading space from each line, even if this attribute is present,
436//!   to allow for a space between `///` and the content.
437//! - Remove leading and trailing blank lines
438//!
439//! ## Mixing Builder and Derive APIs
440//!
441//! The builder and derive APIs do not live in isolation. They can work together, which is
442//! especially helpful if some arguments can be specified at compile-time while others must be
443//! specified at runtime.
444//!
445//! ### Using derived arguments in a builder application
446//!
447//! When using the derive API, you can `#[command(flatten)]` a struct deriving `Args` into a struct
448//! deriving `Args` or `Parser`. This example shows how you can augment a `Command` instance
449//! created using the builder API with `Args` created using the derive API.
450//!
451//! It uses the [`Args::augment_args`][crate::Args::augment_args] method to add the arguments to
452//! the `Command` instance.
453//!
454//! Crates such as [clap-verbosity-flag](https://github.com/rust-cli/clap-verbosity-flag) provide
455//! structs that implement `Args`. Without the technique shown in this example, it would not be
456//! possible to use such crates with the builder API.
457//!
458//! For example:
459//! ```rust
460#![doc = include_str!("../../examples/derive_ref/augment_args.rs")]
461//! ```
462//!
463//! ### Using derived subcommands in a builder application
464//!
465//! When using the derive API, you can use `#[command(subcommand)]` inside the struct to add
466//! subcommands. The type of the field is usually an enum that derived `Parser`. However, you can
467//! also add the subcommands in that enum to a `Command` instance created with the builder API.
468//!
469//! It uses the [`Subcommand::augment_subcommands`][crate::Subcommand::augment_subcommands] method
470//! to add the subcommands to the `Command` instance.
471//!
472//! For example:
473//! ```rust
474#![doc = include_str!("../../examples/derive_ref/augment_subcommands.rs")]
475//! ```
476//!
477//! ### Adding hand-implemented subcommands to a derived application
478//!
479//! When using the derive API, you can use `#[command(subcommand)]` inside the struct to add
480//! subcommands. The type of the field is usually an enum that derived `Parser`. However, you can
481//! also implement the `Subcommand` trait manually on this enum (or any other type) and it can
482//! still be used inside the struct created with the derive API. The implementation of the
483//! `Subcommand` trait will use the builder API to add the subcommands to the `Command` instance
484//! created behind the scenes for you by the derive API.
485//!
486//! Notice how in the previous example we used
487//! [`augment_subcommands`][crate::Subcommand::augment_subcommands] on an enum that derived
488//! `Parser`, whereas now we implement
489//! [`augment_subcommands`][crate::Subcommand::augment_subcommands] ourselves, but the derive API
490//! calls it automatically since we used the `#[command(subcommand)]` attribute.
491//!
492//! For example:
493//! ```rust
494#![doc = include_str!("../../examples/derive_ref/hand_subcommand.rs")]
495//! ```
496//!
497//! ### Flattening hand-implemented args into a derived application
498//!
499//! When using the derive API, you can use `#[command(flatten)]` inside the struct to add arguments as
500//! if they were added directly to the containing struct. The type of the field is usually an
501//! struct that derived `Args`. However, you can also implement the `Args` trait manually on this
502//! struct (or any other type) and it can still be used inside the struct created with the derive
503//! API. The implementation of the `Args` trait will use the builder API to add the arguments to
504//! the `Command` instance created behind the scenes for you by the derive API.
505//!
506//! Notice how in the previous example we used [`augment_args`][crate::Args::augment_args] on the
507//! struct that derived `Parser`, whereas now we implement
508//! [`augment_args`][crate::Args::augment_args] ourselves, but the derive API calls it
509//! automatically since we used the `#[command(flatten)]` attribute.
510//!
511//! For example:
512//! ```rust
513#![doc = include_str!("../../examples/derive_ref/flatten_hand_args.rs")]
514//! ```
515//!
516//! ## Tips
517//!
518//! - To get access to a [`Command`][crate::Command] call
519//!   [`CommandFactory::command`][crate::CommandFactory::command] (implemented when deriving
520//!   [`Parser`][crate::Parser])
521//! - Proactively check for bad [`Command`][crate::Command] configurations by calling
522//!   [`Command::debug_assert`][crate::Command::debug_assert] in a test
523//!   ([example][_tutorial#testing])
524//! - Always remember to [document](#doc-comments) args and commands with `#![deny(missing_docs)]`
525
526// Point people here that search for attributes that don't exist in the derive (a subset of magic
527// attributes)
528#![doc(alias = "skip")]
529#![doc(alias = "verbatim_doc_comment")]
530#![doc(alias = "flatten")]
531#![doc(alias = "external_subcommand")]
532#![doc(alias = "subcommand")]
533#![doc(alias = "rename_all")]
534#![doc(alias = "rename_all_env")]
535#![doc(alias = "default_value_t")]
536#![doc(alias = "default_values_t")]
537#![doc(alias = "default_value_os_t")]
538#![doc(alias = "default_values_os_t")]
539
540pub mod _tutorial;
541#[doc(inline)]
542pub use crate::_cookbook;