argv: tweak the meaning of zero

This commit makes a small tweak to the --max-columns flag. Namely, if
the value of the flag is 0, then ripgrep behaves as-if the flag were
absent.

This is useful in the context of ripgrep reading configuration from the
environment. For example, an end user might set --max-columns=150, but we
should permit the user to disable this setting when needed. Using -M0 is
a nice way to do that.

We do this because a zero value for --max-columns isn't particularly
meaningful. We do leave the --max-count, --max-filesize and --maxdepth
flags alone though, since a zero value for those flags is potentially
meaningful. (--max-count even has tests for ripgrep's behavior when
given a value of 0.)
This commit is contained in:
Andrew Gallant
2018-02-04 11:41:06 -05:00
parent 8cb5833ef9
commit 224c112e05
2 changed files with 22 additions and 2 deletions

View File

@@ -381,8 +381,8 @@ impl<'a> ArgMatches<'a> {
line_number: line_number,
line_number_width: try!(self.usize_of("line-number-width")),
line_per_match: self.is_present("vimgrep"),
max_columns: self.usize_of("max-columns")?,
max_count: self.usize_of("max-count")?.map(|max| max as u64),
max_columns: self.usize_of_nonzero("max-columns")?,
max_count: self.usize_of("max-count")?.map(|n| n as u64),
max_filesize: self.max_filesize()?,
maxdepth: self.usize_of("maxdepth")?,
mmap: mmap,
@@ -971,6 +971,24 @@ impl<'a> ArgMatches<'a> {
self.values_of_lossy(name).unwrap_or_else(Vec::new)
}
/// Safely reads an arg value with the given name, and if it's present,
/// tries to parse it as a usize value.
///
/// If the number is zero, then it is considered absent and `None` is
/// returned.
fn usize_of_nonzero(&self, name: &str) -> Result<Option<usize>> {
match self.value_of_lossy(name) {
None => Ok(None),
Some(v) => v.parse().map_err(From::from).map(|n| {
if n == 0 {
None
} else {
Some(n)
}
}),
}
}
/// Safely reads an arg value with the given name, and if it's present,
/// tries to parse it as a usize value.
fn usize_of(&self, name: &str) -> Result<Option<usize>> {