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
use ::clap::StructOpt;
use ::regex::Regex;
#[derive(StructOpt, Debug)]
#[structopt(
name = "grab",
about = "Filter lines by regular expression, keeping only the matching capture group."
)]
pub struct GrabArgs {
/// Regular expression to match. Returns the capture group if any, or the whole match otherwise.
#[structopt()]
pub pattern: Regex,
#[structopt(short = 'f', long = "first-match-only")]
/// Only print the first match of the pattern per line, even if it matches multiple times.
///
/// Note the difference with --first-capture-only, see help note there.
pub first_match_only: bool,
/// Only print the first capture group per pattern match, even if there are multiple groups in the pattern.
/// {n}* '(a+)(b+)?' matches twice in 'aaba' with one capture each.
/// {n}* '(a+)(b+)?' matches once in 'aabcdef' but has two captures.
#[structopt(short = '1', long = "first_capture_only")]
pub first_capture_only: bool,
/// Keep the full line if it does not match the pattern
#[structopt(short = 'k', long)]
pub keep_unmatched: bool,
/// Maximum number of matching lines
#[structopt(short = 'n', long)]
pub max_lines: Option<u32>,
}
impl Default for GrabArgs {
fn default() -> Self {
GrabArgs {
pattern: Regex::new(".*").unwrap(),
first_match_only: false,
first_capture_only: false,
keep_unmatched: false,
max_lines: None,
}
}
}
#[async_std::test]
async fn test_cli_args() {
use clap::IntoApp;
GrabArgs::into_app().debug_assert()
}