| 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. [Arg Types](#arg-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 |
//! **NOTE:** Some attributes are inferred from [Arg Types](#arg-types) and [Doc |
| 130 |
//! Comments](#doc-comments). Explicit attributes take precedence over inferred |
| 131 |
//! attributes. |
| 132 |
//! |
| 133 |
//! ### Command Attributes |
| 134 |
//! |
| 135 |
//! These correspond to a [`Command`][crate::Command] which is used for both top-level parsers and |
| 136 |
//! when defining subcommands. |
| 137 |
//! |
| 138 |
//! **Raw attributes:** Any [`Command` method][crate::Command] can also be used as an attribute, |
| 139 |
//! see [Terminology](#terminology) for syntax. |
| 140 |
//! - e.g. `#[command(arg_required_else_help(true))]` would translate to `cmd.arg_required_else_help(true)` |
| 141 |
//! |
| 142 |
//! **Magic attributes:** |
| 143 |
//! - `name = <expr>`: [`Command::name`][crate::Command::name] |
| 144 |
//! - 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) |
| 145 |
//! - `version [= <expr>]`: [`Command::version`][crate::Command::version] |
| 146 |
//! - When not present: no version set |
| 147 |
//! - Without `<expr>`: defaults to [crate `version`](https://doc.rust-lang.org/cargo/reference/manifest.html#the-version-field) |
| 148 |
//! - `author [= <expr>]`: [`Command::author`][crate::Command::author] |
| 149 |
//! - When not present: no author set |
| 150 |
//! - Without `<expr>`: defaults to [crate `authors`](https://doc.rust-lang.org/cargo/reference/manifest.html#the-authors-field) |
| 151 |
//! - **NOTE:** A custom [`help_template`][crate::Command::help_template] is needed for author to show up. |
| 152 |
//! - `about [= <expr>]`: [`Command::about`][crate::Command::about] |
| 153 |
//! - When not present: [Doc comment summary](#doc-comments) |
| 154 |
//! - Without `<expr>`: [crate `description`](https://doc.rust-lang.org/cargo/reference/manifest.html#the-description-field) ([`Parser`][crate::Parser] container) |
| 155 |
//! - **TIP:** When a doc comment is also present, you most likely want to add |
| 156 |
//! `#[arg(long_about = None)]` to clear the doc comment so only [`about`][crate::Command::about] |
| 157 |
//! gets shown with both `-h` and `--help`. |
| 158 |
//! - `long_about[ = <expr>]`: [`Command::long_about`][crate::Command::long_about] |
| 159 |
//! - When not present: [Doc comment](#doc-comments) if there is a blank line, else nothing |
| 160 |
//! - When present without a value: [Doc comment](#doc-comments) |
| 161 |
//! - `verbatim_doc_comment`: Minimizes pre-processing when converting doc comments to [`about`][crate::Command::about] / [`long_about`][crate::Command::long_about] |
| 162 |
//! - `next_display_order`: [`Command::next_display_order`][crate::Command::next_display_order] |
| 163 |
//! - `next_help_heading`: [`Command::next_help_heading`][crate::Command::next_help_heading] |
| 164 |
//! - When `flatten`ing [`Args`][crate::Args], this is scoped to just the args in this struct and any struct `flatten`ed into it |
| 165 |
//! - `rename_all = <string_literal>`: Override default field / variant name case conversion for [`Command::name`][crate::Command::name] / [`Arg::id`][crate::Arg::id] |
| 166 |
//! - When not present: `"kebab-case"` |
| 167 |
//! - Available values: `"camelCase"`, `"kebab-case"`, `"PascalCase"`, `"SCREAMING_SNAKE_CASE"`, `"snake_case"`, `"lower"`, `"UPPER"`, `"verbatim"` |
| 168 |
//! - `rename_all_env = <string_literal>`: Override default field name case conversion for env variables for [`Arg::env`][crate::Arg::env] |
| 169 |
//! - When not present: `"SCREAMING_SNAKE_CASE"` |
| 170 |
//! - Available values: `"camelCase"`, `"kebab-case"`, `"PascalCase"`, `"SCREAMING_SNAKE_CASE"`, `"snake_case"`, `"lower"`, `"UPPER"`, `"verbatim"` |
| 171 |
//! |
| 172 |
//! And for [`Subcommand`][crate::Subcommand] variants: |
| 173 |
//! - `skip`: Ignore this variant |
| 174 |
//! - `flatten`: Delegates to the variant for more subcommands (must implement |
| 175 |
//! [`Subcommand`][crate::Subcommand]) |
| 176 |
//! - `subcommand`: Nest subcommands under the current set of subcommands (must implement |
| 177 |
//! [`Subcommand`][crate::Subcommand]) |
| 178 |
//! - `external_subcommand`: [`Command::allow_external_subcommand(true)`][crate::Command::allow_external_subcommands] |
| 179 |
//! - Variant must be either `Variant(Vec<String>)` or `Variant(Vec<OsString>)` |
| 180 |
//! |
| 181 |
//! And for [`Args`][crate::Args] fields: |
| 182 |
//! - `flatten`: Delegates to the field for more arguments (must implement [`Args`][crate::Args]) |
| 183 |
//! - Only [`next_help_heading`][crate::Command::next_help_heading] can be used with `flatten`. See |
| 184 |
//! [clap-rs/clap#3269](https://github.com/clap-rs/clap/issues/3269) for why |
| 185 |
//! arg attributes are not generally supported. |
| 186 |
//! - **Tip:** Though we do apply a flattened [`Args`][crate::Args]'s Parent Command Attributes, this |
| 187 |
//! makes reuse harder. Generally prefer putting the cmd attributes on the |
| 188 |
//! [`Parser`][crate::Parser] or on the flattened field. |
| 189 |
//! - `subcommand`: Delegates definition of subcommands to the field (must implement |
| 190 |
//! [`Subcommand`][crate::Subcommand]) |
| 191 |
//! - When `Option<T>`, the subcommand becomes optional |
| 192 |
//! |
| 193 |
//! See [Configuring the Parser][_tutorial::chapter_1] and |
| 194 |
//! [Subcommands][_tutorial::chapter_2#subcommands] from the tutorial. |
| 195 |
//! |
| 196 |
//! ### ArgGroup Attributes |
| 197 |
//! |
| 198 |
//! These correspond to the [`ArgGroup`][crate::ArgGroup] which is implicitly created for each |
| 199 |
//! `Args` derive. |
| 200 |
//! |
| 201 |
//! **Raw attributes:** Any [`ArgGroup` method][crate::ArgGroup] can also be used as an attribute, see [Terminology](#terminology) for syntax. |
| 202 |
//! - e.g. `#[group(required = true)]` would translate to `arg_group.required(true)` |
| 203 |
//! |
| 204 |
//! **Magic attributes**: |
| 205 |
//! - `id = <expr>`: [`ArgGroup::id`][crate::ArgGroup::id] |
| 206 |
//! - When not present: struct's name is used |
| 207 |
//! - `skip [= <expr>]`: Ignore this field, filling in with `<expr>` |
| 208 |
//! - Without `<expr>`: fills the field with `Default::default()` |
| 209 |
//! |
| 210 |
//! Note: |
| 211 |
//! - For `struct`s, [`multiple = true`][crate::ArgGroup::multiple] is implied |
| 212 |
//! - `enum` support is tracked at [#2621](https://github.com/clap-rs/clap/issues/2621) |
| 213 |
//! |
| 214 |
//! See [Argument Relations][_tutorial::chapter_3#argument-relations] from the tutorial. |
| 215 |
//! |
| 216 |
//! ### Arg Attributes |
| 217 |
//! |
| 218 |
//! These correspond to a [`Arg`][crate::Arg]. |
| 219 |
//! |
| 220 |
//! **Raw attributes:** Any [`Arg` method][crate::Arg] can also be used as an attribute, see [Terminology](#terminology) for syntax. |
| 221 |
//! - e.g. `#[arg(num_args(..=3))]` would translate to `arg.num_args(..=3)` |
| 222 |
//! |
| 223 |
//! **Magic attributes**: |
| 224 |
//! - `id = <expr>`: [`Arg::id`][crate::Arg::id] |
| 225 |
//! - When not present: field's name is used |
| 226 |
//! - `value_parser [= <expr>]`: [`Arg::value_parser`][crate::Arg::value_parser] |
| 227 |
//! - When not present: will auto-select an implementation based on the field type using |
| 228 |
//! [`value_parser!`][crate::value_parser!] |
| 229 |
//! - `action [= <expr>]`: [`Arg::action`][crate::Arg::action] |
| 230 |
//! - When not present: will auto-select an action based on the field type |
| 231 |
//! - `help = <expr>`: [`Arg::help`][crate::Arg::help] |
| 232 |
//! - When not present: [Doc comment summary](#doc-comments) |
| 233 |
//! - `long_help[ = <expr>]`: [`Arg::long_help`][crate::Arg::long_help] |
| 234 |
//! - When not present: [Doc comment](#doc-comments) if there is a blank line, else nothing |
| 235 |
//! - When present without a value: [Doc comment](#doc-comments) |
| 236 |
//! - `verbatim_doc_comment`: Minimizes pre-processing when converting doc comments to [`help`][crate::Arg::help] / [`long_help`][crate::Arg::long_help] |
| 237 |
//! - `short [= <char>]`: [`Arg::short`][crate::Arg::short] |
| 238 |
//! - When not present: no short set |
| 239 |
//! - Without `<char>`: defaults to first character in the case-converted field name |
| 240 |
//! - `long [= <str>]`: [`Arg::long`][crate::Arg::long] |
| 241 |
//! - When not present: no long set |
| 242 |
//! - Without `<str>`: defaults to the case-converted field name |
| 243 |
//! - `env [= <str>]`: [`Arg::env`][crate::Arg::env] (needs [`env` feature][crate::_features] enabled) |
| 244 |
//! - When not present: no env set |
| 245 |
//! - Without `<str>`: defaults to the case-converted field name |
| 246 |
//! - `from_global`: Read a [`Arg::global`][crate::Arg::global] argument (raw attribute), regardless of what subcommand you are in |
| 247 |
//! - `value_enum`: Parse the value using the [`ValueEnum`][crate::ValueEnum] |
| 248 |
//! - `skip [= <expr>]`: Ignore this field, filling in with `<expr>` |
| 249 |
//! - Without `<expr>`: fills the field with `Default::default()` |
| 250 |
//! - `default_value = <str>`: [`Arg::default_value`][crate::Arg::default_value] and [`Arg::required(false)`][crate::Arg::required] |
| 251 |
//! - `default_value_t [= <expr>]`: [`Arg::default_value`][crate::Arg::default_value] and [`Arg::required(false)`][crate::Arg::required] |
| 252 |
//! - Requires `std::fmt::Display` that roundtrips correctly with the |
| 253 |
//! [`Arg::value_parser`][crate::Arg::value_parser] or `#[arg(value_enum)]` |
| 254 |
//! - Without `<expr>`, relies on `Default::default()` |
| 255 |
//! - `default_values_t = <expr>`: [`Arg::default_values`][crate::Arg::default_values] and [`Arg::required(false)`][crate::Arg::required] |
| 256 |
//! - Requires field arg to be of type `Vec<T>` and `T` to implement `std::fmt::Display` or `#[arg(value_enum)]` |
| 257 |
//! - `<expr>` must implement `IntoIterator<T>` |
| 258 |
//! - `default_value_os_t [= <expr>]`: [`Arg::default_value_os`][crate::Arg::default_value_os] and [`Arg::required(false)`][crate::Arg::required] |
| 259 |
//! - Requires `std::convert::Into<OsString>` or `#[arg(value_enum)]` |
| 260 |
//! - Without `<expr>`, relies on `Default::default()` |
| 261 |
//! - `default_values_os_t = <expr>`: [`Arg::default_values_os`][crate::Arg::default_values_os] and [`Arg::required(false)`][crate::Arg::required] |
| 262 |
//! - Requires field arg to be of type `Vec<T>` and `T` to implement `std::convert::Into<OsString>` or `#[arg(value_enum)]` |
| 263 |
//! - `<expr>` must implement `IntoIterator<T>` |
| 264 |
//! |
| 265 |
//! See [Adding Arguments][_tutorial::chapter_2] and [Validation][_tutorial::chapter_3] from the |
| 266 |
//! tutorial. |
| 267 |
//! |
| 268 |
//! ### ValueEnum Attributes |
| 269 |
//! |
| 270 |
//! - `rename_all = <string_literal>`: Override default field / variant name case conversion for [`PossibleValue::new`][crate::builder::PossibleValue] |
| 271 |
//! - When not present: `"kebab-case"` |
| 272 |
//! - Available values: `"camelCase"`, `"kebab-case"`, `"PascalCase"`, `"SCREAMING_SNAKE_CASE"`, `"snake_case"`, `"lower"`, `"UPPER"`, `"verbatim"` |
| 273 |
//! |
| 274 |
//! See [Enumerated values][_tutorial::chapter_3#enumerated-values] from the tutorial. |
| 275 |
//! |
| 276 |
//! ### Possible Value Attributes |
| 277 |
//! |
| 278 |
//! These correspond to a [`PossibleValue`][crate::builder::PossibleValue]. |
| 279 |
//! |
| 280 |
//! **Raw attributes:** Any [`PossibleValue` method][crate::builder::PossibleValue] can also be used as an attribute, see [Terminology](#terminology) for syntax. |
| 281 |
//! - e.g. `#[value(alias("foo"))]` would translate to `pv.alias("foo")` |
| 282 |
//! |
| 283 |
//! **Magic attributes**: |
| 284 |
//! - `name = <expr>`: [`PossibleValue::new`][crate::builder::PossibleValue::new] |
| 285 |
//! - When not present: case-converted field name is used |
| 286 |
//! - `help = <expr>`: [`PossibleValue::help`][crate::builder::PossibleValue::help] |
| 287 |
//! - When not present: [Doc comment summary](#doc-comments) |
| 288 |
//! - `skip`: Ignore this variant |
| 289 |
//! |
| 290 |
//! ## Arg Types |
| 291 |
//! |
| 292 |
//! `clap` assumes some intent based on the type used: |
| 293 |
//! |
| 294 |
//! | Type | Effect | Implies | Notes | |
| 295 |
//! |-----------------------|------------------------------------------------------|-------------------------------------------------------------|-------| |
| 296 |
//! | `()` | user-defined | `.action(ArgAction::Set).required(false)` | | |
| 297 |
//! | `bool` | flag | `.action(ArgAction::SetTrue)` | | |
| 298 |
//! | `Option<T>` | optional argument | `.action(ArgAction::Set).required(false)` | | |
| 299 |
//! | `Option<Option<T>>` | optional value for optional argument | `.action(ArgAction::Set).required(false).num_args(0..=1)` | | |
| 300 |
//! | `T` | required argument | `.action(ArgAction::Set).required(!has_default)` | | |
| 301 |
//! | `Vec<T>` | `0..` occurrences of argument | `.action(ArgAction::Append).required(false)` | | |
| 302 |
//! | `Option<Vec<T>>` | `0..` occurrences of argument | `.action(ArgAction::Append).required(false)` | | |
| 303 |
//! | `Vec<Vec<T>>` | `0..` occurrences of argument, grouped by occurrence | `.action(ArgAction::Append).required(false)` | requires `unstable-v5` | |
| 304 |
//! | `Option<Vec<Vec<T>>>` | `0..` occurrences of argument, grouped by occurrence | `.action(ArgAction::Append).required(false)` | requires `unstable-v5` | |
| 305 |
//! |
| 306 |
//! In addition, [`.value_parser(value_parser!(T))`][crate::value_parser!] is called for each |
| 307 |
//! field. |
| 308 |
//! |
| 309 |
//! Notes: |
| 310 |
//! - For custom type behavior, you can override the implied attributes/settings and/or set additional ones |
| 311 |
//! - To force any inferred type (like `Vec<T>`) to be treated as `T`, you can refer to the type |
| 312 |
//! by another means, like using `std::vec::Vec` instead of `Vec`. For improving this, see |
| 313 |
//! [#4626](https://github.com/clap-rs/clap/issues/4626). |
| 314 |
//! - `Option<Vec<T>>` and `Option<Vec<Vec<T>>` will be `None` instead of `vec![]` if no arguments are provided. |
| 315 |
//! - This gives the user some flexibility in designing their argument, like with `num_args(0..)` |
| 316 |
//! - `Vec<Vec<T>>` will need [`Arg::num_args`][crate::Arg::num_args] set to be meaningful |
| 317 |
//! |
| 318 |
//! ## Doc Comments |
| 319 |
//! |
| 320 |
//! In clap, help messages for the whole binary can be specified |
| 321 |
//! via [`Command::about`][crate::Command::about] and [`Command::long_about`][crate::Command::long_about] while help messages |
| 322 |
//! for individual arguments can be specified via [`Arg::help`][crate::Arg::help] and [`Arg::long_help`][crate::Arg::long_help]. |
| 323 |
//! |
| 324 |
//! `long_*` variants are used when user calls the program with |
| 325 |
//! `--help` and "short" variants are used with `-h` flag. |
| 326 |
//! |
| 327 |
//! ```rust |
| 328 |
//! # use clap::Parser; |
| 329 |
//! |
| 330 |
//! #[derive(Parser)] |
| 331 |
//! #[command(about = "I am a program and I work, just pass `-h`", long_about = None)] |
| 332 |
//! struct Foo { |
| 333 |
//! #[arg(short, help = "Pass `-h` and you'll see me!")] |
| 334 |
//! bar: String, |
| 335 |
//! } |
| 336 |
//! ``` |
| 337 |
//! |
| 338 |
//! For convenience, doc comments can be used instead of raw methods |
| 339 |
//! (this example works exactly like the one above): |
| 340 |
//! |
| 341 |
//! ```rust |
| 342 |
//! # use clap::Parser; |
| 343 |
//! |
| 344 |
//! #[derive(Parser)] |
| 345 |
//! /// I am a program and I work, just pass `-h` |
| 346 |
//! struct Foo { |
| 347 |
//! /// Pass `-h` and you'll see me! |
| 348 |
//! bar: String, |
| 349 |
//! } |
| 350 |
//! ``` |
| 351 |
//! |
| 352 |
//! **NOTE:** Attributes have priority over doc comments! |
| 353 |
//! |
| 354 |
//! **Top level doc comments always generate `Command::about/long_about` calls!** |
| 355 |
//! If you really want to use the `Command::about/long_about` methods (you likely don't), |
| 356 |
//! use the `about` / `long_about` attributes to override the calls generated from |
| 357 |
//! the doc comment. To clear `long_about`, you can use |
| 358 |
//! `#[command(long_about = None)]`. |
| 359 |
//! |
| 360 |
//! ### Pre-processing |
| 361 |
//! |
| 362 |
//! ```rust |
| 363 |
//! # use clap::Parser; |
| 364 |
//! #[derive(Parser)] |
| 365 |
//! /// Hi there, I'm Robo! |
| 366 |
//! /// |
| 367 |
//! /// I like beeping, stumbling, eating your electricity, |
| 368 |
//! /// and making records of you singing in a shower. |
| 369 |
//! /// Pay up, or I'll upload it to youtube! |
| 370 |
//! struct Robo { |
| 371 |
//! /// Call my brother SkyNet. |
| 372 |
//! /// |
| 373 |
//! /// I am artificial superintelligence. I won't rest |
| 374 |
//! /// until I'll have destroyed humanity. Enjoy your |
| 375 |
//! /// pathetic existence, you mere mortals. |
| 376 |
//! #[arg(long, action)] |
| 377 |
//! kill_all_humans: bool, |
| 378 |
//! } |
| 379 |
//! ``` |
| 380 |
//! |
| 381 |
//! A doc comment consists of three parts: |
| 382 |
//! - Short summary |
| 383 |
//! - A blank line (whitespace only) |
| 384 |
//! - Detailed description, all the rest |
| 385 |
//! |
| 386 |
//! The summary corresponds with `Command::about` / `Arg::help`. When a blank line is |
| 387 |
//! present, the whole doc comment will be passed to `Command::long_about` / |
| 388 |
//! `Arg::long_help`. Or in other words, a doc may result in just a `Command::about` / |
| 389 |
//! `Arg::help` or `Command::about` / `Arg::help` and `Command::long_about` / |
| 390 |
//! `Arg::long_help` |
| 391 |
//! |
| 392 |
//! In addition, when `verbatim_doc_comment` is not present, `clap` applies some preprocessing, including: |
| 393 |
//! |
| 394 |
//! - Strip leading and trailing whitespace from every line, if present. |
| 395 |
//! |
| 396 |
//! - Strip leading and trailing blank lines, if present. |
| 397 |
//! |
| 398 |
//! - Interpret each group of non-empty lines as a word-wrapped paragraph. |
| 399 |
//! |
| 400 |
//! We replace newlines within paragraphs with spaces to allow the output |
| 401 |
//! to be re-wrapped to the terminal width. |
| 402 |
//! |
| 403 |
//! - Strip any excess blank lines so that there is exactly one per paragraph break. |
| 404 |
//! |
| 405 |
//! - If the first paragraph ends in exactly one period, |
| 406 |
//! remove the trailing period (i.e. strip trailing periods but not trailing ellipses). |
| 407 |
//! |
| 408 |
//! Sometimes you don't want this preprocessing to apply, for example the comment contains |
| 409 |
//! some ASCII art or markdown tables, you would need to preserve LFs along with |
| 410 |
//! blank lines and the leading/trailing whitespace. When you pass use the |
| 411 |
//! `verbatim_doc_comment` magic attribute, you preserve |
| 412 |
//! them. |
| 413 |
//! |
| 414 |
//! **Note:** Keep in mind that `verbatim_doc_comment` will *still* |
| 415 |
//! - Remove one leading space from each line, even if this attribute is present, |
| 416 |
//! to allow for a space between `///` and the content. |
| 417 |
//! - Remove leading and trailing blank lines |
| 418 |
//! |
| 419 |
//! ## Mixing Builder and Derive APIs |
| 420 |
//! |
| 421 |
//! The builder and derive APIs do not live in isolation. They can work together, which is |
| 422 |
//! especially helpful if some arguments can be specified at compile-time while others must be |
| 423 |
//! specified at runtime. |
| 424 |
//! |
| 425 |
//! ### Using derived arguments in a builder application |
| 426 |
//! |
| 427 |
//! When using the derive API, you can `#[command(flatten)]` a struct deriving `Args` into a struct |
| 428 |
//! deriving `Args` or `Parser`. This example shows how you can augment a `Command` instance |
| 429 |
//! created using the builder API with `Args` created using the derive API. |
| 430 |
//! |
| 431 |
//! It uses the [`Args::augment_args`][crate::Args::augment_args] method to add the arguments to |
| 432 |
//! the `Command` instance. |
| 433 |
//! |
| 434 |
//! Crates such as [clap-verbosity-flag](https://github.com/rust-cli/clap-verbosity-flag) provide |
| 435 |
//! structs that implement `Args`. Without the technique shown in this example, it would not be |
| 436 |
//! possible to use such crates with the builder API. |
| 437 |
//! |
| 438 |
//! For example: |
| 439 |
//! ```rust |
| 440 |
#![doc = include_str!("../../examples/derive_ref/augment_args.rs")] |
| 441 |
//! ``` |
| 442 |
//! |
| 443 |
//! ### Using derived subcommands in a builder application |
| 444 |
//! |
| 445 |
//! When using the derive API, you can use `#[command(subcommand)]` inside the struct to add |
| 446 |
//! subcommands. The type of the field is usually an enum that derived `Parser`. However, you can |
| 447 |
//! also add the subcommands in that enum to a `Command` instance created with the builder API. |
| 448 |
//! |
| 449 |
//! It uses the [`Subcommand::augment_subcommands`][crate::Subcommand::augment_subcommands] method |
| 450 |
//! to add the subcommands to the `Command` instance. |
| 451 |
//! |
| 452 |
//! For example: |
| 453 |
//! ```rust |
| 454 |
#![doc = include_str!("../../examples/derive_ref/augment_subcommands.rs")] |
| 455 |
//! ``` |
| 456 |
//! |
| 457 |
//! ### Adding hand-implemented subcommands to a derived application |
| 458 |
//! |
| 459 |
//! When using the derive API, you can use `#[command(subcommand)]` inside the struct to add |
| 460 |
//! subcommands. The type of the field is usually an enum that derived `Parser`. However, you can |
| 461 |
//! also implement the `Subcommand` trait manually on this enum (or any other type) and it can |
| 462 |
//! still be used inside the struct created with the derive API. The implementation of the |
| 463 |
//! `Subcommand` trait will use the builder API to add the subcommands to the `Command` instance |
| 464 |
//! created behind the scenes for you by the derive API. |
| 465 |
//! |
| 466 |
//! Notice how in the previous example we used |
| 467 |
//! [`augment_subcommands`][crate::Subcommand::augment_subcommands] on an enum that derived |
| 468 |
//! `Parser`, whereas now we implement |
| 469 |
//! [`augment_subcommands`][crate::Subcommand::augment_subcommands] ourselves, but the derive API |
| 470 |
//! calls it automatically since we used the `#[command(subcommand)]` attribute. |
| 471 |
//! |
| 472 |
//! For example: |
| 473 |
//! ```rust |
| 474 |
#![doc = include_str!("../../examples/derive_ref/hand_subcommand.rs")] |
| 475 |
//! ``` |
| 476 |
//! |
| 477 |
//! ### Flattening hand-implemented args into a derived application |
| 478 |
//! |
| 479 |
//! When using the derive API, you can use `#[command(flatten)]` inside the struct to add arguments as |
| 480 |
//! if they were added directly to the containing struct. The type of the field is usually an |
| 481 |
//! struct that derived `Args`. However, you can also implement the `Args` trait manually on this |
| 482 |
//! struct (or any other type) and it can still be used inside the struct created with the derive |
| 483 |
//! API. The implementation of the `Args` trait will use the builder API to add the arguments to |
| 484 |
//! the `Command` instance created behind the scenes for you by the derive API. |
| 485 |
//! |
| 486 |
//! Notice how in the previous example we used [`augment_args`][crate::Args::augment_args] on the |
| 487 |
//! struct that derived `Parser`, whereas now we implement |
| 488 |
//! [`augment_args`][crate::Args::augment_args] ourselves, but the derive API calls it |
| 489 |
//! automatically since we used the `#[command(flatten)]` attribute. |
| 490 |
//! |
| 491 |
//! For example: |
| 492 |
//! ```rust |
| 493 |
#![doc = include_str!("../../examples/derive_ref/flatten_hand_args.rs")] |
| 494 |
//! ``` |
| 495 |
//! |
| 496 |
//! ## Tips |
| 497 |
//! |
| 498 |
//! - To get access to a [`Command`][crate::Command] call |
| 499 |
//! [`CommandFactory::command`][crate::CommandFactory::command] (implemented when deriving |
| 500 |
//! [`Parser`][crate::Parser]) |
| 501 |
//! - Proactively check for bad [`Command`][crate::Command] configurations by calling |
| 502 |
//! [`Command::debug_assert`][crate::Command::debug_assert] in a test |
| 503 |
//! ([example][_tutorial#testing]) |
| 504 |
//! - Always remember to [document](#doc-comments) args and commands with `#![deny(missing_docs)]` |
| 505 |
|
| 506 |
// Point people here that search for attributes that don't exist in the derive (a subset of magic |
| 507 |
// attributes) |
| 508 |
#![doc(alias = "skip")] |
| 509 |
#![doc(alias = "verbatim_doc_comment")] |
| 510 |
#![doc(alias = "flatten")] |
| 511 |
#![doc(alias = "external_subcommand")] |
| 512 |
#![doc(alias = "subcommand")] |
| 513 |
#![doc(alias = "rename_all")] |
| 514 |
#![doc(alias = "rename_all_env")] |
| 515 |
#![doc(alias = "default_value_t")] |
| 516 |
#![doc(alias = "default_values_t")] |
| 517 |
#![doc(alias = "default_value_os_t")] |
| 518 |
#![doc(alias = "default_values_os_t")] |
| 519 |
|
| 520 |
pub mod _tutorial; |
| 521 |
#[doc(inline)] |
| 522 |
pub use crate::_cookbook; |