refactor: Resolve basic clap deprecations

This commit is contained in:
Ed Page 2022-09-01 19:03:25 -05:00 committed by David Peter
parent 2323804f7e
commit 542b3d2317
1 changed files with 85 additions and 97 deletions

View File

@ -1,4 +1,4 @@
use clap::{crate_name, crate_version, App as ClapApp, AppSettings, Arg, ArgGroup, SubCommand}; use clap::{crate_name, crate_version, AppSettings, Arg, ArgGroup, ColorChoice, Command};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::env; use std::env;
use std::path::Path; use std::path::Path;
@ -16,22 +16,21 @@ static VERSION: Lazy<String> = Lazy::new(|| {
} }
}); });
pub fn build_app(interactive_output: bool) -> ClapApp<'static> { pub fn build_app(interactive_output: bool) -> Command<'static> {
let clap_color_setting = if interactive_output && env::var_os("NO_COLOR").is_none() { let color_when = if interactive_output && env::var_os("NO_COLOR").is_none() {
AppSettings::ColoredHelp ColorChoice::Auto
} else { } else {
AppSettings::ColorNever ColorChoice::Never
}; };
let mut app = ClapApp::new(crate_name!()) let mut app = Command::new(crate_name!())
.version(VERSION.as_str()) .version(VERSION.as_str())
.global_setting(clap_color_setting) .color(color_when)
.global_setting(AppSettings::DeriveDisplayOrder) .global_setting(AppSettings::DeriveDisplayOrder)
.global_setting(AppSettings::UnifiedHelpMessage) .hide_possible_values(true)
.global_setting(AppSettings::HidePossibleValuesInHelp) .args_conflicts_with_subcommands(true)
.setting(AppSettings::ArgsNegateSubcommands) .allow_external_subcommands(true)
.setting(AppSettings::AllowExternalSubcommands) .disable_help_subcommand(true)
.setting(AppSettings::DisableHelpSubcommand)
.max_term_width(100) .max_term_width(100)
.about( .about(
"A cat(1) clone with wings.\n\n\ "A cat(1) clone with wings.\n\n\
@ -43,19 +42,18 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
) )
.long_about("A cat(1) clone with syntax highlighting and Git integration.") .long_about("A cat(1) clone with syntax highlighting and Git integration.")
.arg( .arg(
Arg::with_name("FILE") Arg::new("FILE")
.help("File(s) to print / concatenate. Use '-' for standard input.") .help("File(s) to print / concatenate. Use '-' for standard input.")
.long_help( .long_help(
"File(s) to print / concatenate. Use a dash ('-') or no argument at all \ "File(s) to print / concatenate. Use a dash ('-') or no argument at all \
to read from standard input.", to read from standard input.",
) )
.takes_value(true) .takes_value(true)
.multiple(true) .multiple_values(true)
.empty_values(false)
.allow_invalid_utf8(true), .allow_invalid_utf8(true),
) )
.arg( .arg(
Arg::with_name("show-all") Arg::new("show-all")
.long("show-all") .long("show-all")
.alias("show-nonprintable") .alias("show-nonprintable")
.short('A') .short('A')
@ -68,7 +66,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("plain") Arg::new("plain")
.overrides_with("plain") .overrides_with("plain")
.overrides_with("number") .overrides_with("number")
.short('p') .short('p')
@ -82,7 +80,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("language") Arg::new("language")
.short('l') .short('l')
.long("language") .long("language")
.overrides_with("language") .overrides_with("language")
@ -96,12 +94,11 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
Arg::with_name("highlight-line") Arg::new("highlight-line")
.long("highlight-line") .long("highlight-line")
.short('H') .short('H')
.takes_value(true) .takes_value(true)
.number_of_values(1) .multiple_occurrences(true)
.multiple(true)
.value_name("N:M") .value_name("N:M")
.help("Highlight lines N through M.") .help("Highlight lines N through M.")
.long_help( .long_help(
@ -115,11 +112,10 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("file-name") Arg::new("file-name")
.long("file-name") .long("file-name")
.takes_value(true) .takes_value(true)
.number_of_values(1) .multiple_occurrences(true)
.multiple(true)
.value_name("name") .value_name("name")
.allow_invalid_utf8(true) .allow_invalid_utf8(true)
.help("Specify the name to display for a file.") .help("Specify the name to display for a file.")
@ -135,7 +131,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
{ {
app = app app = app
.arg( .arg(
Arg::with_name("diff") Arg::new("diff")
.long("diff") .long("diff")
.short('d') .short('d')
.conflicts_with("line-range") .conflicts_with("line-range")
@ -146,7 +142,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("diff-context") Arg::new("diff-context")
.long("diff-context") .long("diff-context")
.overrides_with("diff-context") .overrides_with("diff-context")
.takes_value(true) .takes_value(true)
@ -159,7 +155,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
}, // Convert to Result<(), String> }, // Convert to Result<(), String>
) )
.hidden_short_help(true) .hide_short_help(true)
.long_help( .long_help(
"Include N lines of context around added/removed/modified lines when using '--diff'.", "Include N lines of context around added/removed/modified lines when using '--diff'.",
), ),
@ -167,7 +163,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
} }
app = app.arg( app = app.arg(
Arg::with_name("tabs") Arg::new("tabs")
.long("tabs") .long("tabs")
.overrides_with("tabs") .overrides_with("tabs")
.takes_value(true) .takes_value(true)
@ -187,12 +183,12 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("wrap") Arg::new("wrap")
.long("wrap") .long("wrap")
.overrides_with("wrap") .overrides_with("wrap")
.takes_value(true) .takes_value(true)
.value_name("mode") .value_name("mode")
.possible_values(&["auto", "never", "character"]) .value_parser(["auto", "never", "character"])
.default_value("auto") .default_value("auto")
.hide_default_value(true) .hide_default_value(true)
.help("Specify the text-wrapping mode (*auto*, never, character).") .help("Specify the text-wrapping mode (*auto*, never, character).")
@ -201,11 +197,11 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
control the output width."), control the output width."),
) )
.arg( .arg(
Arg::with_name("terminal-width") Arg::new("terminal-width")
.long("terminal-width") .long("terminal-width")
.takes_value(true) .takes_value(true)
.value_name("width") .value_name("width")
.hidden_short_help(true) .hide_short_help(true)
.allow_hyphen_values(true) .allow_hyphen_values(true)
.validator( .validator(
|t| { |t| {
@ -226,7 +222,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("number") Arg::new("number")
.long("number") .long("number")
.overrides_with("number") .overrides_with("number")
.short('n') .short('n')
@ -237,12 +233,12 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("color") Arg::new("color")
.long("color") .long("color")
.overrides_with("color") .overrides_with("color")
.takes_value(true) .takes_value(true)
.value_name("when") .value_name("when")
.possible_values(&["auto", "never", "always"]) .value_parser(["auto", "never", "always"])
.hide_default_value(true) .hide_default_value(true)
.default_value("auto") .default_value("auto")
.help("When to use colors (*auto*, never, always).") .help("When to use colors (*auto*, never, always).")
@ -254,23 +250,23 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("italic-text") Arg::new("italic-text")
.long("italic-text") .long("italic-text")
.takes_value(true) .takes_value(true)
.value_name("when") .value_name("when")
.possible_values(&["always", "never"]) .value_parser(["always", "never"])
.default_value("never") .default_value("never")
.hide_default_value(true) .hide_default_value(true)
.help("Use italics in output (always, *never*)") .help("Use italics in output (always, *never*)")
.long_help("Specify when to use ANSI sequences for italic text in the output. Possible values: always, *never*."), .long_help("Specify when to use ANSI sequences for italic text in the output. Possible values: always, *never*."),
) )
.arg( .arg(
Arg::with_name("decorations") Arg::new("decorations")
.long("decorations") .long("decorations")
.overrides_with("decorations") .overrides_with("decorations")
.takes_value(true) .takes_value(true)
.value_name("when") .value_name("when")
.possible_values(&["auto", "never", "always"]) .value_parser(["auto", "never", "always"])
.default_value("auto") .default_value("auto")
.hide_default_value(true) .hide_default_value(true)
.help("When to show the decorations (*auto*, never, always).") .help("When to show the decorations (*auto*, never, always).")
@ -281,25 +277,25 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("force-colorization") Arg::new("force-colorization")
.long("force-colorization") .long("force-colorization")
.short('f') .short('f')
.conflicts_with("color") .conflicts_with("color")
.conflicts_with("decorations") .conflicts_with("decorations")
.overrides_with("force-colorization") .overrides_with("force-colorization")
.hidden_short_help(true) .hide_short_help(true)
.long_help("Alias for '--decorations=always --color=always'. This is useful \ .long_help("Alias for '--decorations=always --color=always'. This is useful \
if the output of bat is piped to another program, but you want \ if the output of bat is piped to another program, but you want \
to keep the colorization/decorations.") to keep the colorization/decorations.")
) )
.arg( .arg(
Arg::with_name("paging") Arg::new("paging")
.long("paging") .long("paging")
.overrides_with("paging") .overrides_with("paging")
.overrides_with("no-paging") .overrides_with("no-paging")
.takes_value(true) .takes_value(true)
.value_name("when") .value_name("when")
.possible_values(&["auto", "never", "always"]) .value_parser(["auto", "never", "always"])
.default_value("auto") .default_value("auto")
.hide_default_value(true) .hide_default_value(true)
.help("Specify when to use the pager, or use `-P` to disable (*auto*, never, always).") .help("Specify when to use the pager, or use `-P` to disable (*auto*, never, always).")
@ -311,22 +307,22 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("no-paging") Arg::new("no-paging")
.short('P') .short('P')
.long("no-paging") .long("no-paging")
.alias("no-pager") .alias("no-pager")
.overrides_with("no-paging") .overrides_with("no-paging")
.hidden(true) .hide(true)
.hidden_short_help(true) .hide_short_help(true)
.help("Alias for '--paging=never'") .help("Alias for '--paging=never'")
) )
.arg( .arg(
Arg::with_name("pager") Arg::new("pager")
.long("pager") .long("pager")
.overrides_with("pager") .overrides_with("pager")
.takes_value(true) .takes_value(true)
.value_name("command") .value_name("command")
.hidden_short_help(true) .hide_short_help(true)
.help("Determine which pager to use.") .help("Determine which pager to use.")
.long_help( .long_help(
"Determine which pager is used. This option will override the \ "Determine which pager is used. This option will override the \
@ -336,12 +332,11 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("map-syntax") Arg::new("map-syntax")
.short('m') .short('m')
.long("map-syntax") .long("map-syntax")
.multiple(true) .multiple_occurrences(true)
.takes_value(true) .takes_value(true)
.number_of_values(1)
.value_name("glob:syntax") .value_name("glob:syntax")
.help("Use the specified syntax for files matching the glob pattern ('*.cpp:C++').") .help("Use the specified syntax for files matching the glob pattern ('*.cpp:C++').")
.long_help( .long_help(
@ -354,19 +349,18 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
Arg::with_name("ignored-suffix") Arg::new("ignored-suffix")
.number_of_values(1) .multiple_occurrences(true)
.multiple(true)
.takes_value(true) .takes_value(true)
.long("ignored-suffix") .long("ignored-suffix")
.hidden_short_help(true) .hide_short_help(true)
.help( .help(
"Ignore extension. For example:\n \ "Ignore extension. For example:\n \
'bat --ignored-suffix \".dev\" my_file.json.dev' will use JSON syntax, and ignore '.dev'" 'bat --ignored-suffix \".dev\" my_file.json.dev' will use JSON syntax, and ignore '.dev'"
) )
) )
.arg( .arg(
Arg::with_name("theme") Arg::new("theme")
.long("theme") .long("theme")
.overrides_with("theme") .overrides_with("theme")
.takes_value(true) .takes_value(true)
@ -380,18 +374,18 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("list-themes") Arg::new("list-themes")
.long("list-themes") .long("list-themes")
.help("Display all supported highlighting themes.") .help("Display all supported highlighting themes.")
.long_help("Display a list of supported themes for syntax highlighting."), .long_help("Display a list of supported themes for syntax highlighting."),
) )
.arg( .arg(
Arg::with_name("style") Arg::new("style")
.long("style") .long("style")
.value_name("components") .value_name("components")
// Need to turn this off for overrides_with to work as we want. See the bottom most // Need to turn this off for overrides_with to work as we want. See the bottom most
// example at https://docs.rs/clap/2.32.0/clap/struct.Arg.html#method.overrides_with // example at https://docs.rs/clap/2.32.0/clap/struct.Arg.html#method.overrides_with
.use_delimiter(false) .use_value_delimiter(false)
.takes_value(true) .takes_value(true)
.overrides_with("style") .overrides_with("style")
.overrides_with("plain") .overrides_with("plain")
@ -451,12 +445,11 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("line-range") Arg::new("line-range")
.long("line-range") .long("line-range")
.short('r') .short('r')
.multiple(true) .multiple_occurrences(true)
.takes_value(true) .takes_value(true)
.number_of_values(1)
.value_name("N:M") .value_name("N:M")
.help("Only print the lines from N to M.") .help("Only print the lines from N to M.")
.long_help( .long_help(
@ -470,7 +463,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("list-languages") Arg::new("list-languages")
.long("list-languages") .long("list-languages")
.short('L') .short('L')
.conflicts_with("list-themes") .conflicts_with("list-themes")
@ -478,10 +471,10 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
.long_help("Display a list of supported languages for syntax highlighting."), .long_help("Display a list of supported languages for syntax highlighting."),
) )
.arg( .arg(
Arg::with_name("unbuffered") Arg::new("unbuffered")
.short('u') .short('u')
.long("unbuffered") .long("unbuffered")
.hidden_short_help(true) .hide_short_help(true)
.long_help( .long_help(
"This option exists for POSIX-compliance reasons ('u' is for \ "This option exists for POSIX-compliance reasons ('u' is for \
'unbuffered'). The output is always unbuffered - this option \ 'unbuffered'). The output is always unbuffered - this option \
@ -489,59 +482,59 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("no-config") Arg::new("no-config")
.long("no-config") .long("no-config")
.hidden(true) .hide(true)
.help("Do not use the configuration file"), .help("Do not use the configuration file"),
) )
.arg( .arg(
Arg::with_name("no-custom-assets") Arg::new("no-custom-assets")
.long("no-custom-assets") .long("no-custom-assets")
.hidden(true) .hide(true)
.help("Do not load custom assets"), .help("Do not load custom assets"),
) )
.arg( .arg(
Arg::with_name("config-file") Arg::new("config-file")
.long("config-file") .long("config-file")
.conflicts_with("list-languages") .conflicts_with("list-languages")
.conflicts_with("list-themes") .conflicts_with("list-themes")
.hidden(true) .hide(true)
.help("Show path to the configuration file."), .help("Show path to the configuration file."),
) )
.arg( .arg(
Arg::with_name("generate-config-file") Arg::new("generate-config-file")
.long("generate-config-file") .long("generate-config-file")
.conflicts_with("list-languages") .conflicts_with("list-languages")
.conflicts_with("list-themes") .conflicts_with("list-themes")
.hidden(true) .hide(true)
.help("Generates a default configuration file."), .help("Generates a default configuration file."),
) )
.arg( .arg(
Arg::with_name("config-dir") Arg::new("config-dir")
.long("config-dir") .long("config-dir")
.hidden(true) .hide(true)
.help("Show bat's configuration directory."), .help("Show bat's configuration directory."),
) )
.arg( .arg(
Arg::with_name("cache-dir") Arg::new("cache-dir")
.long("cache-dir") .long("cache-dir")
.hidden(true) .hide(true)
.help("Show bat's cache directory."), .help("Show bat's cache directory."),
) )
.arg( .arg(
Arg::with_name("diagnostic") Arg::new("diagnostic")
.long("diagnostic") .long("diagnostic")
.alias("diagnostics") .alias("diagnostics")
.hidden_short_help(true) .hide_short_help(true)
.help("Show diagnostic information for bug reports.") .help("Show diagnostic information for bug reports.")
) )
.arg( .arg(
Arg::with_name("acknowledgements") Arg::new("acknowledgements")
.long("acknowledgements") .long("acknowledgements")
.hidden_short_help(true) .hide_short_help(true)
.help("Show acknowledgements."), .help("Show acknowledgements."),
) )
.help_message("Print this help message."); .mut_arg("help", |arg| arg.help("Print this help message."));
// Check if the current directory contains a file name cache. Otherwise, // Check if the current directory contains a file name cache. Otherwise,
// enable the 'bat cache' subcommand. // enable the 'bat cache' subcommand.
@ -549,10 +542,10 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
app app
} else { } else {
app.subcommand( app.subcommand(
SubCommand::with_name("cache") Command::new("cache")
.about("Modify the syntax-definition and theme cache") .about("Modify the syntax-definition and theme cache")
.arg( .arg(
Arg::with_name("build") Arg::new("build")
.long("build") .long("build")
.short('b') .short('b')
.help("Initialize (or update) the syntax/theme cache.") .help("Initialize (or update) the syntax/theme cache.")
@ -562,18 +555,18 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
), ),
) )
.arg( .arg(
Arg::with_name("clear") Arg::new("clear")
.long("clear") .long("clear")
.short('c') .short('c')
.help("Remove the cached syntax definitions and themes."), .help("Remove the cached syntax definitions and themes."),
) )
.group( .group(
ArgGroup::with_name("cache-actions") ArgGroup::new("cache-actions")
.args(&["build", "clear"]) .args(&["build", "clear"])
.required(true), .required(true),
) )
.arg( .arg(
Arg::with_name("source") Arg::new("source")
.long("source") .long("source")
.requires("build") .requires("build")
.takes_value(true) .takes_value(true)
@ -581,7 +574,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
.help("Use a different directory to load syntaxes and themes from."), .help("Use a different directory to load syntaxes and themes from."),
) )
.arg( .arg(
Arg::with_name("target") Arg::new("target")
.long("target") .long("target")
.requires("build") .requires("build")
.takes_value(true) .takes_value(true)
@ -590,17 +583,12 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static> {
"Use a different directory to store the cached syntax and theme set.", "Use a different directory to store the cached syntax and theme set.",
), ),
) )
.arg( .arg(Arg::new("blank").long("blank").requires("build").help(
Arg::with_name("blank") "Create completely new syntax and theme sets \
.long("blank")
.requires("build")
.help(
"Create completely new syntax and theme sets \
(instead of appending to the default sets).", (instead of appending to the default sets).",
), ))
)
.arg( .arg(
Arg::with_name("acknowledgements") Arg::new("acknowledgements")
.long("acknowledgements") .long("acknowledgements")
.requires("build") .requires("build")
.help("Build acknowledgements.bin."), .help("Build acknowledgements.bin."),