Use clap, change --sensitive to --case-sensitive
Before adoption: this commit came in with the history the project adopted, so no landing records it.
see #33
authored by sharkdp · committed by sharkdp · parent 1adcf76 · Browse files · 4 files·+122 added−58 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+122 added−58 removed
- tests:+6 added−6 removed
- other:+116 added−52 removed
Provenance
- Commit
087e709- Tree
3fa15df- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
1adcf76
4 files changed+122 added−58 removed
modified:src/main.rs+57 added-44 removed
1+#[macro_use]2+extern crate clap;3 extern crate ansi_term;2-extern crate getopts;4 extern crate atty;5 extern crate regex;6 extern crate ignore;7 9 unmodified lines17 use std::os::unix::fs::PermissionsExt;18 use std::path::{Path, Component};19 use std::process;20 20-use getopts::Options;21+use clap::{App, AppSettings, Arg};22 use atty::Stream;23 use regex::{Regex, RegexBuilder};24 use ignore::WalkBuilder;25 175 unmodified lines201 process::exit(1);202 }203 204 fn main() {204- let args: Vec<String> = env::args().collect();205+ let matches =206+ App::new("fd")207+ .version(crate_version!())208+ .usage("fd [FLAGS/OPTIONS] [<pattern>] [<path>]")209+ .setting(AppSettings::ColoredHelp)210+ .setting(AppSettings::DeriveDisplayOrder)211+ .arg(Arg::with_name("case-sensitive")212+ .long("case-sensitive")213+ .short("s")214+ .help("Case-sensitive search (default: smart case)"))215+ .arg(Arg::with_name("full-path")216+ .long("full-path")217+ .short("p")218+ .help("Search full path (default: file-/dirname only)"))219+ .arg(Arg::with_name("hidden")220+ .long("hidden")221+ .short("H")222+ .help("Search hidden files and directories"))223+ .arg(Arg::with_name("no-ignore")224+ .long("no-ignore")225+ .short("I")226+ .help("Do not respect .(git)ignore files"))227+ .arg(Arg::with_name("follow")228+ .long("follow")229+ .short("f")230+ .help("Follow symlinks"))231+ .arg(Arg::with_name("absolute-path")232+ .long("absolute-path")233+ .short("a")234+ .help("Show absolute instead of relative paths"))235+ .arg(Arg::with_name("no-color")236+ .long("no-color")237+ .short("n")238+ .help("Do not colorize output"))239+ .arg(Arg::with_name("depth")240+ .long("max-depth")241+ .short("d")242+ .takes_value(true)243+ .help("Set maximum search depth (default: none)"))244+ .arg(Arg::with_name("pattern")245+ .help("the search pattern, a regular expression (optional)"))246+ .arg(Arg::with_name("path")247+ .help("the root directory for the filesystem search (optional)"))248+ .get_matches();249 206- let mut opts = Options::new();207- opts.optflag("h", "help",208- "print this help message");209- opts.optflag("s", "sensitive",210- "case-sensitive search (default: smart case)");211- opts.optflag("p", "full-path",212- "search full path (default: file-/dirname only)");213- opts.optflag("H", "hidden",214- "search hidden files/directories");215- opts.optflag("I", "no-ignore",216- "do not respect .(git)ignore files");217- opts.optflag("f", "follow",218- "follow symlinks");219- opts.optflag("n", "no-color",220- "do not colorize output");221- opts.optopt("d", "max-depth",222- "maximum search depth (default: none)", "D");223- opts.optflag("a", "absolute",224- "show absolute instead of relative paths");225-226- let matches = match opts.parse(&args[1..]) {227- Ok(m) => m,228- Err(e) => error(e.description())229- };230-231- if matches.opt_present("h") {232- let brief = "Usage: fd [options] [PATTERN] [PATH]";233- print!("{}", opts.usage(brief));234- process::exit(1);235- }236-250 // Get the search pattern251 let empty_pattern = String::new();239- let pattern = matches.free.get(0).unwrap_or(&empty_pattern);252+ let pattern = matches.value_of("pattern").unwrap_or(&empty_pattern);253 254 // Get the current working directory255 let current_dir_buf = match env::current_dir() {256 Ok(cd) => cd,1 unmodified line258 };259 let current_dir = current_dir_buf.as_path();260 261 // Get the root directory for the search249- let root_dir_buf = matches.free.get(1)262+ let root_dir_buf = matches.value_of("path")263 .and_then(|r| fs::canonicalize(r).ok())264 .unwrap_or_else(|| current_dir_buf.clone());265 let root_dir = root_dir_buf.as_path();266 267 // The search will be case-sensitive if the command line flag is set or268 // if the pattern has an uppercase character (smart case).256- let case_sensitive = matches.opt_present("sensitive") ||269+ let case_sensitive = matches.is_present("case-sensitive") ||270 pattern.chars().any(char::is_uppercase);271 259- let colored_output = !matches.opt_present("no-color") &&272+ let colored_output = !matches.is_present("no-color") &&273 atty::is(Stream::Stdout);274 275 let ls_colors =276 if colored_output {8 unmodified lines285 };286 287 let config = FdOptions {288 case_sensitive: case_sensitive,276- search_full_path: matches.opt_present("full-path"),277- ignore_hidden: !matches.opt_present("hidden"),278- read_ignore: !matches.opt_present("no-ignore"),279- follow_links: matches.opt_present("follow"),280- max_depth: matches.opt_str("max-depth")289+ search_full_path: matches.is_present("full-path"),290+ ignore_hidden: !matches.is_present("hidden"),291+ read_ignore: !matches.is_present("no-ignore"),292+ follow_links: matches.is_present("follow"),293+ max_depth: matches.value_of("depth")294 .and_then(|ds| usize::from_str_radix(&ds, 10).ok()),282- path_display: if matches.opt_present("absolute") {295+ path_display: if matches.is_present("absolute-path") {296 PathDisplay::Absolute297 } else {298 PathDisplay::Relative299 },16 unmodified linesmodified:tests/test.sh+6 added-6 removed
111 unmodified lines112 one/two/c.foo113 one/two/C.Foo" '[a-c].foo'114 expect "a.foo115 one/b.foo116-one/two/c.foo" --sensitive '[a-c].foo'116+one/two/c.foo" --case-sensitive '[a-c].foo'117 118 119 120 suite "Smart case"2 unmodified lines123 expect "one/two/C.Foo" C.Foo124 expect "one/two/C.Foo" Foo125 126 127-suite "Case-sensitivity (--sensitive)"128-expect "one/two/c.foo" --sensitive c.foo129-expect "one/two/C.Foo" --sensitive C.Foo127+suite "Case-sensitivity (--case-sensitive)"128+expect "one/two/c.foo" --case-sensitive c.foo129+expect "one/two/C.Foo" --case-sensitive C.Foo130 131 132 suite "Full path search (--full-path)"133 expect "one/two/three/d.foo55 unmodified lines189 one190 symlink" --max-depth 1191 192 193-suite "Absolute paths (--absolute)"193+suite "Absolute paths (--absolute-path)"194 expect "$root/a.foo195 $root/one/b.foo196 $root/one/two/c.foo197 $root/one/two/C.Foo198 $root/one/two/three/d.foo199-$root/one/two/three/directory_foo" --absolute foo199+$root/one/two/three/directory_foo" --absolute-path foo200 201 # All done202 echomodified:Cargo.lock+58 added-7 removed
2 unmodified lines3 version = "1.1.0"4 dependencies = [5 "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",6 "atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",7- "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",7+ "clap 2.24.2 (registry+https://github.com/rust-lang/crates.io-index)",8 "ignore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",9 "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",10 ]11 20 unmodified lines32 "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",33 ]34 35 [[package]]36+name = "bitflags"37+version = "0.8.2"38+source = "registry+https://github.com/rust-lang/crates.io-index"39+40+[[package]]41+name = "clap"42+version = "2.24.2"43+source = "registry+https://github.com/rust-lang/crates.io-index"44+dependencies = [45+ "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",46+ "atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",47+ "bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",48+ "strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",49+ "term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",50+ "unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",51+ "unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",52+ "vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",53+]54+55+[[package]]56 name = "crossbeam"57 version = "0.2.10"58 source = "registry+https://github.com/rust-lang/crates.io-index"59 2 unmodified lines62 version = "1.0.5"63 source = "registry+https://github.com/rust-lang/crates.io-index"64 65 [[package]]46-name = "getopts"47-version = "0.2.14"48-source = "registry+https://github.com/rust-lang/crates.io-index"49-50-[[package]]66 name = "globset"67 version = "0.2.0"68 source = "registry+https://github.com/rust-lang/crates.io-index"69 dependencies = [77 unmodified lines147 "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",148 ]149 150 [[package]]151+name = "strsim"152+version = "0.6.0"153+source = "registry+https://github.com/rust-lang/crates.io-index"154+155+[[package]]156+name = "term_size"157+version = "0.3.0"158+source = "registry+https://github.com/rust-lang/crates.io-index"159+dependencies = [160+ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",161+ "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",162+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",163+]164+165+[[package]]166 name = "thread-id"167 version = "3.0.0"168 source = "registry+https://github.com/rust-lang/crates.io-index"169 dependencies = [10 unmodified lines180 "unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",181 ]182 183 [[package]]184+name = "unicode-segmentation"185+version = "1.2.0"186+source = "registry+https://github.com/rust-lang/crates.io-index"187+188+[[package]]189+name = "unicode-width"190+version = "0.1.4"191+source = "registry+https://github.com/rust-lang/crates.io-index"192+193+[[package]]194 name = "unreachable"195 version = "0.1.1"196 source = "registry+https://github.com/rust-lang/crates.io-index"197 dependencies = [5 unmodified lines203 version = "1.0.0"204 source = "registry+https://github.com/rust-lang/crates.io-index"205 206 [[package]]207+name = "vec_map"208+version = "0.8.0"209+source = "registry+https://github.com/rust-lang/crates.io-index"210+211+[[package]]212 name = "void"213 version = "1.0.2"214 source = "registry+https://github.com/rust-lang/crates.io-index"215 20 unmodified lines236 [metadata]237 "checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"238 "checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"239 "checksum atty 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159"240+"checksum bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1370e9fc2a6ae53aea8b7a5110edbd08836ed87c88736dfabccade1c2b44bff4"241+"checksum clap 2.24.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6b8f69e518f967224e628896b54e41ff6acfb4dcfefc5076325c36525dac900f"242 "checksum crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0c5ea215664ca264da8a9d9c3be80d2eaf30923c259d03e870388eb927508f97"243 "checksum fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6cc484842f1e2884faf56f529f960cc12ad8c71ce96cc7abba0a067c98fee344"197-"checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685"244 "checksum globset 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "feeb1b6840809ef5efcf7a4a990bc4e1b7ee3df8cf9e2379a75aeb2ba42ac9c3"245 "checksum ignore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9bda0eaa44a060e21581315efb449e7fabceb93cf38c8376dd1b7e0623e07844"246 "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"247 "checksum lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3b37545ab726dd833ec6420aaba8231c5b320814b9029ad585555d2a03e94fbf"2 unmodified lines250 "checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4"251 "checksum regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4278c17d0f6d62dfef0ab00028feb45bd7d2102843f80763474eeb1be8a10c01"252 "checksum regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9191b1f57603095f105d317e375d19b1c9c5c3185ea9633a99a6dcbed04457"253 "checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7"254+"checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694"255+"checksum term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2b6b55df3198cc93372e85dd2ed817f0e38ce8cc0f22eb32391bfad9c4bf209"256 "checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a"257 "checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7"258+"checksum unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a8083c594e02b8ae1654ae26f0ade5158b119bd88ad0e8227a5d8fcd72407946"259+"checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f"260 "checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91"261 "checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"262+"checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c"263 "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"264 "checksum walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff"265 "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"266 "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"modified:Cargo.toml+1 added-1 removed
3 unmodified lines456788910113 unmodified linesauthors = ["David Peter <mail@david-peter.de>"]
[dependencies]ansi_term = "0.9"getopts = "0.2"clap = "2.24.2"atty = "0.2"regex = "0.2"ignore = "0.2"