Skip to content

Commit c9eef44

Browse files
committed
fix: Make arg!(--flag <value>) optional by default
This was ported over from the usage parser which modeled after docopt. We just never got around to implementing the rest of the syntax. However, when considering this as a standalone feature, an `arg!(--flag <value>)`, outside of other context, should be optional. This is how the help would display it. Fixes clap-rs#4206
1 parent 75a73f3 commit c9eef44

31 files changed

+177
-301
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Subtle changes (i.e. compiler won't catch):
183183
- By default, an `Arg`s default action is `ArgAction::Set`, rather than `ArgAction::IncOccurrence` to reduce confusing magic through consistency (#2687, #4032, see also #3977)
184184
- `mut_arg` can no longer be used to customize help and version arguments, instead disable them (`Command::disable_help_flag`, `Command::disable_version_flag`) and provide your own (#4056)
185185
- Removed lifetimes from `Command`, `Arg`, `ArgGroup`, and `PossibleValue`
186+
- `arg!(--flag <value>)` is now optional, instead of required. Add `.required(true)` at the end to restore the original behavior (#4206)
186187
- *(parser)* Always fill in `""` argument for external subcommands to make it easier to distinguish them from built-in commands (#3263)
187188
- *(parser)* Short flags now have higher precedence than hyphen values with `Arg::allow_hyphen_values`, to be consistent with `Command::allow_hyphen_values` (#4187)
188189
- *(parser)* `Arg::value_terminator` must be its own argument on the CLI rather than being in a delimited list (#4025)

clap_bench/benches/02_simple.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ macro_rules! create_app {
88
.about("tests clap library")
99
.author("Kevin K. <[email protected]>")
1010
.arg(arg!(-f --flag "tests flags"))
11-
.arg(arg!(-o --option <opt> "tests options").required(false))
11+
.arg(arg!(-o --option <opt> "tests options"))
1212
.arg(arg!([positional] "tests positional"))
1313
}};
1414
}
@@ -34,14 +34,14 @@ pub fn build_with_flag_ref(c: &mut Criterion) {
3434

3535
pub fn build_with_opt(c: &mut Criterion) {
3636
c.bench_function("build_with_opt", |b| {
37-
b.iter(|| Command::new("claptests").arg(arg!(-s --some <FILE> "something")))
37+
b.iter(|| Command::new("claptests").arg(arg!(-s --some <FILE> "something").required(true)))
3838
});
3939
}
4040

4141
pub fn build_with_opt_ref(c: &mut Criterion) {
4242
c.bench_function("build_with_opt_ref", |b| {
4343
b.iter(|| {
44-
let arg = arg!(-s --some <FILE> "something");
44+
let arg = arg!(-s --some <FILE> "something").required(true);
4545
Command::new("claptests").arg(&arg)
4646
})
4747
});

clap_bench/benches/03_complex.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,34 @@ macro_rules! create_app {
1010
.version("0.1")
1111
.about("tests clap library")
1212
.author("Kevin K. <[email protected]>")
13-
.arg(arg!(-o --option <opt> ... "tests options").required(false))
13+
.arg(arg!(-o --option <opt> ... "tests options"))
1414
.arg(arg!([positional] "tests positionals"))
1515
.arg(arg!(-f --flag ... "tests flags").global(true))
1616
.args([
1717
arg!(flag2: -F "tests flags with exclusions")
1818
.conflicts_with("flag")
1919
.requires("option2"),
2020
arg!(option2: --"long-option-2" <option2> "tests long options with exclusions")
21-
.required(false)
2221
.conflicts_with("option")
2322
.requires("positional2"),
2423
arg!([positional2] "tests positionals with exclusions"),
2524
arg!(-O --Option <option3> "tests options with specific value sets")
26-
.required(false)
2725
.value_parser(OPT3_VALS),
2826
arg!([positional3] ... "tests positionals with specific values")
2927
.value_parser(POS3_VALS),
30-
arg!(--multvals <s> "Tests multiple values not mult occs").required(false).value_names(["one", "two"]),
28+
arg!(--multvals <s> "Tests multiple values not mult occs").value_names(["one", "two"]),
3129
arg!(
3230
--multvalsmo <s> "Tests multiple values, not mult occs"
3331
).required(false).value_names(["one", "two"]),
34-
arg!(--minvals2 <minvals> ... "Tests 2 min vals").num_args(2..).required(false),
35-
arg!(--maxvals3 <maxvals> ... "Tests 3 max vals").num_args(1..=3).required(false),
32+
arg!(--minvals2 <minvals> ... "Tests 2 min vals").num_args(2..),
33+
arg!(--maxvals3 <maxvals> ... "Tests 3 max vals").num_args(1..=3),
3634
])
3735
.subcommand(
3836
Command::new("subcmd")
3937
.about("tests subcommands")
4038
.version("0.1")
4139
.author("Kevin K. <[email protected]>")
42-
.arg(arg!(-o --option <scoption> ... "tests options").required(false))
40+
.arg(arg!(-o --option <scoption> ... "tests options"))
4341
.arg(arg!([scpositional] "tests positionals"))
4442
)
4543
}};

clap_mangen/examples/man.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ And a few newlines.",
1919
.arg(
2020
arg!(-c --config <FILE> "Sets a custom config file")
2121
.long_help("Some more text about how to set a custom config file")
22-
.required(false)
2322
.default_value("config.toml")
2423
.env("CONFIG_FILE"),
2524
)

examples/cargo-example.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ fn main() {
55
.subcommand(
66
clap::command!("example").arg(
77
clap::arg!(--"manifest-path" <PATH>)
8-
.required(false)
98
.value_parser(clap::value_parser!(std::path::PathBuf)),
109
),
1110
);

examples/escaped-positional.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ use clap::{arg, command, value_parser, ArgAction};
33
fn main() {
44
let matches = command!() // requires `cargo` feature
55
.arg(arg!(eff: -f).action(ArgAction::SetTrue))
6-
.arg(
7-
arg!(pea: -p <PEAR>)
8-
.required(false)
9-
.value_parser(value_parser!(String)),
10-
)
6+
.arg(arg!(pea: -p <PEAR>).value_parser(value_parser!(String)))
117
.arg(
128
// Indicates that `slop` is only accessible after `--`.
139
arg!(slop: [SLOP])

examples/find.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
$ find --help
55
A simple to use, efficient, and full-featured Command Line Argument Parser
66

7-
Usage: find[EXE] [OPTIONS] --name <NAME>
7+
Usage: find[EXE] [OPTIONS]
88

99
Options:
1010
-h, --help Print help information

examples/git.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn cli() -> Command {
4545
}
4646

4747
fn push_args() -> Vec<clap::Arg> {
48-
vec![arg!(-m --message <MESSAGE>).required(false)]
48+
vec![arg!(-m --message <MESSAGE>)]
4949
}
5050

5151
fn main() {

examples/tutorial_builder/02_app_settings.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use clap::{arg, command, ArgAction};
33
fn main() {
44
let matches = command!() // requires `cargo` feature
55
.next_line_help(true)
6-
.arg(arg!(--two <VALUE>).action(ArgAction::Set))
7-
.arg(arg!(--one <VALUE>).action(ArgAction::Set))
6+
.arg(arg!(--two <VALUE>).required(true).action(ArgAction::Set))
7+
.arg(arg!(--one <VALUE>).required(true).action(ArgAction::Set))
88
.get_matches();
99

1010
println!(

examples/tutorial_builder/02_apps.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ fn main() {
55
.version("1.0")
66
.author("Kevin K. <[email protected]>")
77
.about("Does awesome things")
8-
.arg(arg!(--two <VALUE>))
9-
.arg(arg!(--one <VALUE>))
8+
.arg(arg!(--two <VALUE>).required(true))
9+
.arg(arg!(--one <VALUE>).required(true))
1010
.get_matches();
1111

1212
println!(

0 commit comments

Comments
 (0)