summaryrefslogtreecommitdiffstats
path: root/crates
diff options
context:
space:
mode:
authorAnthony Huang <anthony.huang@affirm.com>2021-05-25 21:41:11 -0400
committerAndrew Gallant <jamslam@gmail.com>2021-05-31 21:51:18 -0400
commit578e1992fa21d3612d3e613d8869dbbb16e33cbe (patch)
treed87e8e2aee7e4d0ad393b5d97023b6b6bd5b7da6 /crates
parent46d0130597a8340c19fdd41b107c209653451698 (diff)
cli: add --field-{context,match}-separator flags
These flags permit configuring the bytes used to delimit fields in match or context lines, where "fields" are things like the file path, line number, column number and the match/context itself. Fixes #1842, Closes #1871
Diffstat (limited to 'crates')
-rw-r--r--crates/core/app.rs34
-rw-r--r--crates/core/args.rs22
2 files changed, 54 insertions, 2 deletions
diff --git a/crates/core/app.rs b/crates/core/app.rs
index dd9ae2cd..5058c0ea 100644
--- a/crates/core/app.rs
+++ b/crates/core/app.rs
@@ -568,6 +568,8 @@ pub fn all_args_and_flags() -> Vec<RGArg> {
flag_dfa_size_limit(&mut args);
flag_encoding(&mut args);
flag_engine(&mut args);
+ flag_field_context_separator(&mut args);
+ flag_field_match_separator(&mut args);
flag_file(&mut args);
flag_files(&mut args);
flag_files_with_matches(&mut args);
@@ -1231,6 +1233,38 @@ This overrides previous uses of --pcre2 and --auto-hybrid-regex flags.
args.push(arg);
}
+fn flag_field_context_separator(args: &mut Vec<RGArg>) {
+ const SHORT: &str = "Set the field context separator.";
+ const LONG: &str = long!(
+ "\
+Set the field context separator, which is used to delimit file paths, line
+numbers, columns and the context itself, when printing contextual lines. The
+separator may be any number of bytes, including zero. Escape sequences like
+\\x7F or \\t may be used. The default value is -.
+"
+ );
+ let arg = RGArg::flag("field-context-separator", "SEPARATOR")
+ .help(SHORT)
+ .long_help(LONG);
+ args.push(arg);
+}
+
+fn flag_field_match_separator(args: &mut Vec<RGArg>) {
+ const SHORT: &str = "Set the match separator.";
+ const LONG: &str = long!(
+ "\
+Set the field match separator, which is used to delimit file paths, line
+numbers, columns and the match itself. The separator may be any number of
+bytes, including zero. Escape sequences like \\x7F or \\t may be used. The
+default value is -.
+"
+ );
+ let arg = RGArg::flag("field-match-separator", "SEPARATOR")
+ .help(SHORT)
+ .long_help(LONG);
+ args.push(arg);
+}
+
fn flag_file(args: &mut Vec<RGArg>) {
const SHORT: &str = "Search for patterns from the given file.";
const LONG: &str = long!(
diff --git a/crates/core/args.rs b/crates/core/args.rs
index 7a448362..caa378bd 100644
--- a/crates/core/args.rs
+++ b/crates/core/args.rs
@@ -786,8 +786,8 @@ impl ArgMatches {
.trim_ascii(self.is_present("trim"))
.separator_search(None)
.separator_context(self.context_separator())
- .separator_field_match(b":".to_vec())
- .separator_field_context(b"-".to_vec())
+ .separator_field_match(self.field_match_separator())
+ .separator_field_context(self.field_context_separator())
.separator_path(self.path_separator()?)
.path_terminator(self.path_terminator());
if separator_search {
@@ -1377,6 +1377,24 @@ impl ArgMatches {
}
}
+ /// Returns the unescaped field context separator. If one wasn't specified,
+ /// then '-' is used as the default.
+ fn field_context_separator(&self) -> Vec<u8> {
+ match self.value_of_os("field-context-separator") {
+ None => b"-".to_vec(),
+ Some(sep) => cli::unescape_os(&sep),
+ }
+ }
+
+ /// Returns the unescaped field match separator. If one wasn't specified,
+ /// then ':' is used as the default.
+ fn field_match_separator(&self) -> Vec<u8> {
+ match self.value_of_os("field-match-separator") {
+ None => b":".to_vec(),
+ Some(sep) => cli::unescape_os(&sep),
+ }
+ }
+
/// Get a sequence of all available patterns from the command line.
/// This includes reading the -e/--regexp and -f/--file flags.
///