Merge pull request #1170 from tmccombs/clap-improvements-2

Clap improvements 2
This commit is contained in:
Thayne McCombs 2022-11-12 09:09:48 -07:00 committed by GitHub
commit 85e3adaf18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 96 deletions

12
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",
@ -129,18 +129,18 @@ dependencies = [
[[package]]
name = "clap_complete"
version = "4.0.3"
version = "4.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfe581a2035db4174cdbdc91265e1aba50f381577f0510d0ad36c7bc59cc84a3"
checksum = "96b0fba905b035a30d25c1b585bf1171690712fbb0ad3ac47214963aa4acc36c"
dependencies = [
"clap",
]
[[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

@ -50,10 +50,10 @@ normpath = "0.3.2"
chrono = "0.4"
once_cell = "1.15.0"
crossbeam-channel = "0.5.6"
clap_complete = {version = "4.0", optional = true}
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,32 +627,14 @@ impl Opts {
self.gen_completions
.map(|maybe_shell| match maybe_shell {
Some(sh) => Ok(sh),
None => guess_shell(),
None => {
Shell::from_env().ok_or_else(|| anyhow!("Unable to get shell from environment"))
}
})
.transpose()
}
}
#[cfg(feature = "completions")]
fn guess_shell() -> anyhow::Result<Shell> {
let env_shell = std::env::var_os("SHELL").map(PathBuf::from);
if let Some(shell) = env_shell
.as_ref()
.and_then(|s| s.file_name())
.and_then(|s| s.to_str())
{
shell
.parse::<Shell>()
.map_err(|_| anyhow!("Unknown shell {}", shell))
} else {
// Assume powershell on windows
#[cfg(windows)]
return Ok(Shell::PowerShell);
#[cfg(not(windows))]
return Err(anyhow!("Unable to get shell from environment"));
}
}
#[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]
pub enum FileType {
#[value(alias = "f")]

View file

@ -186,6 +186,7 @@ impl TestEnv {
}
/// Get the path of the fd executable.
#[cfg_attr(windows, allow(unused))]
pub fn test_exe(&self) -> &PathBuf {
&self.fd_exe
}

View file

@ -2208,7 +2208,7 @@ fn test_invalid_cwd() {
std::env::set_current_dir(&root).unwrap();
fs::remove_dir(&root).unwrap();
let output = std::process::Command::new(&te.test_exe())
let output = std::process::Command::new(te.test_exe())
.arg("query")
.arg(te.test_root())
.output()