Skip to content

Commit 3d398b3

Browse files
epagesharkdp
authored andcommitted
refactor: Switch from is_present to actions
Because "diff" is conditionally present, we need to check for it to avoid some of clap's stricter checks meant to prevent bugs.
1 parent 6099f2c commit 3d398b3

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

src/bin/bat/app.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl App {
8585
Some("auto") | None => {
8686
// If we have -pp as an option when in auto mode, the pager should be disabled.
8787
let extra_plain = self.matches.get_count("plain") > 1;
88-
if extra_plain || self.matches.is_present("no-paging") {
88+
if extra_plain || self.matches.get_flag("no-paging") {
8989
PagingMode::Never
9090
} else if inputs.iter().any(Input::is_stdin) {
9191
// If we are reading from stdin, only enable paging if we write to an
@@ -153,13 +153,13 @@ impl App {
153153
.get_one::<String>("language")
154154
.map(|s| s.as_str())
155155
.or_else(|| {
156-
if self.matches.is_present("show-all") {
156+
if self.matches.get_flag("show-all") {
157157
Some("show-nonprintable")
158158
} else {
159159
None
160160
}
161161
}),
162-
show_nonprintable: self.matches.is_present("show-all"),
162+
show_nonprintable: self.matches.get_flag("show-all"),
163163
wrapping_mode: if self.interactive_output || maybe_term_width.is_some() {
164164
match self.matches.get_one::<String>("wrap").map(|s| s.as_str()) {
165165
Some("character") => WrappingMode::Character,
@@ -178,7 +178,7 @@ impl App {
178178
// There's no point in wrapping when this is the case.
179179
WrappingMode::NoWrapping(false)
180180
},
181-
colored_output: self.matches.is_present("force-colorization")
181+
colored_output: self.matches.get_flag("force-colorization")
182182
|| match self.matches.get_one::<String>("color").map(|s| s.as_str()) {
183183
Some("always") => true,
184184
Some("never") => false,
@@ -194,7 +194,7 @@ impl App {
194194
.get_one::<String>("decorations")
195195
.map(|s| s.as_str())
196196
== Some("always")
197-
|| self.matches.is_present("force-colorization")),
197+
|| self.matches.get_flag("force-colorization")),
198198
tab_width: self
199199
.matches
200200
.get_one::<String>("tabs")
@@ -221,7 +221,7 @@ impl App {
221221
}
222222
})
223223
.unwrap_or_else(|| String::from(HighlightingAssets::default_theme())),
224-
visible_lines: match self.matches.is_present("diff") {
224+
visible_lines: match self.matches.contains_id("diff") && self.matches.get_flag("diff") {
225225
#[cfg(feature = "git")]
226226
true => VisibleLines::DiffContext(
227227
self.matches
@@ -255,7 +255,7 @@ impl App {
255255
.map(LineRanges::from)
256256
.map(HighlightedLineRanges)
257257
.unwrap_or_default(),
258-
use_custom_assets: !self.matches.is_present("no-custom-assets"),
258+
use_custom_assets: !self.matches.get_flag("no-custom-assets"),
259259
})
260260
}
261261

@@ -310,7 +310,7 @@ impl App {
310310
let mut styled_components = StyleComponents(
311311
if matches.get_one::<String>("decorations").map(|s| s.as_str()) == Some("never") {
312312
HashSet::new()
313-
} else if matches.is_present("number") {
313+
} else if matches.get_flag("number") {
314314
[StyleComponent::LineNumbers].iter().cloned().collect()
315315
} else if 0 < matches.get_count("plain") {
316316
[StyleComponent::Plain].iter().cloned().collect()

src/bin/bat/clap_app.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
6060
.long("show-all")
6161
.alias("show-nonprintable")
6262
.short('A')
63+
.action(ArgAction::SetTrue)
6364
.conflicts_with("language")
6465
.help("Show non-printable characters (space, tab, newline, ..).")
6566
.long_help(
@@ -137,6 +138,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
137138
Arg::new("diff")
138139
.long("diff")
139140
.short('d')
141+
.action(ArgAction::SetTrue)
140142
.conflicts_with("line-range")
141143
.help("Only show lines that have been added/removed/modified.")
142144
.long_help(
@@ -229,6 +231,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
229231
.long("number")
230232
.overrides_with("number")
231233
.short('n')
234+
.action(ArgAction::SetTrue)
232235
.help("Show line numbers (alias for '--style=numbers').")
233236
.long_help(
234237
"Only show line numbers, no other decorations. This is an alias for \
@@ -283,6 +286,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
283286
Arg::new("force-colorization")
284287
.long("force-colorization")
285288
.short('f')
289+
.action(ArgAction::SetTrue)
286290
.conflicts_with("color")
287291
.conflicts_with("decorations")
288292
.overrides_with("force-colorization")
@@ -314,6 +318,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
314318
.short('P')
315319
.long("no-paging")
316320
.alias("no-pager")
321+
.action(ArgAction::SetTrue)
317322
.overrides_with("no-paging")
318323
.hide(true)
319324
.hide_short_help(true)
@@ -379,6 +384,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
379384
.arg(
380385
Arg::new("list-themes")
381386
.long("list-themes")
387+
.action(ArgAction::SetTrue)
382388
.help("Display all supported highlighting themes.")
383389
.long_help("Display a list of supported themes for syntax highlighting."),
384390
)
@@ -469,6 +475,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
469475
Arg::new("list-languages")
470476
.long("list-languages")
471477
.short('L')
478+
.action(ArgAction::SetTrue)
472479
.conflicts_with("list-themes")
473480
.help("Display all supported languages.")
474481
.long_help("Display a list of supported languages for syntax highlighting."),
@@ -493,12 +500,14 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
493500
.arg(
494501
Arg::new("no-custom-assets")
495502
.long("no-custom-assets")
503+
.action(ArgAction::SetTrue)
496504
.hide(true)
497505
.help("Do not load custom assets"),
498506
)
499507
.arg(
500508
Arg::new("config-file")
501509
.long("config-file")
510+
.action(ArgAction::SetTrue)
502511
.conflicts_with("list-languages")
503512
.conflicts_with("list-themes")
504513
.hide(true)
@@ -507,6 +516,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
507516
.arg(
508517
Arg::new("generate-config-file")
509518
.long("generate-config-file")
519+
.action(ArgAction::SetTrue)
510520
.conflicts_with("list-languages")
511521
.conflicts_with("list-themes")
512522
.hide(true)
@@ -515,25 +525,29 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
515525
.arg(
516526
Arg::new("config-dir")
517527
.long("config-dir")
528+
.action(ArgAction::SetTrue)
518529
.hide(true)
519530
.help("Show bat's configuration directory."),
520531
)
521532
.arg(
522533
Arg::new("cache-dir")
523534
.long("cache-dir")
535+
.action(ArgAction::SetTrue)
524536
.hide(true)
525537
.help("Show bat's cache directory."),
526538
)
527539
.arg(
528540
Arg::new("diagnostic")
529541
.long("diagnostic")
530542
.alias("diagnostics")
543+
.action(ArgAction::SetTrue)
531544
.hide_short_help(true)
532545
.help("Show diagnostic information for bug reports.")
533546
)
534547
.arg(
535548
Arg::new("acknowledgements")
536549
.long("acknowledgements")
550+
.action(ArgAction::SetTrue)
537551
.hide_short_help(true)
538552
.help("Show acknowledgements."),
539553
)
@@ -551,6 +565,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
551565
Arg::new("build")
552566
.long("build")
553567
.short('b')
568+
.action(ArgAction::SetTrue)
554569
.help("Initialize (or update) the syntax/theme cache.")
555570
.long_help(
556571
"Initialize (or update) the syntax/theme cache by loading from \
@@ -561,6 +576,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
561576
Arg::new("clear")
562577
.long("clear")
563578
.short('c')
579+
.action(ArgAction::SetTrue)
564580
.help("Remove the cached syntax definitions and themes."),
565581
)
566582
.group(
@@ -586,13 +602,20 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
586602
"Use a different directory to store the cached syntax and theme set.",
587603
),
588604
)
589-
.arg(Arg::new("blank").long("blank").requires("build").help(
590-
"Create completely new syntax and theme sets \
605+
.arg(
606+
Arg::new("blank")
607+
.long("blank")
608+
.action(ArgAction::SetTrue)
609+
.requires("build")
610+
.help(
611+
"Create completely new syntax and theme sets \
591612
(instead of appending to the default sets).",
592-
))
613+
),
614+
)
593615
.arg(
594616
Arg::new("acknowledgements")
595617
.long("acknowledgements")
618+
.action(ArgAction::SetTrue)
596619
.requires("build")
597620
.help("Build acknowledgements.bin."),
598621
),

src/bin/bat/main.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ fn build_assets(matches: &clap::ArgMatches) -> Result<()> {
4949

5050
bat::assets::build(
5151
source_dir,
52-
!matches.is_present("blank"),
53-
matches.is_present("acknowledgements"),
52+
!matches.get_flag("blank"),
53+
matches.get_flag("acknowledgements"),
5454
target_dir,
5555
clap::crate_version!(),
5656
)
5757
}
5858

5959
fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
60-
if matches.is_present("build") {
60+
if matches.get_flag("build") {
6161
#[cfg(feature = "build-assets")]
6262
build_assets(matches)?;
6363
#[cfg(not(feature = "build-assets"))]
6464
println!("bat has been built without the 'build-assets' feature. The 'cache --build' option is not available.");
65-
} else if matches.is_present("clear") {
65+
} else if matches.get_flag("clear") {
6666
clear_assets();
6767
}
6868

@@ -286,7 +286,7 @@ fn invoke_bugreport(app: &App) {
286286
fn run() -> Result<bool> {
287287
let app = App::new()?;
288288

289-
if app.matches.is_present("diagnostic") {
289+
if app.matches.get_flag("diagnostic") {
290290
#[cfg(feature = "bugreport")]
291291
invoke_bugreport(&app);
292292
#[cfg(not(feature = "bugreport"))]
@@ -313,7 +313,7 @@ fn run() -> Result<bool> {
313313
let inputs = app.inputs()?;
314314
let config = app.config(&inputs)?;
315315

316-
if app.matches.is_present("list-languages") {
316+
if app.matches.get_flag("list-languages") {
317317
let languages: String = get_languages(&config)?;
318318
let inputs: Vec<Input> = vec![Input::from_reader(Box::new(languages.as_bytes()))];
319319
let plain_config = Config {
@@ -322,22 +322,22 @@ fn run() -> Result<bool> {
322322
..Default::default()
323323
};
324324
run_controller(inputs, &plain_config)
325-
} else if app.matches.is_present("list-themes") {
325+
} else if app.matches.get_flag("list-themes") {
326326
list_themes(&config)?;
327327
Ok(true)
328-
} else if app.matches.is_present("config-file") {
328+
} else if app.matches.get_flag("config-file") {
329329
println!("{}", config_file().to_string_lossy());
330330
Ok(true)
331-
} else if app.matches.is_present("generate-config-file") {
331+
} else if app.matches.get_flag("generate-config-file") {
332332
generate_config_file()?;
333333
Ok(true)
334-
} else if app.matches.is_present("config-dir") {
334+
} else if app.matches.get_flag("config-dir") {
335335
writeln!(io::stdout(), "{}", config_dir())?;
336336
Ok(true)
337-
} else if app.matches.is_present("cache-dir") {
337+
} else if app.matches.get_flag("cache-dir") {
338338
writeln!(io::stdout(), "{}", cache_dir())?;
339339
Ok(true)
340-
} else if app.matches.is_present("acknowledgements") {
340+
} else if app.matches.get_flag("acknowledgements") {
341341
writeln!(io::stdout(), "{}", bat::assets::get_acknowledgements())?;
342342
Ok(true)
343343
} else {

0 commit comments

Comments
 (0)