Skip to content

Commit

Permalink
Merge pull request #1170 from tmccombs/clap-improvements-2
Browse files Browse the repository at this point in the history
Clap improvements 2
  • Loading branch information
tmccombs authored Nov 12, 2022
2 parents 9e88f91 + 38d4068 commit 85e3ada
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 96 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
110 changes: 23 additions & 87 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")]
Expand Down
1 change: 1 addition & 0 deletions tests/testenv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 85e3ada

Please sign in to comment.