Use unit type for negating optins with clap_derive

As now supported by https://github.com/clap-rs/clap/pull/4371  and https://github.com/clap-rs/clap/pull/4459
This commit is contained in:
Thayne McCombs 2022-11-08 00:03:55 -07:00
parent 2c3e40c9d9
commit 84bf65e023
3 changed files with 28 additions and 72 deletions

8
Cargo.lock generated
View File

@ -113,9 +113,9 @@ dependencies = [
[[package]]
name = "clap"
version = "4.0.18"
version = "4.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "335867764ed2de42325fafe6d18b8af74ba97ee0c590fa016f157535b42ab04b"
checksum = "91b9970d7505127a162fdaa9b96428d28a479ba78c9ec7550a63a5d9863db682"
dependencies = [
"atty",
"bitflags",
@ -138,9 +138,9 @@ dependencies = [
[[package]]
name = "clap_derive"
version = "4.0.18"
version = "4.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16a1b0f6422af32d5da0c58e2703320f379216ee70198241c84173a8c5ac28f3"
checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014"
dependencies = [
"heck",
"proc-macro-error",

View File

@ -53,7 +53,7 @@ crossbeam-channel = "0.5.6"
clap_complete = {version = "4.0.5", optional = true}
[dependencies.clap]
version = "4.0.12"
version = "4.0.22"
features = ["suggestions", "color", "wrap_help", "cargo", "unstable-grouped", "derive"]
[target.'cfg(unix)'.dependencies]

View File

@ -17,69 +17,6 @@ use crate::filesystem;
use crate::filter::OwnerFilter;
use crate::filter::SizeFilter;
// Type for options that don't have any values, but are used to negate
// earlier options
struct Negations;
impl clap::FromArgMatches for Negations {
fn from_arg_matches(_: &ArgMatches) -> clap::error::Result<Self> {
Ok(Negations)
}
fn update_from_arg_matches(&mut self, _: &ArgMatches) -> clap::error::Result<()> {
Ok(())
}
}
impl clap::Args for Negations {
fn augment_args(cmd: Command) -> Command {
Self::augment_args_for_update(cmd)
}
fn augment_args_for_update(cmd: Command) -> Command {
cmd.arg(
Arg::new("no_hidden")
.action(ArgAction::Count)
.long("no-hidden")
.overrides_with("hidden")
.hide(true)
.long_help("Overrides --hidden."),
)
.arg(
Arg::new("ignore")
.action(ArgAction::Count)
.long("ignore")
.overrides_with("no_ignore")
.hide(true)
.long_help("Overrides --no-ignore."),
)
.arg(
Arg::new("ignore_vcs")
.action(ArgAction::Count)
.long("ignore-vcs")
.overrides_with("no_ignore_vcs")
.hide(true)
.long_help("Overrides --no-ignore-vcs."),
)
.arg(
Arg::new("relative_path")
.action(ArgAction::Count)
.long("relative-path")
.overrides_with("absolute_path")
.hide(true)
.long_help("Overrides --absolute-path."),
)
.arg(
Arg::new("no_follow")
.action(ArgAction::Count)
.long("no-follow")
.overrides_with("follow")
.hide(true)
.long_help("Overrides --follow."),
)
}
}
#[derive(Parser)]
#[command(
name = "fd",
@ -103,6 +40,10 @@ pub struct Opts {
)]
pub hidden: bool,
/// Overrides --hidden
#[arg(long, overrides_with = "hidden", hide = true, action = ArgAction::SetTrue)]
no_hidden: (),
/// Do not respect .(git|fd)ignore files
#[arg(
long,
@ -113,6 +54,10 @@ pub struct Opts {
)]
pub no_ignore: bool,
/// Overrides --no-ignore
#[arg(long, overrides_with = "no_ignore", hide = true, action = ArgAction::SetTrue)]
ignore: (),
/// Do not respect .gitignore files
#[arg(
long,
@ -122,6 +67,10 @@ pub struct Opts {
)]
pub no_ignore_vcs: bool,
/// Overrides --no-ignore-vcs
#[arg(long, overrides_with = "no_ignore_vcs", hide = true, action = ArgAction::SetTrue)]
ignore_vcs: (),
/// Do not respect .(git|fd)ignore files in parent directories
#[arg(
long,
@ -204,6 +153,10 @@ pub struct Opts {
)]
pub absolute_path: bool,
/// Overrides --absolute-path
#[arg(long, overrides_with = "absolute_path", hide = true, action = ArgAction::SetTrue)]
relative_path: (),
/// Use a long listing format with file metadata
#[arg(
long,
@ -227,6 +180,10 @@ pub struct Opts {
)]
pub follow: bool,
/// Overrides --follow
#[arg(long, overrides_with = "follow", hide = true, action = ArgAction::SetTrue)]
no_follow: (),
/// Search full abs. path (default: filename only)
#[arg(
long,
@ -591,9 +548,6 @@ pub struct Opts {
#[cfg(feature = "completions")]
#[arg(long, hide = true, exclusive = true)]
gen_completions: Option<Option<Shell>>,
#[clap(flatten)]
_negations: Negations,
}
impl Opts {
@ -673,7 +627,9 @@ impl Opts {
self.gen_completions
.map(|maybe_shell| match maybe_shell {
Some(sh) => Ok(sh),
None => Shell::from_env().ok_or_else(|| anyhow!("Unable to get shell from environment")),
None => {
Shell::from_env().ok_or_else(|| anyhow!("Unable to get shell from environment"))
}
})
.transpose()
}