Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Clap improvements 2

authored by Thayne McCombs · committed by GitHub · parents 9e88f91, 38d4068 · Browse files · 5 files+33 added−96 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+33 added−96 removed

  • tests:+2 added−1 removed
  • other:+31 added−95 removed

5 files+33 added−96 removed

modified:src/cli.rs+23 added-87 removed
15 unmodified lines
16#[cfg(unix)]17use crate::filter::OwnerFilter;18use crate::filter::SizeFilter;1920// Type for options that don't have any values, but are used to negate21// earlier options22struct Negations;2324impl clap::FromArgMatches for Negations {25 fn from_arg_matches(_: &ArgMatches) -> clap::error::Result<Self> {26 Ok(Negations)27 }2829 fn update_from_arg_matches(&mut self, _: &ArgMatches) -> clap::error::Result<()> {30 Ok(())31 }32}3334impl clap::Args for Negations {35 fn augment_args(cmd: Command) -> Command {36 Self::augment_args_for_update(cmd)37 }3839 fn augment_args_for_update(cmd: Command) -> Command {40 cmd.arg(41 Arg::new("no_hidden")42 .action(ArgAction::Count)43 .long("no-hidden")44 .overrides_with("hidden")45 .hide(true)46 .long_help("Overrides --hidden."),47 )48 .arg(49 Arg::new("ignore")50 .action(ArgAction::Count)51 .long("ignore")52 .overrides_with("no_ignore")53 .hide(true)54 .long_help("Overrides --no-ignore."),55 )56 .arg(57 Arg::new("ignore_vcs")58 .action(ArgAction::Count)59 .long("ignore-vcs")60 .overrides_with("no_ignore_vcs")61 .hide(true)62 .long_help("Overrides --no-ignore-vcs."),63 )64 .arg(65 Arg::new("relative_path")66 .action(ArgAction::Count)67 .long("relative-path")68 .overrides_with("absolute_path")69 .hide(true)70 .long_help("Overrides --absolute-path."),71 )72 .arg(73 Arg::new("no_follow")74 .action(ArgAction::Count)75 .long("no-follow")76 .overrides_with("follow")77 .hide(true)78 .long_help("Overrides --follow."),79 )80 }81}8220#[derive(Parser)]21#[command(22 name = "fd",23 version,
15 unmodified lines
39 The flag can be overridden with --no-hidden."40 )]41 pub hidden: bool,4243 /// Overrides --hidden44 #[arg(long, overrides_with = "hidden", hide = true, action = ArgAction::SetTrue)]45 no_hidden: (),4647 /// Do not respect .(git|fd)ignore files48 #[arg(49 long,50 short = 'I',
2 unmodified lines
53 The flag can be overridden with --ignore."54 )]55 pub no_ignore: bool,5657 /// Overrides --no-ignore58 #[arg(long, overrides_with = "no_ignore", hide = true, action = ArgAction::SetTrue)]59 ignore: (),6061 /// Do not respect .gitignore files62 #[arg(63 long,64 hide_short_help = true,
1 unmodified line
66 ignored by '.gitignore' files. The flag can be overridden with --ignore-vcs."67 )]68 pub no_ignore_vcs: bool,6970 /// Overrides --no-ignore-vcs71 #[arg(long, overrides_with = "no_ignore_vcs", hide = true, action = ArgAction::SetTrue)]72 ignore_vcs: (),7374 /// Do not respect .(git|fd)ignore files in parent directories75 #[arg(76 long,77 hide_short_help = true,
74 unmodified lines
152 The flag can be overridden with --relative-path."153 )]154 pub absolute_path: bool,155156 /// Overrides --absolute-path157 #[arg(long, overrides_with = "absolute_path", hide = true, action = ArgAction::SetTrue)]158 relative_path: (),159160 /// Use a long listing format with file metadata161 #[arg(162 long,163 short = 'l',
15 unmodified lines
179 Flag can be overriden with --no-follow."180 )]181 pub follow: bool,182183 /// Overrides --follow184 #[arg(long, overrides_with = "follow", hide = true, action = ArgAction::SetTrue)]185 no_follow: (),186187 /// Search full abs. path (default: filename only)188 #[arg(189 long,190 short = 'p',
356 unmodified lines
547548 #[cfg(feature = "completions")]549 #[arg(long, hide = true, exclusive = true)]550 gen_completions: Option<Option<Shell>>,594595 #[clap(flatten)]596 _negations: Negations,551}552553impl Opts {554 pub fn search_paths(&self) -> anyhow::Result<Vec<PathBuf>> {
71 unmodified lines
626 pub fn gen_completions(&self) -> anyhow::Result<Option<Shell>> {627 self.gen_completions628 .map(|maybe_shell| match maybe_shell {629 Some(sh) => Ok(sh),676 None => guess_shell(),630 None => {631 Shell::from_env().ok_or_else(|| anyhow!("Unable to get shell from environment"))632 }633 })634 .transpose()635 }636}637682#[cfg(feature = "completions")]683fn guess_shell() -> anyhow::Result<Shell> {684 let env_shell = std::env::var_os("SHELL").map(PathBuf::from);685 if let Some(shell) = env_shell686 .as_ref()687 .and_then(|s| s.file_name())688 .and_then(|s| s.to_str())689 {690 shell691 .parse::<Shell>()692 .map_err(|_| anyhow!("Unknown shell {}", shell))693 } else {694 // Assume powershell on windows695 #[cfg(windows)]696 return Ok(Shell::PowerShell);697 #[cfg(not(windows))]698 return Err(anyhow!("Unable to get shell from environment"));699 }700}701638#[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]639pub enum FileType {640 #[value(alias = "f")]641 File,
143 unmodified lines
modified:tests/testenv/mod.rs+1 added
184 unmodified lines
185 self.temp_dir.path().to_path_buf()186 }187188 /// Get the path of the fd executable.189 #[cfg_attr(windows, allow(unused))]190 pub fn test_exe(&self) -> &PathBuf {191 &self.fd_exe192 }193
128 unmodified lines
modified:tests/tests.rs+1 added-1 removed
2206 unmodified lines
2207 fs::create_dir(&root).unwrap();2208 std::env::set_current_dir(&root).unwrap();2209 fs::remove_dir(&root).unwrap();22102211 let output = std::process::Command::new(&te.test_exe())2211 let output = std::process::Command::new(te.test_exe())2212 .arg("query")2213 .arg(te.test_root())2214 .output()2215 .unwrap();
5 unmodified lines
modified:Cargo.lock+6 added-6 removed
111 unmodified lines
112]113114[[package]]115name = "clap"116version = "4.0.18"116version = "4.0.22"117source = "registry+https://github.com/rust-lang/crates.io-index"118checksum = "335867764ed2de42325fafe6d18b8af74ba97ee0c590fa016f157535b42ab04b"118checksum = "91b9970d7505127a162fdaa9b96428d28a479ba78c9ec7550a63a5d9863db682"119dependencies = [120 "atty",121 "bitflags",122 "clap_derive",
5 unmodified lines
128]129130[[package]]131name = "clap_complete"132version = "4.0.3"132version = "4.0.5"133source = "registry+https://github.com/rust-lang/crates.io-index"134checksum = "dfe581a2035db4174cdbdc91265e1aba50f381577f0510d0ad36c7bc59cc84a3"134checksum = "96b0fba905b035a30d25c1b585bf1171690712fbb0ad3ac47214963aa4acc36c"135dependencies = [136 "clap",137]138139[[package]]140name = "clap_derive"141version = "4.0.18"141version = "4.0.21"142source = "registry+https://github.com/rust-lang/crates.io-index"143checksum = "16a1b0f6422af32d5da0c58e2703320f379216ee70198241c84173a8c5ac28f3"143checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014"144dependencies = [145 "heck",146 "proc-macro-error",147 "proc-macro2",
970 unmodified lines
modified:Cargo.toml+2 added-2 removed