Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: Make ArgAction::Set the default #4032

Merged
merged 6 commits into from Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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