Skip to content
Myr

app.rs

This file

Path

src/app.rs

212 lines · 8.2 KiB · Rust

Last changed by

Add support for exclude-patternssharkdpbefore adoption5ad69fb

History of this file

Take it

1// Copyright (c) 2017 fd developers2// Licensed under the Apache License, Version 2.03// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>4// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.89use std::collections::HashMap;1011use clap::{App, AppSettings, Arg};1213struct Help {14    short: &'static str,15    long: &'static str,16}1718macro_rules! doc {19    ($map:expr, $name:expr, $short:expr) => {20        doc!($map, $name, $short, $short)21    };22    ($map:expr, $name:expr, $short:expr, $long:expr) => {23        $map.insert($name, Help {24            short: $short,25            long: concat!($long, "\n ")26        });27    };28}2930pub fn build_app() -> App<'static, 'static> {31    let helps = usage();32    let arg = |name| {33        Arg::with_name(name).help(helps[name].short).long_help(34            helps[name].long,35        )36    };3738    App::new("fd")39        .version(crate_version!())40        .usage("fd [FLAGS/OPTIONS] [<pattern>] [<path>]")41        .setting(AppSettings::ColoredHelp)42        .setting(AppSettings::DeriveDisplayOrder)43        .arg(arg("hidden").long("hidden").short("H"))44        .arg(arg("no-ignore").long("no-ignore").short("I"))45        .arg(46            arg("rg-alias-hidden-ignore")47                .short("u")48                .multiple(true)49                .hidden(true),50        )51        .arg(52            arg("case-sensitive")53                .long("case-sensitive")54                .short("s")55                .overrides_with("ignore-case"),56        )57        .arg(58            arg("ignore-case")59                .long("ignore-case")60                .short("i")61                .overrides_with("case-sensitive"),62        )63        .arg(arg("absolute-path").long("absolute-path").short("a"))64        .arg(arg("follow").long("follow").short("L").alias("dereference"))65        .arg(arg("full-path").long("full-path").short("p"))66        .arg(arg("null_separator").long("print0").short("0"))67        .arg(arg("depth").long("max-depth").short("d").takes_value(true))68        .arg(69            arg("file-type")70                .long("type")71                .short("t")72                .takes_value(true)73                .value_name("filetype")74                .possible_values(&["f", "file", "d", "directory", "l", "symlink"])75                .hide_possible_values(true),76        )77        .arg(78            arg("extension")79                .long("extension")80                .short("e")81                .takes_value(true)82                .value_name("ext"),83        )84        .arg(85            arg("exec")86                .long("exec")87                .short("x")88                .takes_value(true)89                .value_name("cmd"),90        )91        .arg(92            arg("exclude")93                .long("exclude")94                .short("E")95                .takes_value(true)96                .value_name("pattern")97                .number_of_values(1)98                .multiple(true),99        )100        .arg(101            arg("color")102                .long("color")103                .short("c")104                .takes_value(true)105                .value_name("when")106                .possible_values(&["never", "auto", "always"])107                .hide_possible_values(true),108        )109        .arg(110            arg("threads")111                .long("threads")112                .short("j")113                .takes_value(true)114                .value_name("num"),115        )116        .arg(117            arg("max-buffer-time")118                .long("max-buffer-time")119                .takes_value(true)120                .hidden(true),121        )122        .arg(arg("pattern"))123        .arg(arg("path"))124}125126#[cfg_attr(rustfmt, rustfmt_skip)]127fn usage() -> HashMap<&'static str, Help> {128    let mut h = HashMap::new();129    doc!(h, "hidden"130        , "Search hidden files and directories"131        , "Include hidden directories and files in the search results (default: hidden files \132           and directories are skipped).");133    doc!(h, "no-ignore"134        , "Do not respect .(git)ignore files"135        , "Show search results from files and directories that would otherwise be ignored by \136            '.*ignore' files.");137    doc!(h, "case-sensitive"138        , "Case-sensitive search (default: smart case)"139        , "Perform a case-sensitive search. By default, fd uses case-insensitive searches, \140           unless the pattern contains an uppercase character (smart case).");141    doc!(h, "ignore-case"142        , "Case-insensitive search (default: smart case)"143        , "Perform a case-insensitive search. By default, fd uses case-insensitive searches, \144           unless the pattern contains an uppercase character (smart case).");145    doc!(h, "absolute-path"146        , "Show absolute instead of relative paths"147        , "Shows the full path starting from the root as opposed to relative paths.");148    doc!(h, "follow"149        , "Follow symbolic links"150        , "By default, fd does not descend into symlinked directories. Using this flag, symbolic \151           links are also traversed.");152    doc!(h, "full-path"153        , "Search full path (default: file-/dirname only)"154        , "By default, the search pattern is only matched against the filename (or directory \155           name). Using this flag, the pattern is matched against the full path.");156    doc!(h, "null_separator"157        , "Separate results by the null character"158        , "Separate search results by the null character (instead of newlines). Useful for \159           piping results to 'xargs'.");160    doc!(h, "depth"161        , "Set maximum search depth (default: none)"162        , "Limit the directory traversal to a given depth. By default, there is no limit \163           on the search depth.");164    doc!(h, "file-type"165        , "Filter by type: f(ile), d(irectory), (sym)l(ink)"166        , "Filter the search by type:\n  \167             'f' or 'file':         regular files\n  \168             'd' or 'directory':    directories\n  \169             'l' or 'symlink':      symbolic links");170    doc!(h, "extension"171        , "Filter by file extension"172        , "(Additionally) filter search results by their file extension.");173    doc!(h, "exec"174        , "Execute the given command for each search result"175        , "Execute the given command for each search result.\n\176           The following are valid tokens that can be used within the expression for generating \177           commands:\n \178             '{}':   places the input in the location of this token\n \179             '{.}':  removes the extension from the input\n \180             '{/}':  places the basename of the input\n \181             '{//}': places the parent of the input\n \182             '{/.}': places the basename of the input, without the extension");183    doc!(h, "exclude"184        , "Exclude entries that match the given glob pattern."185        , "Exclude files/directories that match the given glob pattern. This overrides any \186           other ignore logic. Multiple exclude patterns can be specified.");187    doc!(h, "color"188        , "When to use colors: never, *auto*, always"189        , "Declare when to use color for the pattern match output:\n  \190             'auto':      show colors if the output goes to an interactive console (default)\n  \191             'never':     do not use colorized output\n  \192             'always':    always use colorized output");193    doc!(h, "threads"194        , "Set number of threads to use for searching & executing"195        , "Set number of threads to use for searching & executing (default: number of available \196           CPU cores)");197    doc!(h, "max-buffer-time"198        , "the time (in ms) to buffer, before streaming to the console"199        , "Amount of time in milliseconds to buffer, before streaming the search results to\200           the console.");201    doc!(h, "pattern"202        , "the search pattern, a regular expression (optional)");203    doc!(h, "path"204        , "the root directory for the filesystem search (optional)"205        , "The directory where the filesystem search is rooted (optional). \206           If omitted, search the current working directory.");207    doc!(h, "rg-alias-hidden-ignore"208        , "Alias for no-ignore and/or hidden"209        , "Alias for no-ignore ('u') and no-ignore and hidden ('uu')");210211    h212}