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