summaryrefslogtreecommitdiffstats
path: root/src/args.rs
diff options
context:
space:
mode:
authorAndrew Gallant <jamslam@gmail.com>2018-02-03 20:33:52 -0500
committerAndrew Gallant <jamslam@gmail.com>2018-02-04 10:40:20 -0500
commitc57d0fb4e862005ca0c7b79fb90a31a11e022ff7 (patch)
tree5af2b3c6e5426dd30ba55e5250c4e2ddab1f920b /src/args.rs
parentd83bab4d3f29a0176a20ea004c2cba44058d4210 (diff)
config: add persistent configuration
This commit adds support for reading configuration files that change ripgrep's default behavior. The format of the configuration file is an "rc" style and is very simple. It is defined by two rules: 1. Every line is a shell argument, after trimming ASCII whitespace. 2. Lines starting with '#' (optionally preceded by any amount of ASCII whitespace) are ignored. ripgrep will look for a single configuration file if and only if the RIPGREP_CONFIG_PATH environment variable is set and is non-empty. ripgrep will parse shell arguments from this file on startup and will behave as if the arguments in this file were prepended to any explicit arguments given to ripgrep on the command line. For example, if your ripgreprc file contained a single line: --smart-case then the following command RIPGREP_CONFIG_PATH=wherever/.ripgreprc rg foo would behave identically to the following command rg --smart-case foo This commit also adds a new flag, --no-config, that when present will suppress any and all support for configuration. This includes any future support for auto-loading configuration files from pre-determined paths (which this commit does not add). Conflicts between configuration files and explicit arguments are handled exactly like conflicts in the same command line invocation. That is, this command: RIPGREP_CONFIG_PATH=wherever/.ripgreprc rg foo --case-sensitive is exactly equivalent to rg --smart-case foo --case-sensitive in which case, the --case-sensitive flag would override the --smart-case flag. Closes #196
Diffstat (limited to 'src/args.rs')
-rw-r--r--src/args.rs47
1 files changed, 45 insertions, 2 deletions
diff --git a/src/args.rs b/src/args.rs
index df7eeb8f..d0990fdc 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -25,6 +25,7 @@ use printer::{ColorSpecs, Printer};
use unescape::unescape;
use worker::{Worker, WorkerBuilder};
+use config;
use logger::Logger;
use Result;
@@ -88,17 +89,59 @@ impl Args {
///
/// Also, initialize a global logger.
pub fn parse() -> Result<Args> {
- let matches = app::app().get_matches();
+ // We parse the args given on CLI. This does not include args from
+ // the config. We use the CLI args as an initial configuration while
+ // trying to parse config files. If a config file exists and has
+ // arguments, then we re-parse argv, otherwise we just use the matches
+ // we have here.
+ let early_matches = ArgMatches(app::app().get_matches());
if let Err(err) = Logger::init() {
errored!("failed to initialize logger: {}", err);
}
+ if early_matches.is_present("debug") {
+ log::set_max_level(log::LevelFilter::Debug);
+ } else {
+ log::set_max_level(log::LevelFilter::Warn);
+ }
+
+ let matches = Args::matches(early_matches);
+ // The logging level may have changed if we brought in additional
+ // arguments from a configuration file, so recheck it and set the log
+ // level as appropriate.
if matches.is_present("debug") {
log::set_max_level(log::LevelFilter::Debug);
} else {
log::set_max_level(log::LevelFilter::Warn);
}
- ArgMatches(matches).to_args()
+ matches.to_args()
+ }
+
+ /// Run clap and return the matches. If clap determines a problem with the
+ /// user provided arguments (or if --help or --version are given), then an
+ /// error/usage/version will be printed and the process will exit.
+ ///
+ /// If there are no additional arguments from the environment (e.g., a
+ /// config file), then the given matches are returned as is.
+ fn matches(early_matches: ArgMatches<'static>) -> ArgMatches<'static> {
+ // If the end user says no config, then respect it.
+ if early_matches.is_present("no-config") {
+ debug!("not reading config files because --no-config is present");
+ return early_matches;
+ }
+ // If the user wants ripgrep to use a config file, then parse args
+ // from that first.
+ let mut args = config::args(early_matches.is_present("no-messages"));
+ if args.is_empty() {
+ return early_matches;
+ }
+ let mut cliargs = env::args_os();
+ if let Some(bin) = cliargs.next() {
+ args.insert(0, bin);
+ }
+ args.extend(cliargs);
+ debug!("final argv: {:?}", args);
+ ArgMatches(app::app().get_matches_from(args))
}
/// Returns true if ripgrep should print the files it will search and exit