1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-08-10 05:59:25 +02:00

Use clap's overrides_with and default_value_if

to better organize options. These are the changes:
- color will have default value of "never" if --vimgrep is given,
  and only if no --color option is given
- last overrides previous: --line-number and --no-line-number, --heading
  and --no-heading, --with-filename and --no-filename, and --vimgrep and
  --count
- no heading will be show if --vimgrep is defined. This worked inside
  vim actually because heading is also only shown if tty is stdout
  (which is not the case when rg is called from vim).

Unfortunately, clap does not behave like a usual GNU/POSIX in some
cases, as reported in https://github.com/kbknapp/clap-rs/issues/970
and https://github.com/kbknapp/clap-rs/issues/976 (having all the bells
and whistles, on the other hand). So we still have issues like rg
failing when same argument is given more than once (unless for the few
ones marked with `multiple(true)`), or having unintuitive precedence
rules (and probably non-intentional, just there because of clap's
limitations) like:
- --no-filename over --vimgrep
- --no-line-number over --column, --pretty or --vimgrep
- --no-heading over --pretty
regardless of the order in which options where given, where the desired
behavior would be that the last option would override the previous ones
given.
This commit is contained in:
Eric Nielsen
2017-06-01 18:13:43 -05:00
committed by Andrew Gallant
parent ab70815ea2
commit f2d1c582a8
2 changed files with 11 additions and 12 deletions

View File

@@ -80,7 +80,8 @@ pub fn app() -> App<'static, 'static> {
.value_name("WHEN")
.takes_value(true)
.hide_possible_values(true)
.possible_values(&["never", "auto", "always", "ansi"]))
.possible_values(&["never", "auto", "always", "ansi"])
.default_value_if("vimgrep", None, "never"))
.arg(flag("colors").value_name("SPEC")
.takes_value(true).multiple(true).number_of_values(1))
.arg(flag("encoding").short("E").value_name("ENCODING")
@@ -91,7 +92,7 @@ pub fn app() -> App<'static, 'static> {
.value_name("GLOB"))
.arg(flag("ignore-case").short("i"))
.arg(flag("line-number").short("n"))
.arg(flag("no-line-number").short("N"))
.arg(flag("no-line-number").short("N").overrides_with("line-number"))
.arg(flag("quiet").short("q"))
.arg(flag("type").short("t")
.takes_value(true).multiple(true).number_of_values(1)
@@ -125,8 +126,8 @@ pub fn app() -> App<'static, 'static> {
.arg(flag("files-with-matches").short("l"))
.arg(flag("files-without-match"))
.arg(flag("with-filename").short("H"))
.arg(flag("no-filename"))
.arg(flag("heading").overrides_with("no-heading"))
.arg(flag("no-filename").overrides_with("with-filename"))
.arg(flag("heading"))
.arg(flag("no-heading").overrides_with("heading"))
.arg(flag("hidden"))
.arg(flag("ignore-file")
@@ -160,7 +161,7 @@ pub fn app() -> App<'static, 'static> {
.arg(flag("threads")
.short("j").value_name("ARG").takes_value(true)
.validator(validate_number))
.arg(flag("vimgrep"))
.arg(flag("vimgrep").overrides_with("count"))
.arg(flag("max-columns").short("M")
.value_name("NUM").takes_value(true)
.validator(validate_number))

View File

@@ -589,9 +589,9 @@ impl<'a> ArgMatches<'a> {
false
} else {
let only_stdin = paths == &[Path::new("-")];
self.is_present("line-number")
(atty::is(atty::Stream::Stdout) && !only_stdin)
|| self.is_present("line-number")
|| self.is_present("column")
|| (atty::is(atty::Stream::Stdout) && !only_stdin)
|| self.is_present("pretty")
|| self.is_present("vimgrep")
}
@@ -605,11 +605,11 @@ impl<'a> ArgMatches<'a> {
/// Returns true if and only if matches should be grouped with file name
/// headings.
fn heading(&self) -> bool {
if self.is_present("no-heading") {
if self.is_present("no-heading") || self.is_present("vimgrep") {
false
} else {
self.is_present("heading")
|| atty::is(atty::Stream::Stdout)
atty::is(atty::Stream::Stdout)
|| self.is_present("heading")
|| self.is_present("pretty")
}
}
@@ -674,8 +674,6 @@ impl<'a> ArgMatches<'a> {
termcolor::ColorChoice::Always
} else if preference == "ansi" {
termcolor::ColorChoice::AlwaysAnsi
} else if self.is_present("vimgrep") {
termcolor::ColorChoice::Never
} else if preference == "auto" {
if atty::is(atty::Stream::Stdout) || self.is_present("pretty") {
termcolor::ColorChoice::Auto