fd/src/app.rs

260 lines
9.9 KiB
Rust
Raw Normal View History

2017-10-21 10:16:03 +02:00
// Copyright (c) 2017 fd developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.
use std::collections::HashMap;
2017-10-04 14:31:08 +02:00
use clap::{App, AppSettings, Arg};
struct Help {
short: &'static str,
long: &'static str,
}
macro_rules! doc {
2018-04-13 22:46:17 +02:00
($map:expr, $name:expr, $short:expr) => {
doc!($map, $name, $short, $short)
};
2018-04-13 22:46:17 +02:00
($map:expr, $name:expr, $short:expr, $long:expr) => {
2018-03-12 17:46:13 +01:00
$map.insert(
$name,
Help {
short: $short,
long: concat!($long, "\n "),
},
);
};
}
2017-10-04 14:31:08 +02:00
pub fn build_app() -> App<'static, 'static> {
let helps = usage();
let arg = |name| {
2018-01-01 12:16:43 +01:00
Arg::with_name(name)
.help(helps[name].short)
.long_help(helps[name].long)
};
2017-10-04 14:31:08 +02:00
App::new("fd")
2017-10-05 21:29:29 +02:00
.version(crate_version!())
2017-12-10 06:40:13 +01:00
.usage("fd [FLAGS/OPTIONS] [<pattern>] [<path>...]")
2017-10-05 21:29:29 +02:00
.setting(AppSettings::ColoredHelp)
.setting(AppSettings::DeriveDisplayOrder)
.arg(arg("hidden").long("hidden").short("H"))
.arg(arg("no-ignore").long("no-ignore").short("I"))
.arg(arg("no-ignore-vcs").long("no-ignore-vcs"))
2017-10-12 08:01:51 +02:00
.arg(
arg("rg-alias-hidden-ignore")
.short("u")
.multiple(true)
.hidden(true),
)
.arg(
arg("case-sensitive")
.long("case-sensitive")
.short("s")
.overrides_with("ignore-case"),
)
.arg(
arg("ignore-case")
.long("ignore-case")
.short("i")
.overrides_with("case-sensitive"),
)
2018-02-10 15:19:53 +01:00
.arg(
arg("fixed-strings")
.long("fixed-strings")
.short("F")
.alias("literal"),
)
.arg(arg("absolute-path").long("absolute-path").short("a"))
.arg(arg("follow").long("follow").short("L").alias("dereference"))
.arg(arg("full-path").long("full-path").short("p"))
.arg(arg("null_separator").long("print0").short("0"))
.arg(arg("depth").long("max-depth").short("d").takes_value(true))
2017-10-05 21:29:29 +02:00
.arg(
arg("file-type")
2017-10-04 14:31:08 +02:00
.long("type")
.short("t")
.multiple(true)
.number_of_values(1)
2017-10-04 14:31:08 +02:00
.takes_value(true)
2017-10-05 21:35:22 +02:00
.value_name("filetype")
2018-03-25 16:36:37 +02:00
.possible_values(&[
"f",
"file",
"d",
"directory",
"l",
"symlink",
"x",
"executable",
])
.hide_possible_values(true),
2017-10-05 21:29:29 +02:00
)
.arg(
arg("extension")
2017-10-04 14:31:08 +02:00
.long("extension")
.short("e")
.multiple(true)
.number_of_values(1)
2017-10-04 14:31:08 +02:00
.takes_value(true)
.value_name("ext"),
2017-10-05 21:29:29 +02:00
)
.arg(
arg("exec")
.long("exec")
.short("x")
.min_values(1)
2017-11-03 01:39:03 +01:00
.allow_hyphen_values(true)
.value_terminator(";")
.value_name("cmd"),
)
.arg(
arg("exclude")
.long("exclude")
.short("E")
.takes_value(true)
.value_name("pattern")
.number_of_values(1)
.multiple(true),
)
2018-03-26 00:15:01 +02:00
.arg(
arg("ignore-file")
.long("ignore-file")
.takes_value(true)
.value_name("path")
.number_of_values(1)
.multiple(true),
)
2017-10-05 21:29:29 +02:00
.arg(
arg("color")
2017-10-04 14:31:08 +02:00
.long("color")
.short("c")
.takes_value(true)
2017-10-05 21:35:22 +02:00
.value_name("when")
2017-10-04 14:31:08 +02:00
.possible_values(&["never", "auto", "always"])
.hide_possible_values(true),
2017-10-05 21:29:29 +02:00
)
.arg(
arg("threads")
2017-10-04 14:31:08 +02:00
.long("threads")
.short("j")
.takes_value(true)
.value_name("num"),
2017-10-05 21:29:29 +02:00
)
.arg(
arg("max-buffer-time")
2017-10-04 14:31:08 +02:00
.long("max-buffer-time")
.takes_value(true)
.hidden(true),
2017-10-05 21:29:29 +02:00
)
.arg(arg("pattern"))
Add multiple path support (#182) * Adding support for multiple paths. (panic) - Started adding multiple file support - fd panics with multiple files right now * Moved the ctrlc handler to main. - Moved the ctrlc handler to main so we can search multiple files * Tests now allow custom directory setup - TestEnv::new() now takes two arguments, the directories to create and the files to create inside those directories. * rust-fmt changes * rust-fmt changes * Moving code around, no need to do everything in one big loop - PathDisplay was never actually used for anything, removed it during refactor of main - Removed redundant logic for absolute paths - Moved code placed needlessly inside a loop in the last commit outside of that loop. * Moving code around, no need to do everything in one big loop - PathDisplay was never actually used for anything, removed it during refactor of main - Removed redundant logic for absolute paths - Moved code placed needlessly inside a loop in the last commit outside of that loop. * Removed commented code in testenv * Refactored walk::scan to accept the path buffer vector. Using the ParallelWalker allows for multithreaded searching of multiple directories * Moved ctrlc handler back into walker, it is only called once from main now. * Moved the colored output check back to it's original place * Removing shell-escape, not sure how it got added... * Added test for `fd 'a.foo' test1` to show that a.foo is only found in the test1 and not the test2 direcotry * Removing side effect from walk::scan, `dir_vec` is no longer a mutable reference and an iterator is being used instead. * Running rustfmt to format code correctly
2017-12-06 23:52:23 +01:00
.arg(arg("path").multiple(true))
}
#[cfg_attr(rustfmt, rustfmt_skip)]
fn usage() -> HashMap<&'static str, Help> {
let mut h = HashMap::new();
doc!(h, "hidden"
, "Search hidden files and directories"
2017-10-07 15:15:30 +02:00
, "Include hidden directories and files in the search results (default: hidden files \
and directories are skipped).");
doc!(h, "no-ignore"
2018-03-26 10:25:33 +02:00
, "Do not respect .(git|fd)ignore files"
2017-10-07 15:15:30 +02:00
, "Show search results from files and directories that would otherwise be ignored by \
2018-03-26 10:25:33 +02:00
'.gitignore' or '.fdignore' files.");
doc!(h, "no-ignore-vcs"
, "Do not respect .gitignore files"
, "Show search results from files and directories that would otherwise be ignored by \
'.gitignore' files.");
doc!(h, "case-sensitive"
, "Case-sensitive search (default: smart case)"
2017-10-07 15:15:30 +02:00
, "Perform a case-sensitive search. By default, fd uses case-insensitive searches, \
unless the pattern contains an uppercase character (smart case).");
2017-10-12 01:21:44 +02:00
doc!(h, "ignore-case"
, "Case-insensitive search (default: smart case)"
, "Perform a case-insensitive search. By default, fd uses case-insensitive searches, \
unless the pattern contains an uppercase character (smart case).");
2018-02-10 15:19:53 +01:00
doc!(h, "fixed-strings"
, "Treat the pattern as a literal string"
, "Treat the pattern as a literal string instead of a regular expression.");
doc!(h, "absolute-path"
, "Show absolute instead of relative paths"
2017-10-07 15:15:30 +02:00
, "Shows the full path starting from the root as opposed to relative paths.");
doc!(h, "follow"
, "Follow symbolic links"
, "By default, fd does not descend into symlinked directories. Using this flag, symbolic \
2017-10-07 15:15:30 +02:00
links are also traversed.");
doc!(h, "full-path"
, "Search full path (default: file-/dirname only)"
2017-10-07 15:15:30 +02:00
, "By default, the search pattern is only matched against the filename (or directory \
name). Using this flag, the pattern is matched against the full path.");
doc!(h, "null_separator"
, "Separate results by the null character"
2017-10-07 15:15:30 +02:00
, "Separate search results by the null character (instead of newlines). Useful for \
piping results to 'xargs'.");
doc!(h, "depth"
, "Set maximum search depth (default: none)"
2017-10-07 15:15:30 +02:00
, "Limit the directory traversal to a given depth. By default, there is no limit \
on the search depth.");
doc!(h, "file-type"
2018-03-26 10:25:33 +02:00
, "Filter by type: file (f), directory (d), symlink (l),\nexecutable (x)"
, "Filter the search by type (multiple allowable filetypes can be specified):\n \
2017-10-07 15:15:30 +02:00
'f' or 'file': regular files\n \
'd' or 'directory': directories\n \
'l' or 'symlink': symbolic links\n \
'x' or 'executable': executables");
doc!(h, "extension"
, "Filter by file extension"
, "(Additionally) filter search results by their file extension. Multiple allowable file \
extensions can be specified.");
2017-10-14 18:04:11 +02:00
doc!(h, "exec"
, "Execute a command for each search result"
, "Execute a command for each search result.\n\
2017-11-15 22:07:04 +01:00
All arguments following --exec are taken to be arguments to the command until the \
argument ';' is encountered.\n\
Each occurrence of the following placeholders is substituted by a path derived from the \
current search result before the command is executed:\n \
'{}': path\n \
'{/}': basename\n \
'{//}': parent directory\n \
'{.}': path without file extension\n \
'{/.}': basename without file extension");
doc!(h, "exclude"
2018-01-03 10:28:34 +01:00
, "Exclude entries that match the given glob pattern"
, "Exclude files/directories that match the given glob pattern. This overrides any \
other ignore logic. Multiple exclude patterns can be specified.");
2018-03-26 00:15:01 +02:00
doc!(h, "ignore-file"
, "Add a custom ignore-file in .gitignore format"
2018-03-26 10:25:33 +02:00
, "Add a custom ignore-file in '.gitignore' format. These files have a low precedence.");
doc!(h, "color"
2017-10-07 15:15:30 +02:00
, "When to use colors: never, *auto*, always"
, "Declare when to use color for the pattern match output:\n \
'auto': show colors if the output goes to an interactive console (default)\n \
'never': do not use colorized output\n \
'always': always use colorized output");
doc!(h, "threads"
2017-10-14 18:04:11 +02:00
, "Set number of threads to use for searching & executing"
2017-10-14 20:04:04 +02:00
, "Set number of threads to use for searching & executing (default: number of available \
CPU cores)");
doc!(h, "max-buffer-time"
, "the time (in ms) to buffer, before streaming to the console"
, "Amount of time in milliseconds to buffer, before streaming the search results to\
2017-10-07 15:15:30 +02:00
the console.");
doc!(h, "pattern"
, "the search pattern, a regular expression (optional)");
doc!(h, "path"
, "the root directory for the filesystem search (optional)"
2017-10-07 15:15:30 +02:00
, "The directory where the filesystem search is rooted (optional). \
If omitted, search the current working directory.");
doc!(h, "rg-alias-hidden-ignore"
, "Alias for no-ignore and/or hidden"
, "Alias for no-ignore ('u') and no-ignore and hidden ('uu')");
h
2017-10-05 21:29:29 +02:00
}