|
61 | 61 | //! 1. [Positionals](#positionals)
|
62 | 62 | //! 2. [Options](#options)
|
63 | 63 | //! 3. [Flags](#flags)
|
64 |
| -//! 4. [Subcommands](#subcommands) |
| 64 | +//! 4. [Optional](#optional) |
65 | 65 | //! 5. [Defaults](#defaults)
|
| 66 | +//! 6. [Subcommands](#subcommands) |
66 | 67 | //!
|
67 | 68 | //! Arguments are inferred from the fields of your struct.
|
68 | 69 | //!
|
|
85 | 86 | //! ### Options
|
86 | 87 | //!
|
87 | 88 | //! You can name your arguments with a flag:
|
| 89 | +//! - Intent of the value is clearer |
88 | 90 | //! - Order doesn't matter
|
89 |
| -//! - They can be optional |
90 |
| -//! - Intent is clearer |
91 | 91 | //!
|
92 | 92 | //! To specify the flags for an argument, you can use [`#[arg(short = 'n')]`][Arg::short] and/or
|
93 | 93 | //! [`#[arg(long = "name")]`][Arg::long] attributes on a field. When no value is given (e.g.
|
|
125 | 125 | //!
|
126 | 126 | //! This also shows that any[`Arg`][crate::Args] method may be used as an attribute.
|
127 | 127 | //!
|
| 128 | +//! ### Optional |
| 129 | +//! |
| 130 | +//! By default, arguments are assumed to be [`required`][crate::Arg::required]. |
| 131 | +//! To make an argument optional, wrap the field's type in `Option`: |
| 132 | +//! ```rust |
| 133 | +#![doc = include_str!("../../examples/tutorial_derive/03_06_optional.rs")] |
| 134 | +//! ``` |
| 135 | +#![doc = include_str!("../../examples/tutorial_derive/03_06_optional.md")] |
| 136 | +//! |
| 137 | +//! ### Defaults |
| 138 | +//! |
| 139 | +//! We've previously showed that arguments can be [`required`][crate::Arg::required] or optional. |
| 140 | +//! When optional, you work with a `Option` and can `unwrap_or`. Alternatively, you can |
| 141 | +//! set [`#[arg(default_value_t)]`][super#arg-attributes]. |
| 142 | +//! |
| 143 | +//! ```rust |
| 144 | +#![doc = include_str!("../../examples/tutorial_derive/03_05_default_values.rs")] |
| 145 | +//! ``` |
| 146 | +#![doc = include_str!("../../examples/tutorial_derive/03_05_default_values.md")] |
| 147 | +//! |
128 | 148 | //! ### Subcommands
|
129 | 149 | //!
|
130 | 150 | //! Subcommands are derived with `#[derive(Subcommand)]` and be added via
|
|
143 | 163 | //!
|
144 | 164 | #![doc = include_str!("../../examples/tutorial_derive/03_04_subcommands.md")]
|
145 | 165 | //!
|
146 |
| -//! ### Defaults |
147 |
| -//! |
148 |
| -//! We've previously showed that arguments can be [`required`][crate::Arg::required] or optional. |
149 |
| -//! When optional, you work with a `Option` and can `unwrap_or`. Alternatively, you can |
150 |
| -//! set [`#[arg(default_value_t)]`][super#arg-attributes]. |
151 |
| -//! |
152 |
| -//! ```rust |
153 |
| -#![doc = include_str!("../../examples/tutorial_derive/03_05_default_values.rs")] |
154 |
| -//! ``` |
155 |
| -#![doc = include_str!("../../examples/tutorial_derive/03_05_default_values.md")] |
156 |
| -//! |
157 | 166 | //! ## Validation
|
158 | 167 | //!
|
159 | 168 | //! 1. [Enumerated values](#enumerated-values)
|
|
0 commit comments