• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //!
10 //! ### Positionals
11 //!
12 //! You can have users specify values by their position on the command-line:
13 //!
14 //! ```rust
15 #![doc = include_str!("../../examples/tutorial_builder/03_03_positional.rs")]
16 //! ```
17 #![doc = include_str!("../../examples/tutorial_builder/03_03_positional.md")]
18 //!
19 //! Note that the default [`ArgAction`][crate::ArgAction] is [`Set`][crate::ArgAction::Set].  To
20 //! accept multiple values, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append]:
21 //! ```rust
22 #![doc = include_str!("../../examples/tutorial_builder/03_03_positional_mult.rs")]
23 //! ```
24 #![doc = include_str!("../../examples/tutorial_builder/03_03_positional_mult.md")]
25 //!
26 //! ### Options
27 //!
28 //! You can name your arguments with a flag:
29 //! - Order doesn't matter
30 //! - They can be optional
31 //! - Intent is clearer
32 //!
33 //! ```rust
34 #![doc = include_str!("../../examples/tutorial_builder/03_02_option.rs")]
35 //! ```
36 #![doc = include_str!("../../examples/tutorial_builder/03_02_option.md")]
37 //!
38 //! Note that the default [`ArgAction`][crate::ArgAction] is [`Set`][crate::ArgAction::Set].  To
39 //! accept multiple occurrences, override the [action][Arg::action] with [`Append`][crate::ArgAction::Append]:
40 //! ```rust
41 #![doc = include_str!("../../examples/tutorial_builder/03_02_option_mult.rs")]
42 //! ```
43 #![doc = include_str!("../../examples/tutorial_builder/03_02_option_mult.md")]
44 //!
45 //! ### Flags
46 //!
47 //! Flags can also be switches that can be on/off:
48 //!
49 //! ```rust
50 #![doc = include_str!("../../examples/tutorial_builder/03_01_flag_bool.rs")]
51 //! ```
52 #![doc = include_str!("../../examples/tutorial_builder/03_01_flag_bool.md")]
53 //!
54 //! To accept multiple flags, use [`Count`][crate::ArgAction::Count]:
55 //!
56 //! ```rust
57 #![doc = include_str!("../../examples/tutorial_builder/03_01_flag_count.rs")]
58 //! ```
59 #![doc = include_str!("../../examples/tutorial_builder/03_01_flag_count.md")]
60 //!
61 //! ### Subcommands
62 //!
63 //! Subcommands are defined as [`Command`][crate::Command]s that get added via
64 //! [`Command::subcommand`][crate::Command::subcommand]. Each instance of a Subcommand can have its
65 //! own version, author(s), Args, and even its own subcommands.
66 //!
67 //! ```rust
68 #![doc = include_str!("../../examples/tutorial_builder/03_04_subcommands.rs")]
69 //! ```
70 #![doc = include_str!("../../examples/tutorial_builder/03_04_subcommands.md")]
71 //!
72 //! ### Defaults
73 //!
74 //! We've previously showed that arguments can be [`required`][crate::Arg::required] or optional.
75 //! When optional, you work with a `Option` and can `unwrap_or`.  Alternatively, you can set
76 //! [`Arg::default_value`][crate::Arg::default_value].
77 //!
78 //! ```rust
79 #![doc = include_str!("../../examples/tutorial_builder/03_05_default_values.rs")]
80 //! ```
81 #![doc = include_str!("../../examples/tutorial_builder/03_05_default_values.md")]
82 #![allow(unused_imports)]
83 use crate::builder::*;
84 
85 pub use super::chapter_1 as previous;
86 pub use super::chapter_3 as next;
87 pub use crate::_tutorial as table_of_contents;
88