Merge pull request #391 from epage/migrate

feat: Upgrate to clap3
This commit is contained in:
Ed Page 2021-12-31 16:04:58 -06:00 committed by GitHub
commit b356f9d845
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 151 additions and 94 deletions

82
Cargo.lock generated
View file

@ -159,11 +159,28 @@ dependencies = [
"atty", "atty",
"bitflags", "bitflags",
"strsim 0.8.0", "strsim 0.8.0",
"textwrap", "textwrap 0.11.0",
"unicode-width", "unicode-width",
"vec_map", "vec_map",
] ]
[[package]]
name = "clap"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d17bf219fcd37199b9a29e00ba65dfb8cd5b2688b7297ec14ff829c40ac50ca9"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"indexmap",
"lazy_static",
"os_str_bytes",
"strsim 0.10.0",
"termcolor",
"textwrap 0.14.2",
]
[[package]] [[package]]
name = "clap-verbosity-flag" name = "clap-verbosity-flag"
version = "0.3.2" version = "0.3.2"
@ -174,6 +191,29 @@ dependencies = [
"structopt", "structopt",
] ]
[[package]]
name = "clap-verbosity-flag"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb1281ab1a7abc0f415a57cf6bc1f46282957ce0c5f2b3fe6b98ff3adf8e29b3"
dependencies = [
"clap 3.0.0",
"log",
]
[[package]]
name = "clap_derive"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1b9752c030a14235a0bd5ef3ad60a1dcac8468c30921327fc8af36b20c790b9"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "codegenrs" name = "codegenrs"
version = "1.0.0" version = "1.0.0"
@ -217,12 +257,12 @@ dependencies = [
[[package]] [[package]]
name = "concolor-clap" name = "concolor-clap"
version = "0.0.6" version = "0.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eb23c99e877c098e60ba3e84410153bfc311ac91b6ac7eabc0da0414ffa12e4" checksum = "14b1e2c32ce41369500cb16afec166bb78f8044ab18264802e679aeec6871039"
dependencies = [ dependencies = [
"clap 3.0.0",
"concolor-control", "concolor-control",
"structopt",
] ]
[[package]] [[package]]
@ -265,7 +305,7 @@ checksum = "1604dafd25fba2fe2d5895a9da139f8dc9b319a5fe5354ca137cbbce4e178d10"
dependencies = [ dependencies = [
"atty", "atty",
"cast", "cast",
"clap", "clap 2.34.0",
"criterion-plot", "criterion-plot",
"csv", "csv",
"itertools", "itertools",
@ -889,6 +929,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "os_str_bytes"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "os_type" name = "os_type"
version = "2.3.0" version = "2.3.0"
@ -1265,13 +1314,19 @@ version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "structopt" name = "structopt"
version = "0.3.25" version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40b9788f4202aa75c240ecc9c15c65185e6a39ccdeb0fd5d008b98825464c87c" checksum = "40b9788f4202aa75c240ecc9c15c65185e6a39ccdeb0fd5d008b98825464c87c"
dependencies = [ dependencies = [
"clap", "clap 2.34.0",
"lazy_static", "lazy_static",
"structopt-derive", "structopt-derive",
] ]
@ -1338,6 +1393,12 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "textwrap"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80"
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.30" version = "1.0.30"
@ -1454,8 +1515,8 @@ dependencies = [
"assert_fs", "assert_fs",
"atty", "atty",
"bstr", "bstr",
"clap", "clap 3.0.0",
"clap-verbosity-flag", "clap-verbosity-flag 0.4.0",
"concolor-clap", "concolor-clap",
"concolor-control", "concolor-control",
"content_inspector", "content_inspector",
@ -1475,7 +1536,6 @@ dependencies = [
"proc-exit", "proc-exit",
"serde", "serde",
"serde_json", "serde_json",
"structopt",
"toml", "toml",
"trycmd", "trycmd",
"typed-arena", "typed-arena",
@ -1536,8 +1596,8 @@ dependencies = [
name = "typos-vars-codegen" name = "typos-vars-codegen"
version = "1.3.0" version = "1.3.0"
dependencies = [ dependencies = [
"clap", "clap 2.34.0",
"clap-verbosity-flag", "clap-verbosity-flag 0.3.2",
"codegenrs", "codegenrs",
"dictgen", "dictgen",
"env_logger", "env_logger",

View file

@ -64,9 +64,8 @@ typos-dict = { version = "^0.7", path = "crates/typos-dict", optional = true }
typos-vars = { version = "^0.8", path = "crates/typos-vars", optional = true } typos-vars = { version = "^0.8", path = "crates/typos-vars", optional = true }
unicase = "2.5" unicase = "2.5"
anyhow = "1.0" anyhow = "1.0"
structopt = "0.3" clap = "3.0"
clap = "2" clap-verbosity-flag = "0.4"
clap-verbosity-flag = "0.3"
ignore = "0.4" ignore = "0.4"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
toml = "0.5" toml = "0.5"
@ -75,7 +74,7 @@ env_logger = { version = "0.9", default-features = false, features = ["termcolor
atty = "0.2.14" atty = "0.2.14"
yansi = "0.5.0" yansi = "0.5.0"
concolor-control = { version = "0.0.7" } concolor-control = { version = "0.0.7" }
concolor-clap = { version = "0.0.6", features = ["api_unstable"] } concolor-clap = { version = "0.0.7", features = ["api_unstable"] }
bstr = "0.2" bstr = "0.2"
once_cell = "1.2.0" once_cell = "1.2.0"
ahash = "0.7" ahash = "0.7"

View file

@ -1,15 +1,13 @@
use structopt::StructOpt; use clap::Parser;
use typos_cli::config; use typos_cli::config;
arg_enum! { #[derive(Debug, Copy, Clone, PartialEq, Eq, clap::ArgEnum)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum Format {
pub enum Format {
Silent, Silent,
Brief, Brief,
Long, Long,
Json, Json,
}
} }
impl Format { impl Format {
@ -39,109 +37,101 @@ impl Default for Format {
} }
} }
#[derive(Debug, StructOpt)] #[derive(Debug, Parser)]
#[structopt(rename_all = "kebab-case")] #[clap(rename_all = "kebab-case")]
#[structopt( #[clap(about, author, version)]
setting = structopt::clap::AppSettings::UnifiedHelpMessage, #[clap(
setting = structopt::clap::AppSettings::DeriveDisplayOrder, setting = clap::AppSettings::DeriveDisplayOrder,
setting = structopt::clap::AppSettings::DontCollapseArgsInUsage, setting = clap::AppSettings::DontCollapseArgsInUsage,
setting = concolor_clap::color_choice(), color = concolor_clap::color_choice(),
)] )]
#[structopt(group = structopt::clap::ArgGroup::with_name("mode").multiple(false))] #[clap(group = clap::ArgGroup::new("mode").multiple(false))]
pub(crate) struct Args { pub(crate) struct Args {
#[structopt(parse(from_os_str), default_value = ".")] #[clap(parse(from_os_str), default_value = ".")]
/// Paths to check with `-` for stdin /// Paths to check with `-` for stdin
pub(crate) path: Vec<std::path::PathBuf>, pub(crate) path: Vec<std::path::PathBuf>,
#[structopt(short = "c", long = "config")] #[clap(short = 'c', long = "config", parse(from_os_str))]
/// Custom config file /// Custom config file
pub(crate) custom_config: Option<std::path::PathBuf>, pub(crate) custom_config: Option<std::path::PathBuf>,
#[structopt(long)] #[clap(long)]
/// Ignore implicit configuration files. /// Ignore implicit configuration files.
pub(crate) isolated: bool, pub(crate) isolated: bool,
#[structopt(long, group = "mode")] #[clap(long, group = "mode")]
/// Print a diff of what would change /// Print a diff of what would change
pub(crate) diff: bool, pub(crate) diff: bool,
#[structopt(long, short = "w", group = "mode")] #[clap(long, short = 'w', group = "mode")]
/// Write fixes out /// Write fixes out
pub(crate) write_changes: bool, pub(crate) write_changes: bool,
#[structopt(long, group = "mode")] #[clap(long, group = "mode")]
/// Debug: Print each file that would be spellchecked. /// Debug: Print each file that would be spellchecked.
pub(crate) files: bool, pub(crate) files: bool,
#[structopt(long, group = "mode")] #[clap(long, group = "mode")]
/// Debug: Print each identifier that would be spellchecked. /// Debug: Print each identifier that would be spellchecked.
pub(crate) identifiers: bool, pub(crate) identifiers: bool,
#[structopt(long, group = "mode")] #[clap(long, group = "mode")]
/// Debug: Print each word that would be spellchecked. /// Debug: Print each word that would be spellchecked.
pub(crate) words: bool, pub(crate) words: bool,
#[structopt(long, group = "mode")] #[clap(long, parse(from_os_str), group = "mode")]
/// Write the current configuration to file with `-` for stdout /// Write the current configuration to file with `-` for stdout
pub(crate) dump_config: Option<std::path::PathBuf>, pub(crate) dump_config: Option<std::path::PathBuf>,
#[structopt(long, group = "mode")] #[clap(long, group = "mode")]
/// Show all supported file types. /// Show all supported file types.
pub(crate) type_list: bool, pub(crate) type_list: bool,
#[structopt( #[clap(long, arg_enum, ignore_case = true, default_value("long"))]
long,
possible_values(&Format::variants()),
case_insensitive(true),
default_value("long")
)]
pub(crate) format: Format, pub(crate) format: Format,
#[structopt(short = "j", long = "threads", default_value = "0")] #[clap(short = 'j', long = "threads", default_value = "0")]
/// The approximate number of threads to use. /// The approximate number of threads to use.
pub(crate) threads: usize, pub(crate) threads: usize,
#[structopt(flatten)] #[clap(flatten)]
pub(crate) config: ConfigArgs, pub(crate) config: ConfigArgs,
#[structopt(flatten)] #[clap(flatten)]
pub(crate) color: concolor_clap::Color, pub(crate) color: concolor_clap::Color,
#[structopt(flatten)] #[clap(flatten)]
pub(crate) verbose: clap_verbosity_flag::Verbosity, pub(crate) verbose: clap_verbosity_flag::Verbosity,
} }
#[derive(Debug, Clone, StructOpt)] #[derive(Debug, Clone, clap::Args)]
#[structopt(rename_all = "kebab-case")] #[clap(rename_all = "kebab-case")]
pub(crate) struct FileArgs { pub(crate) struct FileArgs {
#[structopt(long, overrides_with("no-binary"))] #[clap(long, overrides_with("no-binary"))]
/// Search binary files. /// Search binary files.
binary: bool, binary: bool,
#[structopt(long, overrides_with("binary"), hidden(true))] #[clap(long, overrides_with("binary"), hide = true)]
no_binary: bool, no_binary: bool,
#[structopt(long, overrides_with("check-filenames"))] #[clap(long, overrides_with("check-filenames"))]
/// Skip verifying spelling in file names. /// Skip verifying spelling in file names.
no_check_filenames: bool, no_check_filenames: bool,
#[structopt(long, overrides_with("no-check-filenames"), hidden(true))] #[clap(long, overrides_with("no-check-filenames"), hide = true)]
check_filenames: bool, check_filenames: bool,
#[structopt(long, overrides_with("check-files"))] #[clap(long, overrides_with("check-files"))]
/// Skip verifying spelling in files. /// Skip verifying spelling in files.
no_check_files: bool, no_check_files: bool,
#[structopt(long, overrides_with("no-check-files"), hidden(true))] #[clap(long, overrides_with("no-check-files"), hide = true)]
check_files: bool, check_files: bool,
#[structopt(long, overrides_with("no-unicode"), hidden(true))] #[clap(long, overrides_with("no-unicode"), hide = true)]
unicode: bool, unicode: bool,
#[structopt(long, overrides_with("unicode"))] #[clap(long, overrides_with("unicode"))]
/// Only allow ASCII characters in identifiers /// Only allow ASCII characters in identifiers
no_unicode: bool, no_unicode: bool,
#[structopt( #[clap(long, possible_values(config::Locale::variants()))]
long,
possible_values(&config::Locale::variants()),
)]
pub(crate) locale: Option<config::Locale>, pub(crate) locale: Option<config::Locale>,
} }
@ -179,12 +169,12 @@ impl FileArgs {
} }
} }
#[derive(Debug, StructOpt)] #[derive(Debug, clap::Args)]
#[structopt(rename_all = "kebab-case")] #[clap(rename_all = "kebab-case")]
pub(crate) struct ConfigArgs { pub(crate) struct ConfigArgs {
#[structopt(flatten)] #[clap(flatten)]
walk: WalkArgs, walk: WalkArgs,
#[structopt(flatten)] #[clap(flatten)]
overrides: FileArgs, overrides: FileArgs,
} }
@ -198,47 +188,47 @@ impl ConfigArgs {
} }
} }
#[derive(Debug, StructOpt)] #[derive(Debug, clap::Args)]
#[structopt(rename_all = "kebab-case")] #[clap(rename_all = "kebab-case")]
pub(crate) struct WalkArgs { pub(crate) struct WalkArgs {
#[structopt(long, name = "GLOB")] #[clap(long, name = "GLOB")]
/// Ignore files & directories matching the glob. /// Ignore files & directories matching the glob.
exclude: Vec<String>, exclude: Vec<String>,
#[structopt(long, overrides_with("no-hidden"))] #[clap(long, overrides_with("no-hidden"))]
/// Search hidden files and directories. /// Search hidden files and directories.
hidden: bool, hidden: bool,
#[structopt(long, overrides_with("hidden"), hidden(true))] #[clap(long, overrides_with("hidden"), hide = true)]
no_hidden: bool, no_hidden: bool,
#[structopt(long, overrides_with("ignore"))] #[clap(long, overrides_with("ignore"))]
/// Don't respect ignore files. /// Don't respect ignore files.
no_ignore: bool, no_ignore: bool,
#[structopt(long, overrides_with("no-ignore"), hidden(true))] #[clap(long, overrides_with("no-ignore"), hide = true)]
ignore: bool, ignore: bool,
#[structopt(long, overrides_with("ignore-dot"))] #[clap(long, overrides_with("ignore-dot"))]
/// Don't respect .ignore files. /// Don't respect .ignore files.
no_ignore_dot: bool, no_ignore_dot: bool,
#[structopt(long, overrides_with("no-ignore-dot"), hidden(true))] #[clap(long, overrides_with("no-ignore-dot"), hide = true)]
ignore_dot: bool, ignore_dot: bool,
#[structopt(long, overrides_with("ignore-global"))] #[clap(long, overrides_with("ignore-global"))]
/// Don't respect global ignore files. /// Don't respect global ignore files.
no_ignore_global: bool, no_ignore_global: bool,
#[structopt(long, overrides_with("no-ignore-global"), hidden(true))] #[clap(long, overrides_with("no-ignore-global"), hide = true)]
ignore_global: bool, ignore_global: bool,
#[structopt(long, overrides_with("ignore-parent"))] #[clap(long, overrides_with("ignore-parent"))]
/// Don't respect ignore files in parent directories. /// Don't respect ignore files in parent directories.
no_ignore_parent: bool, no_ignore_parent: bool,
#[structopt(long, overrides_with("no-ignore-parent"), hidden(true))] #[clap(long, overrides_with("no-ignore-parent"), hide = true)]
ignore_parent: bool, ignore_parent: bool,
#[structopt(long, overrides_with("ignore-vcs"))] #[clap(long, overrides_with("ignore-vcs"))]
/// Don't respect ignore files in vcs directories. /// Don't respect ignore files in vcs directories.
no_ignore_vcs: bool, no_ignore_vcs: bool,
#[structopt(long, overrides_with("no-ignore-vcs"), hidden(true))] #[clap(long, overrides_with("no-ignore-vcs"), hide = true)]
ignore_vcs: bool, ignore_vcs: bool,
} }
@ -285,6 +275,17 @@ fn resolve_bool_arg(yes: bool, no: bool) -> Option<bool> {
(true, false) => Some(true), (true, false) => Some(true),
(false, true) => Some(false), (false, true) => Some(false),
(false, false) => None, (false, false) => None,
(_, _) => unreachable!("StructOpt should make this impossible"), (_, _) => unreachable!("clap should make this impossible"),
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn verify_app() {
use clap::IntoApp;
Args::into_app().debug_assert()
} }
} }

View file

@ -1,10 +1,6 @@
// 2015-edition macros.
#[macro_use]
extern crate clap;
use std::io::Write; use std::io::Write;
use structopt::StructOpt; use clap::Parser;
mod args; mod args;
mod report; mod report;
@ -19,13 +15,14 @@ fn main() {
fn run() -> proc_exit::ExitResult { fn run() -> proc_exit::ExitResult {
// clap's `get_matches` uses Failure rather than Usage, so bypass it for `get_matches_safe`. // clap's `get_matches` uses Failure rather than Usage, so bypass it for `get_matches_safe`.
let args = match args::Args::from_args_safe() { let args = match args::Args::try_parse() {
Ok(args) => args, Ok(args) => args,
Err(e) if e.use_stderr() => { Err(e) if e.use_stderr() => {
return Err(proc_exit::Code::USAGE_ERR.with_message(e)); let _ = e.print();
return proc_exit::Code::USAGE_ERR.ok();
} }
Err(e) => { Err(e) => {
writeln!(std::io::stdout(), "{}", e)?; let _ = e.print();
return proc_exit::Code::SUCCESS.ok(); return proc_exit::Code::SUCCESS.ok();
} }
}; };