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