forked from crate-ci/typos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.rs
301 lines (250 loc) · 8.77 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
use clap::builder::TypedValueParser;
use clap::Parser;
use typos_cli::config;
#[derive(Debug, Copy, Clone, PartialEq, Eq, clap::ValueEnum, Default)]
pub enum Format {
Silent,
Brief,
#[default]
Long,
Json,
}
impl Format {
pub(crate) fn reporter(self) -> Box<dyn typos_cli::report::Report> {
match self {
Format::Silent => Box::new(crate::report::PrintSilent),
Format::Brief => Box::new(crate::report::PrintBrief),
Format::Long => Box::new(crate::report::PrintLong),
Format::Json => Box::new(crate::report::PrintJson),
}
}
}
#[derive(Debug, Parser)]
#[command(rename_all = "kebab-case")]
#[command(about, author, version)]
#[command(group = clap::ArgGroup::new("mode").multiple(false))]
pub(crate) struct Args {
/// Paths to check with `-` for stdin
#[arg(default_value = ".", group = "source")]
pub(crate) path: Vec<std::path::PathBuf>,
/// Read the list of newline separated paths from file or stdin (if `-`)
#[arg(long, group = "source")]
pub(crate) file_list: Option<std::path::PathBuf>,
/// The approximate number of threads to use.
#[arg(short = 'j', long = "threads", default_value = "0")]
pub(crate) threads: usize,
/// Sort results
#[arg(long)]
pub(crate) sort: bool,
/// Respect excluded files even for paths passed explicitly.
#[arg(long, help_heading = None)]
pub(crate) force_exclude: bool,
/// Custom config file
#[arg(short = 'c', long = "config", help_heading = "Config")]
pub(crate) custom_config: Option<std::path::PathBuf>,
/// Ignore implicit configuration files.
#[arg(long, help_heading = "Config")]
pub(crate) isolated: bool,
#[command(flatten, next_help_heading = "Config")]
pub(crate) config: ConfigArgs,
/// Print a diff of what would change
#[arg(long, group = "mode", help_heading = "Mode")]
pub(crate) diff: bool,
/// Write fixes out
#[arg(long, short = 'w', group = "mode", help_heading = "Mode")]
pub(crate) write_changes: bool,
/// Debug: Print each file that would be spellchecked.
#[arg(long, group = "mode", help_heading = "Mode")]
pub(crate) files: bool,
/// Debug: Print each file's type
#[arg(long, group = "mode", help_heading = "Mode")]
pub(crate) file_types: bool,
/// Debug: Print each identifier that would be spellchecked.
#[arg(long, group = "mode", help_heading = "Mode")]
pub(crate) identifiers: bool,
/// Debug: Print each word that would be spellchecked.
#[arg(long, group = "mode", help_heading = "Mode")]
pub(crate) words: bool,
/// Write the current configuration to file with `-` for stdout
#[arg(long, group = "mode", help_heading = "Mode")]
pub(crate) dump_config: Option<std::path::PathBuf>,
/// Show all supported file types.
#[arg(long, group = "mode", help_heading = "Mode")]
pub(crate) type_list: bool,
/// Render style for messages
#[arg(
long,
value_enum,
ignore_case = true,
default_value("long"),
help_heading = "Output"
)]
pub(crate) format: Format,
#[command(flatten, next_help_heading = "Output")]
pub(crate) color: colorchoice_clap::Color,
#[command(flatten, next_help_heading = "Output")]
pub(crate) verbose: clap_verbosity_flag::Verbosity,
}
#[derive(Debug, Clone, clap::Args)]
#[command(rename_all = "kebab-case")]
pub(crate) struct FileArgs {
/// Search binary files.
#[arg(long, overrides_with("no_binary"))]
binary: bool,
#[arg(long, overrides_with("binary"), hide = true)]
no_binary: bool,
/// Skip verifying spelling in file names.
#[arg(long, overrides_with("check_filenames"))]
no_check_filenames: bool,
#[arg(long, overrides_with("no_check_filenames"), hide = true)]
check_filenames: bool,
/// Skip verifying spelling in files.
#[arg(long, overrides_with("check_files"))]
no_check_files: bool,
#[arg(long, overrides_with("no_check_files"), hide = true)]
check_files: bool,
#[arg(long, overrides_with("no_unicode"), hide = true)]
unicode: bool,
/// Only allow ASCII characters in identifiers
#[arg(long, overrides_with("unicode"))]
no_unicode: bool,
/// Language locale to suggest corrections for
#[arg(long)]
#[arg(
value_parser = clap::builder::PossibleValuesParser::new(config::Locale::variants())
.map(|l| l.parse::<config::Locale>().unwrap())
)]
pub(crate) locale: Option<config::Locale>,
}
impl FileArgs {
pub fn to_config(&self) -> config::EngineConfig {
config::EngineConfig {
binary: self.binary(),
check_filename: self.check_filename(),
check_file: self.check_file(),
tokenizer: config::TokenizerConfig {
unicode: self.unicode(),
..Default::default()
},
dict: config::DictConfig {
locale: self.locale,
..Default::default()
},
extend_ignore_re: Default::default(),
}
}
fn binary(&self) -> Option<bool> {
resolve_bool_arg(self.binary, self.no_binary)
}
fn check_filename(&self) -> Option<bool> {
resolve_bool_arg(self.check_filenames, self.no_check_filenames)
}
fn unicode(&self) -> Option<bool> {
resolve_bool_arg(self.unicode, self.no_unicode)
}
fn check_file(&self) -> Option<bool> {
resolve_bool_arg(self.check_files, self.no_check_files)
}
}
#[derive(Debug, clap::Args)]
#[command(rename_all = "kebab-case")]
pub(crate) struct ConfigArgs {
#[command(flatten)]
walk: WalkArgs,
#[command(flatten)]
overrides: FileArgs,
}
impl ConfigArgs {
pub fn to_config(&self) -> config::Config {
config::Config {
files: self.walk.to_config(),
overrides: self.overrides.to_config(),
..Default::default()
}
}
}
#[derive(Debug, clap::Args)]
#[command(rename_all = "kebab-case")]
pub(crate) struct WalkArgs {
/// Ignore files & directories matching the glob.
#[arg(long, value_name = "GLOB")]
exclude: Vec<String>,
/// Search hidden files and directories.
#[arg(long, overrides_with("no_hidden"))]
hidden: bool,
#[arg(long, overrides_with("hidden"), hide = true)]
no_hidden: bool,
/// Don't respect ignore files.
#[arg(long, overrides_with("ignore"))]
no_ignore: bool,
#[arg(long, overrides_with("no_ignore"), hide = true)]
ignore: bool,
/// Don't respect .ignore files.
#[arg(long, overrides_with("ignore_dot"))]
no_ignore_dot: bool,
#[arg(long, overrides_with("no_ignore_dot"), hide = true)]
ignore_dot: bool,
/// Don't respect global ignore files.
#[arg(long, overrides_with("ignore_global"))]
no_ignore_global: bool,
#[arg(long, overrides_with("no_ignore_global"), hide = true)]
ignore_global: bool,
/// Don't respect ignore files in parent directories.
#[arg(long, overrides_with("ignore_parent"))]
no_ignore_parent: bool,
#[arg(long, overrides_with("no_ignore_parent"), hide = true)]
ignore_parent: bool,
/// Don't respect ignore files in vcs directories.
#[arg(long, overrides_with("ignore_vcs"))]
no_ignore_vcs: bool,
#[arg(long, overrides_with("no_ignore_vcs"), hide = true)]
ignore_vcs: bool,
}
impl WalkArgs {
pub fn to_config(&self) -> config::Walk {
config::Walk {
extend_exclude: self.exclude.clone(),
ignore_hidden: self.ignore_hidden(),
ignore_files: self.ignore_files(),
ignore_dot: self.ignore_dot(),
ignore_vcs: self.ignore_vcs(),
ignore_global: self.ignore_global(),
ignore_parent: self.ignore_parent(),
}
}
fn ignore_hidden(&self) -> Option<bool> {
resolve_bool_arg(self.no_hidden, self.hidden)
}
fn ignore_files(&self) -> Option<bool> {
resolve_bool_arg(self.ignore, self.no_ignore)
}
fn ignore_dot(&self) -> Option<bool> {
resolve_bool_arg(self.ignore_dot, self.no_ignore_dot)
}
fn ignore_vcs(&self) -> Option<bool> {
resolve_bool_arg(self.ignore_vcs, self.no_ignore_vcs)
}
fn ignore_global(&self) -> Option<bool> {
resolve_bool_arg(self.ignore_global, self.no_ignore_global)
}
fn ignore_parent(&self) -> Option<bool> {
resolve_bool_arg(self.ignore_parent, self.no_ignore_parent)
}
}
fn resolve_bool_arg(yes: bool, no: bool) -> Option<bool> {
match (yes, no) {
(true, false) => Some(true),
(false, true) => Some(false),
(false, false) => None,
(_, _) => unreachable!("clap should make this impossible"),
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn verify_app() {
use clap::CommandFactory;
Args::command().debug_assert()
}
}