Skip to content

Commit

Permalink
Use clap's overrides_with and default_value_if
Browse files Browse the repository at this point in the history
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 clap-rs/clap#970
and clap-rs/clap#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.
  • Loading branch information
ericbn authored and BurntSushi committed Jun 12, 2017
1 parent ab70815 commit f2d1c58
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
11 changes: 6 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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))
Expand Down
12 changes: 5 additions & 7 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f2d1c58

Please sign in to comment.