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
use clap::Parser;
use std::ffi::OsString;
#[derive(Default, Parser, Debug)]
#[clap(author = "ccQpein", version, about)]
pub struct Args {
#[clap(short, long)]
pub(crate) filetypes: Vec<OsString>,
#[clap(short = 'x', long = "ignore-dir")]
pub(crate) ignore_dirs: Vec<OsString>,
#[clap(short, long)]
pub(crate) keywords: Option<Vec<String>>,
#[clap(short, long)]
pub(crate) jsonx: Option<String>,
#[clap(name = "files/dirs", default_value = ".")]
pub(crate) targets: Vec<String>,
#[clap(short = 'D', long = "del")]
pub(crate) delete: bool,
}
impl Args {
pub fn union(&mut self, other: Self) {
if other.filetypes.len() != 0 {
self.filetypes = other.filetypes
}
if other.ignore_dirs.len() != 0 {
self.ignore_dirs = other.ignore_dirs
}
if other.keywords.is_some() {
self.keywords = other.keywords
}
if other.jsonx.is_some() {
self.jsonx = other.jsonx
}
if other.targets.len() != 0 {
self.targets = other.targets;
}
if other.delete {
self.delete = other.delete
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
#[test]
fn test_parse_from_iter() {
let args = vec!["codeitlater", "-x", "dd"];
assert_eq!(Args::parse_from(args).ignore_dirs, vec!["dd"]);
let args = vec!["codeitlater", "-x", "dd", "-x", "ff"];
assert_eq!(Args::parse_from(args).ignore_dirs, vec!["dd", "ff"]);
let args = vec!["codeitlater", "-x", "dd", "--", "a", "b", "c"];
assert_eq!(
Args::parse_from(args).targets,
vec!["a".to_string(), "b".to_string(), "c".to_string()]
);
let args = vec!["codeitlater", "--", "a", "b", "c"];
assert_eq!(
Args::parse_from(args).targets,
vec!["a".to_string(), "b".to_string(), "c".to_string()]
);
let args = vec!["codeitlater", "a", "b", "c"];
assert_eq!(
Args::parse_from(args).targets,
vec!["a".to_string(), "b".to_string(), "c".to_string()]
);
let args = vec!["codeitlater", "--del", "--", "a", "b", "c"];
assert_eq!(
Args::parse_from(args).targets,
vec!["a".to_string(), "b".to_string(), "c".to_string()]
);
let args = vec!["codeitlater", "-x", "dd", "-x", "ff", "-D", "a", "b", "c"];
let args = Args::parse_from(args);
assert_eq!(
args.targets,
vec!["a".to_string(), "b".to_string(), "c".to_string()]
);
assert_eq!(args.delete, true);
assert_eq!(args.ignore_dirs, vec!["dd", "ff"]);
}
}