Debian Codegraph @

third_party/rust/clap/src/_derive/_tutorial/chapter_2.rs

raw
1 //! ## Adding Arguments
2 //!
3 //! 1. [Positionals](#positionals)
4 //! 2. [Options](#options)
5 //! 3. [Flags](#flags)
6 //! 4. [Subcommands](#subcommands)
7 //! 5. [Defaults](#defaults)
8 //!
9 //! Arguments are inferred from the fields of your struct.
10 //!
11 //! ### Positionals
12 //!
13 //! You can have users specify values by their position on the command-line:
14 //!
15 //! ```rust
16 #![doc = include_str!("../../../examples/tutorial_derive/03_03_positional.rs")]
17 //! ```
18 #![doc = include_str!("../../../examples/tutorial_derive/03_03_positional.md")]
19 //!
20 //! Note that the [default `ArgAction` is `Set`][super#arg-types]. To
21 //! accept multiple values, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append] via `Vec`:
22 //! ```rust
23 #![doc = include_str!("../../../examples/tutorial_derive/03_03_positional_mult.rs")]
24 //! ```
25 #![doc = include_str!("../../../examples/tutorial_derive/03_03_positional_mult.md")]
26 //!
27 //! ### Options
28 //!
29 //! You can name your arguments with a flag:
30 //! - Order doesn't matter
31 //! - They can be optional
32 //! - Intent is clearer
33 //!
34 //! To specify the flags for an argument, you can use [`#[arg(short = 'n')]`][Arg::short] and/or
35 //! [`#[arg(long = "name")]`][Arg::long] attributes on a field. When no value is given (e.g.
36 //! `#[arg(short)]`), the flag is inferred from the field's name.
37 //!
38 //! ```rust
39 #![doc = include_str!("../../../examples/tutorial_derive/03_02_option.rs")]
40 //! ```
41 #![doc = include_str!("../../../examples/tutorial_derive/03_02_option.md")]
42 //!
43 //! Note that the [default `ArgAction` is `Set`][super#arg-types]. To
44 //! accept multiple occurrences, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append] via `Vec`:
45 //! ```rust
46 #![doc = include_str!("../../../examples/tutorial_derive/03_02_option_mult.rs")]
47 //! ```
48 #![doc = include_str!("../../../examples/tutorial_derive/03_02_option_mult.md")]
49 //!
50 //! ### Flags
51 //!
52 //! Flags can also be switches that can be on/off:
53 //!
54 //! ```rust
55 #![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_bool.rs")]
56 //! ```
57 #![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_bool.md")]
58 //!
59 //! Note that the [default `ArgAction` for a `bool` field is
60 //! `SetTrue`][super#arg-types]. To accept multiple flags, override the [action][Arg::action] with
61 //! [`Count`][crate::ArgAction::Count]:
62 //!
63 //! ```rust
64 #![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_count.rs")]
65 //! ```
66 #![doc = include_str!("../../../examples/tutorial_derive/03_01_flag_count.md")]
67 //!
68 //! This also shows that any[`Arg`][crate::Args] method may be used as an attribute.
69 //!
70 //! ### Subcommands
71 //!
72 //! Subcommands are derived with `#[derive(Subcommand)]` and be added via
73 //! [`#[command(subcommand)]` attribute][super#command-attributes] on the field using that type.
74 //! Each instance of a [Subcommand][crate::Subcommand] can have its own version, author(s), Args,
75 //! and even its own subcommands.
76 //!
77 //! ```rust
78 #![doc = include_str!("../../../examples/tutorial_derive/03_04_subcommands.rs")]
79 //! ```
80 //! We used a struct-variant to define the `add` subcommand.
81 //! Alternatively, you can use a struct for your subcommand's arguments:
82 //! ```rust
83 #![doc = include_str!("../../../examples/tutorial_derive/03_04_subcommands_alt.rs")]
84 //! ```
85 //!
86 #![doc = include_str!("../../../examples/tutorial_derive/03_04_subcommands.md")]
87 //!
88 //! ### Defaults
89 //!
90 //! We've previously showed that arguments can be [`required`][crate::Arg::required] or optional.
91 //! When optional, you work with a `Option` and can `unwrap_or`. Alternatively, you can
92 //! set [`#[arg(default_value_t)]`][super#arg-attributes].
93 //!
94 //! ```rust
95 #![doc = include_str!("../../../examples/tutorial_derive/03_05_default_values.rs")]
96 //! ```
97 #![doc = include_str!("../../../examples/tutorial_derive/03_05_default_values.md")]
98 #![allow(unused_imports)]
99 use crate::builder::*;
100
101 pub use super::chapter_1 as previous;
102 pub use super::chapter_3 as next;
103 pub use crate::_derive::_tutorial as table_of_contents;