Skip to content

Commit 08c91a1

Browse files
epagesharkdp
authored andcommitted
refactor: Parse, don't validate
1 parent 3d398b3 commit 08c91a1

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/bin/bat/clap_app.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
152152
.overrides_with("diff-context")
153153
.takes_value(true)
154154
.value_name("N")
155-
.validator(
156-
|n| {
155+
.value_parser(
156+
|n: &str| {
157157
n.parse::<usize>()
158158
.map_err(|_| "must be a number")
159-
.map(|_| ()) // Convert to Result<(), &str>
159+
.map(|_| n.to_owned()) // Convert to Result<String, &str>
160160
.map_err(|e| e.to_string())
161161
}, // Convert to Result<(), String>
162162
)
@@ -173,11 +173,11 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
173173
.overrides_with("tabs")
174174
.takes_value(true)
175175
.value_name("T")
176-
.validator(
177-
|t| {
176+
.value_parser(
177+
|t: &str| {
178178
t.parse::<u32>()
179179
.map_err(|_t| "must be a number")
180-
.map(|_t| ()) // Convert to Result<(), &str>
180+
.map(|_t| t.to_owned()) // Convert to Result<String, &str>
181181
.map_err(|e| e.to_string())
182182
}, // Convert to Result<(), String>
183183
)
@@ -208,15 +208,15 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
208208
.value_name("width")
209209
.hide_short_help(true)
210210
.allow_hyphen_values(true)
211-
.validator(
212-
|t| {
211+
.value_parser(
212+
|t: &str| {
213213
let is_offset = t.starts_with('+') || t.starts_with('-');
214214
t.parse::<i32>()
215215
.map_err(|_e| "must be an offset or number")
216216
.and_then(|v| if v == 0 && !is_offset {
217217
Err("terminal width cannot be zero")
218218
} else {
219-
Ok(())
219+
Ok(t.to_owned())
220220
})
221221
.map_err(|e| e.to_string())
222222
})
@@ -400,7 +400,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
400400
.overrides_with("plain")
401401
.overrides_with("number")
402402
// Cannot use claps built in validation because we have to turn off clap's delimiters
403-
.validator(|val| {
403+
.value_parser(|val: &str| {
404404
let mut invalid_vals = val.split(',').filter(|style| {
405405
!&[
406406
"auto",
@@ -422,7 +422,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
422422
if let Some(invalid) = invalid_vals.next() {
423423
Err(format!("Unknown style, '{}'", invalid))
424424
} else {
425-
Ok(())
425+
Ok(val.to_owned())
426426
}
427427
})
428428
.help(

0 commit comments

Comments
 (0)