File tree Expand file tree Collapse file tree 3 files changed +19
-6
lines changed Expand file tree Collapse file tree 3 files changed +19
-6
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,10 @@ $ 03_04_subcommands add bob
29
29
30
30
```
31
31
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:
33
36
``` console
34
37
$ 03_04_subcommands
35
38
? failed
Original file line number Diff line number Diff line change @@ -29,8 +29,8 @@ $ 03_04_subcommands_derive add bob
29
29
30
30
```
31
31
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 ] .
34
34
``` console
35
35
$ 03_04_subcommands_derive
36
36
? failed
@@ -47,6 +47,7 @@ Options:
47
47
-V, --version Print version
48
48
49
49
```
50
+ To make a subcommand optional, wrap it in an ` Option ` (e.g. ` command: Option<Commands> ` ).
50
51
51
52
Since we specified [ ` #[command(propagate_version = true)] ` ] [ crate::Command::propagate_version ] ,
52
53
the ` --version ` flag is available in all subcommands:
Original file line number Diff line number Diff line change 8
8
//! 3. [Arg Attributes](#arg-attributes)
9
9
//! 4. [ValueEnum Attributes](#valueenum-attributes)
10
10
//! 5. [Possible Value Attributes](#possible-value-attributes)
11
- //! 3. [Arg Types](#arg -types)
11
+ //! 3. [Field Types](#field -types)
12
12
//! 4. [Doc Comments](#doc-comments)
13
13
//! 5. [Mixing Builder and Derive APIs](#mixing-builder-and-derive-apis)
14
14
//! 6. [Tips](#tips)
294
294
//! - When not present: [Doc comment summary](#doc-comments)
295
295
//! - `skip`: Ignore this variant
296
296
//!
297
- //! ## Arg Types
297
+ //! ## Field Types
298
298
//!
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
300
309
//!
301
310
//! | Type | Effect | Implies | Notes |
302
311
//! |-----------------------|------------------------------------------------------|-------------------------------------------------------------|-------|
You can’t perform that action at this time.
0 commit comments