mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 15:27:16 +00:00
fix: Ensure arg!
gets help/version correct
Because of our changes from v3, we can't rely on `_build` taking care of this for us.
This commit is contained in:
parent
0105b65e4e
commit
95207a1e6f
2 changed files with 73 additions and 5 deletions
|
@ -207,9 +207,14 @@ macro_rules! arg_impl {
|
|||
if arg.get_id().is_empty() {
|
||||
arg = arg.id(long);
|
||||
}
|
||||
let action = match arg.get_id() {
|
||||
"help" => $crate::ArgAction::Help,
|
||||
"version" => $crate::ArgAction::Version,
|
||||
_ => $crate::ArgAction::SetTrue,
|
||||
};
|
||||
arg
|
||||
.long(long)
|
||||
.action($crate::ArgAction::SetTrue)
|
||||
.action(action)
|
||||
})
|
||||
$($tail)*
|
||||
}
|
||||
|
@ -231,9 +236,14 @@ macro_rules! arg_impl {
|
|||
if arg.get_id().is_empty() {
|
||||
arg = arg.id(long);
|
||||
}
|
||||
let action = match arg.get_id() {
|
||||
"help" => $crate::ArgAction::Help,
|
||||
"version" => $crate::ArgAction::Version,
|
||||
_ => $crate::ArgAction::SetTrue,
|
||||
};
|
||||
arg
|
||||
.long(long)
|
||||
.action($crate::ArgAction::SetTrue)
|
||||
.action(action)
|
||||
})
|
||||
$($tail)*
|
||||
}
|
||||
|
@ -251,9 +261,14 @@ macro_rules! arg_impl {
|
|||
debug_assert_eq!($arg.get_value_names(), None, "Flags should precede values");
|
||||
debug_assert!(!matches!($arg.get_action(), $crate::ArgAction::Append), "Flags should precede `...`");
|
||||
|
||||
let action = match $arg.get_id() {
|
||||
"help" => $crate::ArgAction::Help,
|
||||
"version" => $crate::ArgAction::Version,
|
||||
_ => $crate::ArgAction::SetTrue,
|
||||
};
|
||||
$arg
|
||||
.short($crate::arg_impl! { @char $short })
|
||||
.action($crate::ArgAction::SetTrue)
|
||||
.action(action)
|
||||
})
|
||||
$($tail)*
|
||||
}
|
||||
|
@ -271,9 +286,14 @@ macro_rules! arg_impl {
|
|||
debug_assert_eq!($arg.get_value_names(), None, "Flags should precede values");
|
||||
debug_assert!(!matches!($arg.get_action(), $crate::ArgAction::Append), "Flags should precede `...`");
|
||||
|
||||
let action = match $arg.get_id() {
|
||||
"help" => $crate::ArgAction::Help,
|
||||
"version" => $crate::ArgAction::Version,
|
||||
_ => $crate::ArgAction::SetTrue,
|
||||
};
|
||||
$arg
|
||||
.short($crate::arg_impl! { @char $short })
|
||||
.action($crate::ArgAction::SetTrue)
|
||||
.action(action)
|
||||
})
|
||||
$($tail)*
|
||||
}
|
||||
|
@ -413,7 +433,7 @@ macro_rules! arg_impl {
|
|||
$arg.action($crate::ArgAction::Append)
|
||||
}
|
||||
},
|
||||
$crate::ArgAction::SetTrue => {
|
||||
$crate::ArgAction::SetTrue | $crate::ArgAction::Help | $crate::ArgAction::Version => {
|
||||
$arg.action($crate::ArgAction::Count)
|
||||
}
|
||||
action => {
|
||||
|
|
|
@ -112,6 +112,54 @@ mod arg {
|
|||
assert_eq!(arg.get_help(), Some("How to use it"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn short_help() {
|
||||
let arg = clap::arg!(help: -b);
|
||||
assert!(matches!(arg.get_action(), clap::ArgAction::Help));
|
||||
|
||||
let arg = clap::arg!(help: -b ...);
|
||||
assert!(matches!(arg.get_action(), clap::ArgAction::Count));
|
||||
|
||||
let arg = clap::arg!(help: -b <NUM>);
|
||||
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_help() {
|
||||
let arg = clap::arg!(-'?' - -help);
|
||||
assert!(matches!(arg.get_action(), clap::ArgAction::Help));
|
||||
|
||||
let arg = clap::arg!(-'?' --help ...);
|
||||
assert!(matches!(arg.get_action(), clap::ArgAction::Count));
|
||||
|
||||
let arg = clap::arg!(-'?' --help <NUM>);
|
||||
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn short_version() {
|
||||
let arg = clap::arg!(version: -b);
|
||||
assert!(matches!(arg.get_action(), clap::ArgAction::Version));
|
||||
|
||||
let arg = clap::arg!(version: -b ...);
|
||||
assert!(matches!(arg.get_action(), clap::ArgAction::Count));
|
||||
|
||||
let arg = clap::arg!(version: -b <NUM>);
|
||||
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_version() {
|
||||
let arg = clap::arg!(-'?' - -version);
|
||||
assert!(matches!(arg.get_action(), clap::ArgAction::Version));
|
||||
|
||||
let arg = clap::arg!(-'?' --version ...);
|
||||
assert!(matches!(arg.get_action(), clap::ArgAction::Count));
|
||||
|
||||
let arg = clap::arg!(-'?' --version <NUM>);
|
||||
assert!(matches!(arg.get_action(), clap::ArgAction::Set));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn short_with_value() {
|
||||
let arg = clap::arg!(foo: -b <NUM>);
|
||||
|
|
Loading…
Add table
Reference in a new issue