-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathargs.rs
428 lines (388 loc) · 13.6 KB
/
args.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use crate::{
editor::Editor,
ui::{context_viewer::ContextViewerPosition, theme::ThemeVariant},
};
use clap::{ArgGroup, CommandFactory, Parser};
use std::{
ffi::OsString,
fs::File,
io::{self, BufRead, BufReader},
iter::once,
path::PathBuf,
};
pub const IGREP_CUSTOM_EDITOR_ENV: &str = "IGREP_CUSTOM_EDITOR";
pub const IGREP_EDITOR_ENV: &str = "IGREP_EDITOR";
pub const EDITOR_ENV: &str = "EDITOR";
pub const RIPGREP_CONFIG_PATH_ENV: &str = "RIPGREP_CONFIG_PATH";
pub const VISUAL_ENV: &str = "VISUAL";
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
#[clap(group(
ArgGroup::new("pattern_or_type_list")
.args(&["pattern", "type-list"])
.required(true)
))]
pub struct Args {
/// Regular expression used for searching.
pub pattern: Option<String>,
/// Files or directories to search. Directories are searched recursively.
/// If not specified, searching starts from current directory.
pub paths: Vec<PathBuf>,
#[clap(flatten)]
pub editor: EditorOpt,
/// UI color theme.
#[clap(long, arg_enum, default_value_t = ThemeVariant::Dark)]
pub theme: ThemeVariant,
/// Searches case insensitively.
#[clap(short = 'i', long)]
pub ignore_case: bool,
/// Searches case insensitively if the pattern is all lowercase.
/// Search case sensitively otherwise.
#[clap(short = 'S', long)]
pub smart_case: bool,
/// Search hidden files and directories.
/// By default, hidden files and directories are skipped.
#[clap(short = '.', long = "hidden")]
pub search_hidden: bool,
/// Follow symbolic links while traversing directories.
#[clap(short = 'L', long = "follow")]
pub follow_links: bool,
/// Only show matches surrounded by word boundaries.
#[clap(short = 'w', long = "word-regexp")]
pub word_regexp: bool,
/// Include files and directories for searching that match the given glob.
/// Multiple globs may be provided.
#[clap(short, long)]
pub glob: Vec<String>,
/// Show all supported file types and their corresponding globs.
#[clap(long)]
pub type_list: bool,
/// Only search files matching TYPE. Multiple types may be provided.
#[clap(short = 't', long = "type")]
pub type_matching: Vec<String>,
/// Do not search files matching TYPE-NOT. Multiple types-not may be provided.
#[clap(short = 'T', long)]
pub type_not: Vec<String>,
/// Context viewer position at startup
#[clap(long, value_enum, default_value_t = ContextViewerPosition::None)]
pub context_viewer: ContextViewerPosition,
}
#[derive(Parser, Debug)]
#[clap(group(
ArgGroup::new("editor_command")
.args(&["editor", "custom-command"])
))]
pub struct EditorOpt {
/// Text editor used to open selected match.
#[clap(long, arg_enum)]
pub editor: Option<Editor>,
/// Custom command used to open selected match. Must contain {file_name} and {line_number} tokens.
#[clap(long, env = IGREP_CUSTOM_EDITOR_ENV)]
pub custom_command: Option<String>,
}
impl Args {
pub fn parse_cli_and_config_file() -> Self {
// first validate if CLI arguments are valid
Args::parse_from(std::env::args_os());
let mut args_os: Vec<_> = std::env::args_os().collect();
let to_ignore = args_os
.iter()
.filter_map(|arg| {
let arg = arg.to_str().expect("Not valid UTF-8");
arg.starts_with('-')
.then(|| arg.trim_start_matches('-').to_owned())
})
.collect::<Vec<_>>();
// then extend them with those from config file
args_os.extend(Self::parse_config_file(to_ignore));
Args::parse_from(args_os)
}
fn parse_config_file(to_ignore: Vec<String>) -> Vec<OsString> {
match std::env::var_os(RIPGREP_CONFIG_PATH_ENV) {
None => Vec::default(),
Some(config_path) => match File::open(config_path) {
Ok(file) => {
let supported_arguments = Self::collect_supported_arguments();
let to_ignore = Self::pair_ignored(to_ignore, &supported_arguments);
Self::parse_from_reader(file, supported_arguments, to_ignore)
}
Err(_) => Vec::default(),
},
}
}
fn pair_ignored(
to_ignore: Vec<String>,
supported_arguments: &[(Option<String>, Option<String>, bool)],
) -> Vec<String> {
to_ignore
.iter()
.filter(|i| {
supported_arguments
.iter()
.any(|arg| (arg.0.as_ref() == Some(i) || arg.1.as_ref() == Some(i)) && !arg.2)
})
.flat_map(|i| {
match supported_arguments
.iter()
.find(|arg| arg.0.as_ref() == Some(i) || arg.1.as_ref() == Some(i))
{
Some(arg) => Box::new(once(arg.0.clone()).chain(once(arg.1.clone())))
as Box<dyn Iterator<Item = _>>,
None => Box::new(once(None)),
}
})
.flatten()
.collect()
}
fn collect_supported_arguments() -> Vec<(Option<String>, Option<String>, bool)> {
Args::command()
.get_arguments()
.filter_map(|arg| match (arg.get_long(), arg.get_short()) {
(None, None) => None,
(l, s) => Some((
l.map(|l| l.to_string()),
s.map(|s| s.to_string()),
arg.is_multiple_occurrences_set(),
)),
})
.collect::<Vec<_>>()
}
fn parse_from_reader<R: io::Read>(
reader: R,
supported: Vec<(Option<String>, Option<String>, bool)>,
to_ignore: Vec<String>,
) -> Vec<OsString> {
let reader = BufReader::new(reader);
let mut ignore_next_line = false;
reader
.lines()
.filter_map(|line| {
let line = line.expect("Not valid UTF-8");
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
return None;
}
if let Some(long) = line.strip_prefix("--") {
ignore_next_line = false;
let long = long.split_terminator('=').next().expect("Empty line");
if supported.iter().any(|el| el.0 == Some(long.to_string()))
&& !to_ignore.contains(&long.to_owned())
{
return Some(OsString::from(line));
}
if !line.contains('=') {
ignore_next_line = true;
}
None
} else if let Some(short) = line.strip_prefix('-') {
ignore_next_line = false;
let short = short.split_terminator('=').next().expect("Empty line");
if supported.iter().any(|el| el.1 == Some(short.to_string()))
&& !to_ignore.contains(&short.to_owned())
{
return Some(OsString::from(line));
}
if !line.contains('=') {
ignore_next_line = true;
}
None
} else {
if ignore_next_line {
ignore_next_line = false;
return None;
}
Some(OsString::from(line))
}
})
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;
#[test]
fn ripgrep_example_config() {
let supported_args = vec![
(Some("glob".to_owned()), Some("g".to_owned()), true),
(Some("smart-case".to_owned()), None, false),
];
let input = "\
# Don't let ripgrep vomit really long lines to my terminal, and show a preview.
--max-columns=150
--max-columns-preview
# Add my 'web' type.
--type-add
web:*.{html,css,js}*
# Using glob patterns to include/exclude files or folders
-g=!git/*
# or
--glob
!git/*
# Set the colors.
--colors=line:none
--colors=line:style:bold
# Because who cares about case!?
--smart-case";
let args = Args::parse_from_reader(input.as_bytes(), supported_args, vec![])
.into_iter()
.map(|s| s.into_string().unwrap())
.collect::<Vec<_>>();
assert_eq!(args, ["-g=!git/*", "--glob", "!git/*", "--smart-case"]);
}
#[test]
fn trim_whitespaces() {
let supported_args = vec![(Some("sup".to_owned()), Some("s".to_owned()), false)];
let input = "\
# comment
--sup=value\n\r\
-s \n\
value
--unsup
# --comment
value
-s";
let args = Args::parse_from_reader(input.as_bytes(), supported_args, vec![])
.into_iter()
.map(|s| s.into_string().unwrap())
.collect::<Vec<_>>();
assert_eq!(args, ["--sup=value", "-s", "value", "-s"]);
}
#[test]
fn skip_line_after_ignored_option() {
let supported_args = vec![
(Some("aaa".to_owned()), Some("a".to_owned()), false),
(Some("bbb".to_owned()), Some("b".to_owned()), false),
];
let input = "\
--aaa
value
--bbb
value
";
let args = Args::parse_from_reader(
input.as_bytes(),
supported_args.clone(),
vec!["aaa".to_owned()],
)
.into_iter()
.map(|s| s.into_string().unwrap())
.collect::<Vec<_>>();
assert_eq!(args, ["--bbb", "value"]);
let input = "\
-a
value
-b
value
";
let args = Args::parse_from_reader(input.as_bytes(), supported_args, vec!["a".to_owned()])
.into_iter()
.map(|s| s.into_string().unwrap())
.collect::<Vec<_>>();
assert_eq!(args, ["-b", "value"]);
}
#[test]
fn do_not_skip_line_after_ignored_option_if_value_inline() {
let supported_args = vec![
(Some("aaa".to_owned()), Some("a".to_owned()), false),
(Some("bbb".to_owned()), Some("b".to_owned()), false),
];
let input = "\
--aaa=value
--bbb
value
";
let args = Args::parse_from_reader(
input.as_bytes(),
supported_args.clone(),
vec!["aaa".to_owned()],
)
.into_iter()
.map(|s| s.into_string().unwrap())
.collect::<Vec<_>>();
assert_eq!(args, ["--bbb", "value"]);
let input = "\
-a=value
-b
value
";
let args = Args::parse_from_reader(input.as_bytes(), supported_args, vec!["a".to_owned()])
.into_iter()
.map(|s| s.into_string().unwrap())
.collect::<Vec<_>>();
assert_eq!(args, ["-b", "value"]);
}
#[test]
fn do_not_skip_line_after_ignored_flag() {
let supported_args = vec![
(Some("aaa".to_owned()), Some("a".to_owned()), false),
(Some("bbb".to_owned()), Some("b".to_owned()), false),
];
let input = "\
--aaa
--bbb
value
";
let args = Args::parse_from_reader(
input.as_bytes(),
supported_args.clone(),
vec!["aaa".to_owned()],
)
.into_iter()
.map(|s| s.into_string().unwrap())
.collect::<Vec<_>>();
assert_eq!(args, ["--bbb", "value"]);
let input = "\
-a
-b
value
";
let args = Args::parse_from_reader(input.as_bytes(), supported_args, vec!["a".to_owned()])
.into_iter()
.map(|s| s.into_string().unwrap())
.collect::<Vec<_>>();
assert_eq!(args, ["-b", "value"]);
}
#[test]
fn pair_ignored() {
let to_ignore = Args::pair_ignored(
vec![
"a".to_owned(),
"bbb".to_owned(),
"ddd".to_owned(),
"e".to_owned(),
],
&vec![
(Some("aaa".to_owned()), Some("a".to_owned()), false),
(Some("bbb".to_owned()), Some("b".to_owned()), false),
(Some("ccc".to_owned()), Some("c".to_owned()), false),
(Some("ddd".to_owned()), None, false),
(None, Some("e".to_owned()), false),
],
);
let extended: HashSet<String> = HashSet::from_iter(to_ignore);
let expected: HashSet<String> = HashSet::from([
"aaa".to_owned(),
"a".to_owned(),
"bbb".to_owned(),
"b".to_owned(),
"ddd".to_owned(),
"e".to_owned(),
]);
assert_eq!(extended, expected);
}
#[test]
fn do_not_ignore_multi_value_options() {
let to_ignore = Args::pair_ignored(
vec!["aaa".to_owned(), "b".to_owned(), "c".to_owned()],
&[
(Some("aaa".to_owned()), Some("a".to_owned()), true),
(Some("bbb".to_owned()), Some("b".to_owned()), false),
(Some("ccc".to_owned()), Some("c".to_owned()), true),
],
);
let extended: HashSet<String> = HashSet::from_iter(to_ignore);
let expected: HashSet<String> = HashSet::from(["bbb".to_owned(), "b".to_owned()]);
assert_eq!(extended, expected);
}
}