Skip to content

Commit

Permalink
Merge pull request #4032 from epage/flag
Browse files Browse the repository at this point in the history
fix!: Make ArgAction::Set the default
  • Loading branch information
epage committed Aug 6, 2022
2 parents 58b0529 + 69ad5cf commit 179faa6
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 108 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- `arg!` now sets `ArgAction::SetTrue`, `ArgAction::Count`, `ArgAction::Set`, or `ArgAction::Append` as appropriate (#3795)
- Default actions are now `Set` and `SetTrue`
- Removed `PartialEq` and `Eq` from `Command`
- By default, an `Arg`s default action is `ArgAction::Set`, rather than `ArgAction::SetTrue`
- Replace `Arg::number_of_values` (average across occurrences) with `Arg::num_args` (per occurrence, raw CLI args, not parsed values)
- `num_args(0)` no longer implies `takes_value(true).multiple_values(true)`
- `num_args(1)` no longer implies `multiple_values(true)`
Expand Down
3 changes: 2 additions & 1 deletion clap_bench/benches/03_complex.rs
Expand Up @@ -67,12 +67,13 @@ pub fn build_from_builder(c: &mut Criterion) {
.help("tests flags")
.long("flag")
.global(true)
.action(ArgAction::Append),
.action(ArgAction::Count),
)
.arg(
Arg::new("flag2")
.short('F')
.help("tests flags with exclusions")
.action(ArgAction::SetTrue)
.conflicts_with("flag")
.requires("option2"),
)
Expand Down
4 changes: 3 additions & 1 deletion clap_bench/benches/04_new_help.rs
Expand Up @@ -42,7 +42,8 @@ fn app_example3<'c>() -> Command<'c> {
.arg(
Arg::new("debug")
.help("turn on debugging information")
.short('d'),
.short('d')
.action(ArgAction::SetTrue),
)
.args(&[
Arg::new("config")
Expand Down Expand Up @@ -73,6 +74,7 @@ fn app_example4<'c>() -> Command<'c> {
Arg::new("debug")
.help("turn on debugging information")
.short('d')
.action(ArgAction::SetTrue)
.long("debug"),
)
.arg(
Expand Down
15 changes: 13 additions & 2 deletions clap_bench/benches/05_ripgrep.rs
Expand Up @@ -299,7 +299,7 @@ where
F: Fn(&'static str) -> &'static str,
{
let arg = |name| Arg::new(name).help(doc(name));
let flag = |name| arg(name).long(name);
let flag = |name| arg(name).long(name).action(ArgAction::SetTrue);

Command::new("ripgrep")
.author("BurntSushi") // simulating since it's only a bench
Expand Down Expand Up @@ -379,18 +379,21 @@ where
.arg(
flag("after-context")
.short('A')
.action(ArgAction::Set)
.value_name("NUM")
.value_parser(value_parser!(usize)),
)
.arg(
flag("before-context")
.short('B')
.action(ArgAction::Set)
.value_name("NUM")
.value_parser(value_parser!(usize)),
)
.arg(
flag("context")
.short('C')
.action(ArgAction::Set)
.value_name("NUM")
.value_parser(value_parser!(usize)),
)
Expand Down Expand Up @@ -419,11 +422,13 @@ where
.arg(
flag("max-count")
.short('m')
.action(ArgAction::Set)
.value_name("NUM")
.value_parser(value_parser!(usize)),
)
.arg(
flag("maxdepth")
.action(ArgAction::Set)
.value_name("NUM")
.value_parser(value_parser!(usize)),
)
Expand All @@ -436,13 +441,19 @@ where
.arg(flag("null"))
.arg(flag("path-separator").value_name("SEPARATOR"))
.arg(flag("pretty").short('p'))
.arg(flag("replace").short('r').value_name("ARG"))
.arg(
flag("replace")
.short('r')
.action(ArgAction::Set)
.value_name("ARG"),
)
.arg(flag("case-sensitive").short('s'))
.arg(flag("smart-case").short('S'))
.arg(flag("sort-files"))
.arg(
flag("threads")
.short('j')
.action(ArgAction::Set)
.value_name("ARG")
.value_parser(value_parser!(usize)),
)
Expand Down
10 changes: 8 additions & 2 deletions clap_bench/benches/06_rustup.rs
Expand Up @@ -30,7 +30,8 @@ fn build_cli() -> Command<'static> {
Arg::new("verbose")
.help("Enable verbose output")
.short('v')
.long("verbose"),
.long("verbose")
.action(ArgAction::SetTrue),
)
.subcommand(
Command::new("show")
Expand All @@ -53,6 +54,7 @@ fn build_cli() -> Command<'static> {
Arg::new("no-self-update")
.help("Don't perform self update when running the `rustup` command")
.long("no-self-update")
.action(ArgAction::SetTrue)
.hide(true),
),
)
Expand Down Expand Up @@ -210,6 +212,7 @@ fn build_cli() -> Command<'static> {
.arg(
Arg::new("nonexistent")
.long("nonexistent")
.action(ArgAction::SetTrue)
.help("Remove override toolchain for all nonexistent directories"),
),
)
Expand All @@ -226,6 +229,7 @@ fn build_cli() -> Command<'static> {
.arg(
Arg::new("nonexistent")
.long("nonexistent")
.action(ArgAction::SetTrue)
.help("Remove override toolchain for all nonexistent directories"),
),
),
Expand All @@ -250,11 +254,13 @@ fn build_cli() -> Command<'static> {
.arg(
Arg::new("book")
.long("book")
.action(ArgAction::SetTrue)
.help("The Rust Programming Language book"),
)
.arg(
Arg::new("std")
.long("std")
.action(ArgAction::SetTrue)
.help("Standard library API documentation"),
)
.group(ArgGroup::new("page").args(&["book", "std"])),
Expand All @@ -276,7 +282,7 @@ fn build_cli() -> Command<'static> {
.subcommand(
Command::new("uninstall")
.about("Uninstall rustup.")
.arg(Arg::new("no-prompt").short('y')),
.arg(Arg::new("no-prompt").short('y').action(ArgAction::SetTrue)),
)
.subcommand(
Command::new("upgrade-data").about("Upgrade the internal data format."),
Expand Down
15 changes: 8 additions & 7 deletions clap_complete/src/shells/zsh.rs
Expand Up @@ -623,13 +623,14 @@ fn write_positionals_of(p: &Command) -> String {
debug!("write_positionals_of:iter: arg={}", arg.get_id());

let num_args = arg.get_num_args().expect("built");
let cardinality = if num_args != 1.into() && num_args != 0.into() {
"*:"
} else if !arg.is_required_set() {
":"
} else {
""
};
let cardinality =
if num_args != builder::ValueRange::EMPTY && num_args != builder::ValueRange::SINGLE {
"*:"
} else if !arg.is_required_set() {
":"
} else {
""
};

let a = format!(
"'{cardinality}:{name}{help}:{value_completion}' \\",
Expand Down
2 changes: 1 addition & 1 deletion clap_complete_fig/src/fig.rs
Expand Up @@ -335,7 +335,7 @@ fn gen_args(arg: &Arg, indent: usize) -> String {
));

let num_args = arg.get_num_args().expect("built");
if num_args != 0.into() && num_args != 1.into() {
if num_args != builder::ValueRange::EMPTY && num_args != builder::ValueRange::SINGLE {
buffer.push_str(&format!(
"{:indent$}isVariadic: true,\n",
"",
Expand Down

0 comments on commit 179faa6

Please sign in to comment.