Skip to content

Commit bd01bdc

Browse files
authored
Merge pull request #6012 from epage/sub
docs(derive): Specify arg_required_else_help is set for users
2 parents e2188d9 + ed0c63d commit bd01bdc

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

examples/tutorial_builder/03_04_subcommands.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ $ 03_04_subcommands add bob
2929

3030
```
3131

32-
Because we set [`Command::arg_required_else_help`][crate::Command::arg_required_else_help]:
32+
We set
33+
[`Command::arg_required_else_help`][crate::Command::arg_required_else_help] to
34+
show the help, rather than an error, when the
35+
[required subcommand][crate::Command::subcommand_required] is missing:
3336
```console
3437
$ 03_04_subcommands
3538
? failed

examples/tutorial_derive/03_04_subcommands.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ $ 03_04_subcommands_derive add bob
2929

3030
```
3131

32-
When specifying commands with `command: Commands`, they are required.
33-
Alternatively, you could do `command: Option<Commands>` to make it optional.
32+
When specifying commands with `command: Commands`, they are [required][crate::Command::subcommand_required].
33+
By default, a missing subcommand will [show help rather than error][crate::Command::arg_required_else_help].
3434
```console
3535
$ 03_04_subcommands_derive
3636
? failed
@@ -47,6 +47,7 @@ Options:
4747
-V, --version Print version
4848

4949
```
50+
To make a subcommand optional, wrap it in an `Option` (e.g. `command: Option<Commands>`).
5051

5152
Since we specified [`#[command(propagate_version = true)]`][crate::Command::propagate_version],
5253
the `--version` flag is available in all subcommands:

src/_derive/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! 3. [Arg Attributes](#arg-attributes)
99
//! 4. [ValueEnum Attributes](#valueenum-attributes)
1010
//! 5. [Possible Value Attributes](#possible-value-attributes)
11-
//! 3. [Arg Types](#arg-types)
11+
//! 3. [Field Types](#field-types)
1212
//! 4. [Doc Comments](#doc-comments)
1313
//! 5. [Mixing Builder and Derive APIs](#mixing-builder-and-derive-apis)
1414
//! 6. [Tips](#tips)
@@ -294,9 +294,18 @@
294294
//! - When not present: [Doc comment summary](#doc-comments)
295295
//! - `skip`: Ignore this variant
296296
//!
297-
//! ## Arg Types
297+
//! ## Field Types
298298
//!
299-
//! `clap` assumes some intent based on the type used:
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
300309
//!
301310
//! | Type | Effect | Implies | Notes |
302311
//! |-----------------------|------------------------------------------------------|-------------------------------------------------------------|-------|

0 commit comments

Comments
 (0)