app.rs
This file
Path
src/app.rs
760 lines · 33.2 KiB · Rust
Last changed by
Merge pull request #861 from jcaplan/no-strip/760David Peterbefore adoptionb211ded
History of this fileTake it
1use clap::{crate_version, App, AppSettings, Arg};23pub fn build_app() -> App<'static, 'static> {4 let clap_color_setting = if std::env::var_os("NO_COLOR").is_none() {5 AppSettings::ColoredHelp6 } else {7 AppSettings::ColorNever8 };910 let mut app = App::new("fd")11 .version(crate_version!())12 .usage("fd [FLAGS/OPTIONS] [<pattern>] [<path>...]")13 .setting(clap_color_setting)14 .setting(AppSettings::DeriveDisplayOrder)15 .after_help(16 "Note: `fd -h` prints a short and concise overview while `fd --help` gives all \17 details.",18 )19 .arg(20 Arg::with_name("hidden")21 .long("hidden")22 .short("H")23 .overrides_with("hidden")24 .help("Search hidden files and directories")25 .long_help(26 "Include hidden directories and files in the search results (default: \27 hidden files and directories are skipped). Files and directories are \28 considered to be hidden if their name starts with a `.` sign (dot). \29 The flag can be overridden with --no-hidden.",30 ),31 )32 .arg(33 Arg::with_name("no-hidden")34 .long("no-hidden")35 .overrides_with("hidden")36 .hidden(true)37 .long_help(38 "Overrides --hidden.",39 ),40 )41 .arg(42 Arg::with_name("no-ignore")43 .long("no-ignore")44 .short("I")45 .overrides_with("no-ignore")46 .help("Do not respect .(git|fd)ignore files")47 .long_help(48 "Show search results from files and directories that would otherwise be \49 ignored by '.gitignore', '.ignore', '.fdignore', or the global ignore file. \50 The flag can be overridden with --ignore.",51 ),52 )53 .arg(54 Arg::with_name("ignore")55 .long("ignore")56 .overrides_with("no-ignore")57 .hidden(true)58 .long_help(59 "Overrides --no-ignore.",60 ),61 )62 .arg(63 Arg::with_name("no-ignore-vcs")64 .long("no-ignore-vcs")65 .overrides_with("no-ignore-vcs")66 .hidden_short_help(true)67 .help("Do not respect .gitignore files")68 .long_help(69 "Show search results from files and directories that would otherwise be \70 ignored by '.gitignore' files. The flag can be overridden with --ignore-vcs.",71 ),72 )73 .arg(74 Arg::with_name("ignore-vcs")75 .long("ignore-vcs")76 .overrides_with("no-ignore-vcs")77 .hidden(true)78 .long_help(79 "Overrides --no-ignore-vcs.",80 ),81 )82 .arg(83 Arg::with_name("no-ignore-parent")84 .long("no-ignore-parent")85 .overrides_with("no-ignore-parent")86 .hidden_short_help(true)87 .help("Do not respect .(git|fd)ignore files in parent directories")88 .long_help(89 "Show search results from files and directories that would otherwise be \90 ignored by '.gitignore', '.ignore', or '.fdignore' files in parent directories.",91 ),92 )93 .arg(94 Arg::with_name("no-global-ignore-file")95 .long("no-global-ignore-file")96 .hidden(true)97 .help("Do not respect the global ignore file")98 .long_help("Do not respect the global ignore file."),99 )100 .arg(101 Arg::with_name("rg-alias-hidden-ignore")102 .short("u")103 .long("unrestricted")104 .overrides_with_all(&["ignore", "no-hidden"])105 .multiple(true)106 .hidden_short_help(true)107 .help("Alias for '--no-ignore', and '--hidden' when given twice")108 .long_help(109 "Alias for '--no-ignore'. Can be repeated. '-uu' is an alias for \110 '--no-ignore --hidden'.",111 ),112 )113 .arg(114 Arg::with_name("case-sensitive")115 .long("case-sensitive")116 .short("s")117 .overrides_with_all(&["ignore-case", "case-sensitive"])118 .help("Case-sensitive search (default: smart case)")119 .long_help(120 "Perform a case-sensitive search. By default, fd uses case-insensitive \121 searches, unless the pattern contains an uppercase character (smart \122 case).",123 ),124 )125 .arg(126 Arg::with_name("ignore-case")127 .long("ignore-case")128 .short("i")129 .overrides_with_all(&["case-sensitive", "ignore-case"])130 .help("Case-insensitive search (default: smart case)")131 .long_help(132 "Perform a case-insensitive search. By default, fd uses case-insensitive \133 searches, unless the pattern contains an uppercase character (smart \134 case).",135 ),136 )137 .arg(138 Arg::with_name("glob")139 .long("glob")140 .short("g")141 .conflicts_with("fixed-strings")142 .overrides_with("glob")143 .help("Glob-based search (default: regular expression)")144 .long_help("Perform a glob-based search instead of a regular expression search."),145 )146 .arg(147 Arg::with_name("regex")148 .long("regex")149 .overrides_with_all(&["glob", "regex"])150 .hidden_short_help(true)151 .help("Regular-expression based search (default)")152 .long_help(153 "Perform a regular-expression based search (default). This can be used to \154 override --glob.",155 ),156 )157 .arg(158 Arg::with_name("fixed-strings")159 .long("fixed-strings")160 .short("F")161 .alias("literal")162 .overrides_with("fixed-strings")163 .hidden_short_help(true)164 .help("Treat pattern as literal string instead of regex")165 .long_help(166 "Treat the pattern as a literal string instead of a regular expression. Note \167 that this also performs substring comparison. If you want to match on an \168 exact filename, consider using '--glob'.",169 ),170 )171 .arg(172 Arg::with_name("absolute-path")173 .long("absolute-path")174 .short("a")175 .overrides_with("absolute-path")176 .help("Show absolute instead of relative paths")177 .long_help(178 "Shows the full path starting from the root as opposed to relative paths. \179 The flag can be overridden with --relative-path.",180 ),181 )182 .arg(183 Arg::with_name("relative-path")184 .long("relative-path")185 .overrides_with("absolute-path")186 .hidden(true)187 .long_help(188 "Overrides --absolute-path.",189 ),190 )191 .arg(192 Arg::with_name("list-details")193 .long("list-details")194 .short("l")195 .conflicts_with("absolute-path")196 .help("Use a long listing format with file metadata")197 .long_help(198 "Use a detailed listing format like 'ls -l'. This is basically an alias \199 for '--exec-batch ls -l' with some additional 'ls' options. This can be \200 used to see more metadata, to show symlink targets and to achieve a \201 deterministic sort order.",202 ),203 )204 .arg(205 Arg::with_name("follow")206 .long("follow")207 .short("L")208 .alias("dereference")209 .overrides_with("follow")210 .help("Follow symbolic links")211 .long_help(212 "By default, fd does not descend into symlinked directories. Using this \213 flag, symbolic links are also traversed. \214 Flag can be overriden with --no-follow.",215 ),216 )217 .arg(218 Arg::with_name("no-follow")219 .long("no-follow")220 .overrides_with("follow")221 .hidden(true)222 .long_help(223 "Overrides --follow.",224 ),225 )226 .arg(227 Arg::with_name("full-path")228 .long("full-path")229 .short("p")230 .overrides_with("full-path")231 .help("Search full abs. path (default: filename only)")232 .long_help(233 "By default, the search pattern is only matched against the filename (or \234 directory name). Using this flag, the pattern is matched against the full \235 (absolute) path. Example:\n \236 fd --glob -p '**/.git/config'",237 ),238 )239 .arg(240 Arg::with_name("null_separator")241 .long("print0")242 .short("0")243 .overrides_with("print0")244 .conflicts_with("list-details")245 .hidden_short_help(true)246 .help("Separate results by the null character")247 .long_help(248 "Separate search results by the null character (instead of newlines). \249 Useful for piping results to 'xargs'.",250 ),251 )252 .arg(253 Arg::with_name("max-depth")254 .long("max-depth")255 .short("d")256 .takes_value(true)257 .value_name("depth")258 .help("Set maximum search depth (default: none)")259 .long_help(260 "Limit the directory traversal to a given depth. By default, there is no \261 limit on the search depth.",262 ),263 )264 // support --maxdepth as well, for compatibility with rg265 .arg(266 Arg::with_name("rg-depth")267 .long("maxdepth")268 .hidden(true)269 .takes_value(true)270 .help("Set maximum search depth (default: none)")271 )272 .arg(273 Arg::with_name("min-depth")274 .long("min-depth")275 .takes_value(true)276 .value_name("depth")277 .hidden_short_help(true)278 .help("Only show results starting at given depth")279 .long_help(280 "Only show search results starting at the given depth. \281 See also: '--max-depth' and '--exact-depth'",282 ),283 )284 .arg(285 Arg::with_name("exact-depth")286 .long("exact-depth")287 .takes_value(true)288 .value_name("depth")289 .hidden_short_help(true)290 .conflicts_with_all(&["max-depth", "min-depth"])291 .help("Only show results at exact given depth")292 .long_help(293 "Only show search results at the exact given depth. This is an alias for \294 '--min-depth <depth> --max-depth <depth>'.",295 ),296 )297 .arg(298 Arg::with_name("prune")299 .long("prune")300 .conflicts_with_all(&["size", "exact-depth"])301 .hidden_short_help(true)302 .help("Do not traverse into matching directories")303 .long_help("Do not traverse into directories that match the search criteria. If \304 you want to exclude specific directories, use the '--exclude=…' option.")305 )306 .arg(307 Arg::with_name("file-type")308 .long("type")309 .short("t")310 .multiple(true)311 .number_of_values(1)312 .takes_value(true)313 .value_name("filetype")314 .possible_values(&[315 "f",316 "file",317 "d",318 "directory",319 "l",320 "symlink",321 "x",322 "executable",323 "e",324 "empty",325 "s",326 "socket",327 "p",328 "pipe",329 ])330 .hide_possible_values(true)331 .help(332 "Filter by type: file (f), directory (d), symlink (l),\nexecutable (x), \333 empty (e), socket (s), pipe (p)",334 )335 .long_help(336 "Filter the search by type:\n \337 'f' or 'file': regular files\n \338 'd' or 'directory': directories\n \339 'l' or 'symlink': symbolic links\n \340 's' or 'socket': socket\n \341 'p' or 'pipe': named pipe (FIFO)\n\n \342 'x' or 'executable': executables\n \343 'e' or 'empty': empty files or directories\n\n\344 This option can be specified more than once to include multiple file types. \345 Searching for '--type file --type symlink' will show both regular files as \346 well as symlinks. Note that the 'executable' and 'empty' filters work differently: \347 '--type executable' implies '--type file' by default. And '--type empty' searches \348 for empty files and directories, unless either '--type file' or '--type directory' \349 is specified in addition.\n\n\350 Examples:\n \351 - Only search for files:\n \352 fd --type file …\n \353 fd -tf …\n \354 - Find both files and symlinks\n \355 fd --type file --type symlink …\n \356 fd -tf -tl …\n \357 - Find executable files:\n \358 fd --type executable\n \359 fd -tx\n \360 - Find empty files:\n \361 fd --type empty --type file\n \362 fd -te -tf\n \363 - Find empty directories:\n \364 fd --type empty --type directory\n \365 fd -te -td"366 ),367 )368 .arg(369 Arg::with_name("extension")370 .long("extension")371 .short("e")372 .multiple(true)373 .number_of_values(1)374 .takes_value(true)375 .value_name("ext")376 .help("Filter by file extension")377 .long_help(378 "(Additionally) filter search results by their file extension. Multiple \379 allowable file extensions can be specified.\n\380 If you want to search for files without extension, \381 you can use the regex '^[^.]+$' as a normal search pattern.",382 ),383 )384 .arg(385 Arg::with_name("exec")386 .long("exec")387 .short("x")388 .min_values(1)389 .allow_hyphen_values(true)390 .value_terminator(";")391 .value_name("cmd")392 .conflicts_with("list-details")393 .help("Execute a command for each search result")394 .long_help(395 "Execute a command for each search result in parallel (use --threads=1 for sequential command execution). \396 All positional arguments following --exec are considered to be arguments to the command - not to fd. \397 It is therefore recommended to place the '-x'/'--exec' option last.\n\398 The following placeholders are substituted before the command is executed:\n \399 '{}': path (of the current search result)\n \400 '{/}': basename\n \401 '{//}': parent directory\n \402 '{.}': path without file extension\n \403 '{/.}': basename without file extension\n\n\404 If no placeholder is present, an implicit \"{}\" at the end is assumed.\n\n\405 Examples:\n\n \406 - find all *.zip files and unzip them:\n\n \407 fd -e zip -x unzip\n\n \408 - find *.h and *.cpp files and run \"clang-format -i ..\" for each of them:\n\n \409 fd -e h -e cpp -x clang-format -i\n\n \410 - Convert all *.jpg files to *.png files:\n\n \411 fd -e jpg -x convert {} {.}.png\412 ",413 ),414 )415 .arg(416 Arg::with_name("exec-batch")417 .long("exec-batch")418 .short("X")419 .min_values(1)420 .allow_hyphen_values(true)421 .value_terminator(";")422 .value_name("cmd")423 .conflicts_with_all(&["exec", "list-details"])424 .help("Execute a command with all search results at once")425 .long_help(426 "Execute the given command once, with all search results as arguments.\n\427 One of the following placeholders is substituted before the command is executed:\n \428 '{}': path (of all search results)\n \429 '{/}': basename\n \430 '{//}': parent directory\n \431 '{.}': path without file extension\n \432 '{/.}': basename without file extension\n\n\433 If no placeholder is present, an implicit \"{}\" at the end is assumed.\n\n\434 Examples:\n\n \435 - Find all test_*.py files and open them in your favorite editor:\n\n \436 fd -g 'test_*.py' -X vim\n\n \437 - Find all *.rs files and count the lines with \"wc -l ...\":\n\n \438 fd -e rs -X wc -l\439 "440 ),441 )442 .arg(443 Arg::with_name("batch-size")444 .long("batch-size")445 .takes_value(true)446 .value_name("size")447 .hidden_short_help(true)448 .requires("exec-batch")449 .help("Max number of arguments to run as a batch with -X")450 .long_help(451 "Maximum number of arguments to pass to the command given with -X. \452 If the number of results is greater than the given size, \453 the command given with -X is run again with remaining arguments. \454 A batch size of zero means there is no limit.",455 ),456 )457 .arg(458 Arg::with_name("exclude")459 .long("exclude")460 .short("E")461 .takes_value(true)462 .value_name("pattern")463 .number_of_values(1)464 .multiple(true)465 .help("Exclude entries that match the given glob pattern")466 .long_help(467 "Exclude files/directories that match the given glob pattern. This \468 overrides any other ignore logic. Multiple exclude patterns can be \469 specified.\n\n\470 Examples:\n \471 --exclude '*.pyc'\n \472 --exclude node_modules",473 ),474 )475 .arg(476 Arg::with_name("ignore-file")477 .long("ignore-file")478 .takes_value(true)479 .value_name("path")480 .number_of_values(1)481 .multiple(true)482 .hidden_short_help(true)483 .help("Add custom ignore-file in '.gitignore' format")484 .long_help(485 "Add a custom ignore-file in '.gitignore' format. These files have a low \486 precedence.",487 ),488 )489 .arg(490 Arg::with_name("color")491 .long("color")492 .short("c")493 .takes_value(true)494 .value_name("when")495 .possible_values(&["never", "auto", "always"])496 .hide_possible_values(true)497 .help("When to use colors: never, *auto*, always")498 .long_help(499 "Declare when to use color for the pattern match output:\n \500 'auto': show colors if the output goes to an interactive console (default)\n \501 'never': do not use colorized output\n \502 'always': always use colorized output",503 ),504 )505 .arg(506 Arg::with_name("threads")507 .long("threads")508 .short("j")509 .takes_value(true)510 .value_name("num")511 .hidden_short_help(true)512 .help("Set number of threads")513 .long_help(514 "Set number of threads to use for searching & executing (default: number \515 of available CPU cores)",516 ),517 )518 .arg(519 Arg::with_name("size")520 .long("size")521 .short("S")522 .takes_value(true)523 .number_of_values(1)524 .allow_hyphen_values(true)525 .multiple(true)526 .help("Limit results based on the size of files")527 .long_help(528 "Limit results based on the size of files using the format <+-><NUM><UNIT>.\n \529 '+': file size must be greater than or equal to this\n \530 '-': file size must be less than or equal to this\n\531 If neither '+' nor '-' is specified, file size must be exactly equal to this.\n \532 'NUM': The numeric size (e.g. 500)\n \533 'UNIT': The units for NUM. They are not case-sensitive.\n\534 Allowed unit values:\n \535 'b': bytes\n \536 'k': kilobytes (base ten, 10^3 = 1000 bytes)\n \537 'm': megabytes\n \538 'g': gigabytes\n \539 't': terabytes\n \540 'ki': kibibytes (base two, 2^10 = 1024 bytes)\n \541 'mi': mebibytes\n \542 'gi': gibibytes\n \543 'ti': tebibytes",544 ),545 )546 .arg(547 Arg::with_name("max-buffer-time")548 .long("max-buffer-time")549 .takes_value(true)550 .hidden(true)551 .help("Milliseconds to buffer before streaming search results to console")552 .long_help(553 "Amount of time in milliseconds to buffer, before streaming the search \554 results to the console.",555 ),556 )557 .arg(558 Arg::with_name("changed-within")559 .long("changed-within")560 .alias("change-newer-than")561 .alias("newer")562 .takes_value(true)563 .value_name("date|dur")564 .number_of_values(1)565 .help("Filter by file modification time (newer than)")566 .long_help(567 "Filter results based on the file modification time. The argument can be provided \568 as a specific point in time (YYYY-MM-DD HH:MM:SS) or as a duration (10h, 1d, 35min). \569 If the time is not specified, it defaults to 00:00:00. \570 '--change-newer-than' or '--newer' can be used as aliases.\n\571 Examples:\n \572 --changed-within 2weeks\n \573 --change-newer-than '2018-10-27 10:00:00'\n \574 --newer 2018-10-27",575 ),576 )577 .arg(578 Arg::with_name("changed-before")579 .long("changed-before")580 .alias("change-older-than")581 .alias("older")582 .takes_value(true)583 .value_name("date|dur")584 .number_of_values(1)585 .help("Filter by file modification time (older than)")586 .long_help(587 "Filter results based on the file modification time. The argument can be provided \588 as a specific point in time (YYYY-MM-DD HH:MM:SS) or as a duration (10h, 1d, 35min). \589 '--change-older-than' or '--older' can be used as aliases.\n\590 Examples:\n \591 --changed-before '2018-10-27 10:00:00'\n \592 --change-older-than 2weeks\n \593 --older 2018-10-27",594 ),595 )596 .arg(597 Arg::with_name("max-results")598 .long("max-results")599 .takes_value(true)600 .value_name("count")601 // We currently do not support --max-results in combination with602 // program execution because the results that come up in a --max-results603 // search are non-deterministic. Users might think that they can run the604 // same search with `--exec rm` attached and get a reliable removal of605 // the files they saw in the previous search.606 .conflicts_with_all(&["exec", "exec-batch", "list-details"])607 .hidden_short_help(true)608 .help("Limit number of search results")609 .long_help("Limit the number of search results to 'count' and quit immediately."),610 )611 .arg(612 Arg::with_name("max-one-result")613 .short("1")614 .hidden_short_help(true)615 .overrides_with("max-results")616 .conflicts_with_all(&["exec", "exec-batch", "list-details"])617 .help("Limit search to a single result")618 .long_help("Limit the search to a single result and quit immediately. \619 This is an alias for '--max-results=1'.")620 )621 .arg(622 Arg::with_name("quiet")623 .long("quiet")624 .short("q")625 .alias("has-results")626 .hidden_short_help(true)627 .conflicts_with_all(&["exec", "exec-batch", "list-details", "max-results"])628 .help("Print nothing, exit code 0 if match found, 1 otherwise")629 .long_help(630 "When the flag is present, the program does not print anything and will \631 return with an exit code of 0 if there is at least one match. Otherwise, the \632 exit code will be 1. \633 '--has-results' can be used as an alias."634 )635 )636 .arg(637 Arg::with_name("show-errors")638 .long("show-errors")639 .hidden_short_help(true)640 .overrides_with("show-errors")641 .help("Show filesystem errors")642 .long_help(643 "Enable the display of filesystem errors for situations such as \644 insufficient permissions or dead symlinks.",645 ),646 )647 .arg(648 Arg::with_name("base-directory")649 .long("base-directory")650 .takes_value(true)651 .value_name("path")652 .number_of_values(1)653 .hidden_short_help(true)654 .help("Change current working directory")655 .long_help(656 "Change the current working directory of fd to the provided path. This \657 means that search results will be shown with respect to the given base \658 path. Note that relative paths which are passed to fd via the positional \659 <path> argument or the '--search-path' option will also be resolved \660 relative to this directory.",661 ),662 )663 .arg(664 Arg::with_name("pattern").help(665 "the search pattern (a regular expression, unless '--glob' is used; optional)",666 ).long_help(667 "the search pattern which is either a regular expression (default) or a glob \668 pattern (if --glob is used). If no pattern has been specified, every entry \669 is considered a match. If your pattern starts with a dash (-), make sure to \670 pass '--' first, or it will be considered as a flag (fd -- '-foo').")671 )672 .arg(673 Arg::with_name("path-separator")674 .takes_value(true)675 .value_name("separator")676 .long("path-separator")677 .hidden_short_help(true)678 .help("Set path separator when printing file paths")679 .long_help(680 "Set the path separator to use when printing file paths. The default is \681 the OS-specific separator ('/' on Unix, '\\' on Windows).",682 ),683 )684 .arg(685 Arg::with_name("path")686 .multiple(true)687 .help("the root directory for the filesystem search (optional)")688 .long_help(689 "The directory where the filesystem search is rooted (optional). If \690 omitted, search the current working directory.",691 ),692 )693 .arg(694 Arg::with_name("search-path")695 .long("search-path")696 .takes_value(true)697 .conflicts_with("path")698 .multiple(true)699 .hidden_short_help(true)700 .number_of_values(1)701 .help("Provide paths to search as an alternative to the positional <path>")702 .long_help(703 "Provide paths to search as an alternative to the positional <path> \704 argument. Changes the usage to `fd [FLAGS/OPTIONS] --search-path <path> \705 --search-path <path2> [<pattern>]`",706 ),707 )708 .arg(709 Arg::with_name("strip-cwd-prefix")710 .long("strip-cwd-prefix")711 .conflicts_with_all(&["path", "search-path"])712 .hidden_short_help(true)713 .help("strip './' prefix from non-tty outputs")714 .long_help(715 "By default, relative paths are prefixed with './' when the output goes to a non \716 interactive terminal (TTY). Use this flag to disable this behaviour."717 )718 );719720 if cfg!(unix) {721 app = app.arg(722 Arg::with_name("owner")723 .long("owner")724 .short("o")725 .takes_value(true)726 .value_name("user:group")727 .help("Filter by owning user and/or group")728 .long_help(729 "Filter files by their user and/or group. \730 Format: [(user|uid)][:(group|gid)]. Either side is optional. \731 Precede either side with a '!' to exclude files instead.\n\732 Examples:\n \733 --owner john\n \734 --owner :students\n \735 --owner '!john:students'",736 ),737 );738 }739740 // Make `--one-file-system` available only on Unix and Windows platforms, as per the741 // restrictions on the corresponding option in the `ignore` crate.742 // Provide aliases `mount` and `xdev` for people coming from `find`.743 if cfg!(any(unix, windows)) {744 app = app.arg(745 Arg::with_name("one-file-system")746 .long("one-file-system")747 .aliases(&["mount", "xdev"])748 .hidden_short_help(true)749 .help("Do not descend into a different file system")750 .long_help(751 "By default, fd will traverse the file system tree as far as other options \752 dictate. With this flag, fd ensures that it does not descend into a \753 different file system than the one it started in. Comparable to the -mount \754 or -xdev filters of find(1).",755 ),756 );757 }758759 app760}