From 5f26bd583a03c1210ea9aedbf3cd870b6cd205e9 Mon Sep 17 00:00:00 2001 From: Fred Brennan Date: Sun, 27 Mar 2022 02:38:24 -0400 Subject: [PATCH] Update to Clap 3.1 --- Cargo.lock | 43 +++++++++++++++++++------------------------ Cargo.toml | 2 +- src/arbitrary.rs | 28 ++++++++++++++-------------- src/glyphpathlen.rs | 18 +++++++++--------- src/glyphs.rs | 18 +++++++++--------- src/glyphslen.rs | 4 ++-- src/main.rs | 13 ++++++------- src/util/mod.rs | 16 ++++++++-------- src/write_metainfo.rs | 4 ++-- 9 files changed, 70 insertions(+), 76 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c237f19..9a713b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -135,17 +135,18 @@ dependencies = [ [[package]] name = "clap" -version = "2.34.0" +version = "3.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +checksum = "d8c93436c21e4698bacadf42917db28b23017027a4deccb35dbe47a7e7840123" dependencies = [ - "ansi_term", "atty", "bitflags", + "indexmap", + "lazy_static", + "os_str_bytes", "strsim", + "termcolor", "textwrap", - "unicode-width", - "vec_map", ] [[package]] @@ -614,6 +615,15 @@ dependencies = [ "libc", ] +[[package]] +name = "os_str_bytes" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" +dependencies = [ + "memchr", +] + [[package]] name = "plist" version = "1.3.1" @@ -776,9 +786,9 @@ checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" [[package]] name = "strsim" -version = "0.8.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" @@ -802,12 +812,9 @@ dependencies = [ [[package]] name = "textwrap" -version = "0.11.0" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" [[package]] name = "thiserror" @@ -1039,12 +1046,6 @@ dependencies = [ "unic-common", ] -[[package]] -name = "unicode-width" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" - [[package]] name = "unicode-xid" version = "0.2.2" @@ -1066,12 +1067,6 @@ dependencies = [ "getrandom", ] -[[package]] -name = "vec_map" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" - [[package]] name = "walkdir" version = "2.3.2" diff --git a/Cargo.toml b/Cargo.toml index e9401ff..3741265 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ license = "Apache-2.0" keywords = ["ufo", "font-format", "glif", "fonts"] [dependencies] -clap = "2.33.3" +clap = { version = "3.1", features = ["cargo"] } norad = { git = "https://github.com/linebender/norad" } serde_json = "1" unic-ucd = "0.9" diff --git a/src/arbitrary.rs b/src/arbitrary.rs index bf19819..49776a5 100644 --- a/src/arbitrary.rs +++ b/src/arbitrary.rs @@ -11,49 +11,49 @@ use std::time::Instant; use crate::util; use crate::write_metainfo::write_metainfo_impl; -pub fn clap_subcommand() -> clap::App<'static, 'static> { - clap::SubCommand::with_name("arbitrary") +pub fn clap_subcommand<'help>() -> clap::Command<'help> { + clap::Command::new("arbitrary") .setting(clap::AppSettings::DeriveDisplayOrder) .about("Performs arbitrary operations on a plist file, by default a font's fontinfo.plist.\n\nNote: The arguments `-k`, `-v`, and `-d` must be provided multiple times for multiple values, not delimited.") .version(env!("CARGO_PKG_VERSION")) .arg( - clap::Arg::with_name("keys") - .required_unless("delete-keys") - .multiple(true) + clap::Arg::new("keys") + .required_unless_present("delete-keys") + .multiple_occurrences(true) .takes_value(true) .allow_hyphen_values(true) .number_of_values(1) - .short("k") + .short('k') .long("key") .value_name("key") .help("List of key values to display, one per line, in order requested"), ) .arg( - clap::Arg::with_name("values") - .multiple(true) + clap::Arg::new("values") + .multiple_occurrences(true) .takes_value(true) .allow_hyphen_values(true) .number_of_values(1) - .short("v") + .short('v') .long("value") .value_name("value") .help("List of values to write, in order requested"), ) .arg( - clap::Arg::with_name("delete-keys") - .multiple(true) + clap::Arg::new("delete-keys") + .multiple_occurrences(true) .takes_value(true) .allow_hyphen_values(true) .number_of_values(1) - .short("d") + .short('d') .long("delete") .value_name("key") .help("List of keys to delete from the plist"), ) .arg( - clap::Arg::with_name("xml-redirect") + clap::Arg::new("xml-redirect") .takes_value(true) - .short("X") + .short('X') .long("xml-redirect") .value_name("FILE") .help("Redirect XML to this path instead. Use /dev/stdout or /dev/stderr if that's what you want, `-` not recognized.") diff --git a/src/glyphpathlen.rs b/src/glyphpathlen.rs index 83937b3..ae509a9 100644 --- a/src/glyphpathlen.rs +++ b/src/glyphpathlen.rs @@ -5,30 +5,30 @@ use MFEKmath::{piecewise::SegmentIterator, Piecewise}; use crate::util; -pub fn clap_subcommand() -> clap::App<'static, 'static> { - clap::SubCommand::with_name("glyphpathlen") +pub fn clap_subcommand<'help>() -> clap::Command<'help> { + clap::Command::new("glyphpathlen") .setting(clap::AppSettings::DeriveDisplayOrder) .about("Show length of contours in a glyph (.glif) on separate lines") .arg( - clap::Arg::with_name("segmentwise") - .short("s") + clap::Arg::new("segmentwise") + .short('s') .long("segmentwise") .help("Display length of each segment separated by spaces"), ) .arg( - clap::Arg::with_name("joined") + clap::Arg::new("joined") .long("joined") - .short("j") + .short('j') .help("Display one line: sum of joined path"), ) - .arg(clap::Arg::with_name("json").long("json").short("J").help("Output JSON instead")) + .arg(clap::Arg::new("json").long("json").short('J').help("Output JSON instead")) .arg( - clap::Arg::with_name("accuracy") + clap::Arg::new("accuracy") .long("accuracy") .help("Precision of length calculation") .takes_value(true) .default_value("0.01") - .empty_values(false) + .forbid_empty_values(true) .number_of_values(1) .validator(util::arg_validator_positive_f64), ) diff --git a/src/glyphs.rs b/src/glyphs.rs index 0db272c..09f9ac0 100644 --- a/src/glyphs.rs +++ b/src/glyphs.rs @@ -5,33 +5,33 @@ use unic_ucd::name::Name; use std::cmp::Ordering; -pub fn clap_subcommands() -> [clap::App<'static, 'static>; 2] { +pub fn clap_subcommands<'help>() -> [clap::Command<'help>; 2] { [ - clap::SubCommand::with_name("glyphs") + clap::Command::new("glyphs") .about("Dumps the font's glyphs") .setting(clap::AppSettings::DeriveDisplayOrder) .arg( - clap::Arg::with_name("sort") + clap::Arg::new("sort") .takes_value(false) - .short("s") + .short('s') .long("sort") .help("Sort by Unicode char"), ) .arg( - clap::Arg::with_name("hide-unencoded") + clap::Arg::new("hide-unencoded") .takes_value(false) - .short("H") + .short('H') .long("hide-unencoded") .help("Don't show unencoded glyphs in listing"), ) .arg( - clap::Arg::with_name("unencoded-at-top") + clap::Arg::new("unencoded-at-top") .takes_value(false) - .short("u") + .short('u') .long("unencoded-at-top") .help("Glyphs without encodings go to the top"), ), - clap::SubCommand::with_name("glyph").about("Dumps a single font glyph in the format of `MFEKmetadata glyphs`"), + clap::Command::new("glyph").about("Dumps a single font glyph in the format of `MFEKmetadata glyphs`"), ] } diff --git a/src/glyphslen.rs b/src/glyphslen.rs index 8e59a67..731ddb3 100644 --- a/src/glyphslen.rs +++ b/src/glyphslen.rs @@ -1,8 +1,8 @@ use clap; use norad::{DataRequest, Font}; -pub fn clap_subcommand() -> clap::App<'static, 'static> { - clap::SubCommand::with_name("glyphslen").about("Show number of glyphs in font") +pub fn clap_subcommand<'help>() -> clap::Command<'help> { + clap::Command::new("glyphslen").about("Show number of glyphs in font") } pub fn glyphslen(path: &std::ffi::OsStr, _args: &clap::ArgMatches) { diff --git a/src/main.rs b/src/main.rs index b5b47ea..2c7914e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,14 +21,15 @@ pub mod util; use std::path; -fn parse_args() -> clap::ArgMatches<'static> { - let mut app = clap::App::new(clap::crate_name!()) +fn parse_args() -> clap::ArgMatches { + let mut app = clap::Command::new(clap::crate_name!()) .version(env!("CARGO_PKG_VERSION")) .author(clap::crate_authors!()) .about(clap::crate_description!()) - .setting(clap::AppSettings::SubcommandRequiredElseHelp) + .subcommand_required(true) + .arg_required_else_help(true) .arg( - clap::Arg::with_name("PATH") + clap::Arg::new("PATH") .help("Sets the input file (glif/UFO/rarely plist) to use") .required(true) .index(1) @@ -59,12 +60,10 @@ fn main() { util::init_env_logger(); mfek_ipc::display_header("metadata"); let matches = parse_args(); - let (program, args) = matches.subcommand(); + let (program, args) = matches.subcommand().expect("Failed to parse args?"); let path = matches.value_of_os("PATH").unwrap(); - let args = args.expect("Failed to parse args?"); - match program { "arbitrary" => arbitrary(path, &args), "glyphs" => glyphs(path, &args), diff --git a/src/util/mod.rs b/src/util/mod.rs index ec17039..cb00f31 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -10,7 +10,7 @@ enum ValidatorRange { Positive, } -fn arg_validator_f64_impl(v: String, range: ValidatorRange) -> Result<(), String> { +fn arg_validator_f64_impl(v: &str, range: ValidatorRange) -> Result<(), String> { match v.parse::() { Ok(i) => { let err = || Err(String::from(&format!("Value outside allowed range ({:?})", range))); @@ -36,38 +36,38 @@ fn arg_validator_f64_impl(v: String, range: ValidatorRange) -> Result<(), String } } -pub fn arg_validator_positive_f64(v: String) -> Result<(), String> { +pub fn arg_validator_positive_f64(v: &str) -> Result<(), String> { arg_validator_f64_impl(v, ValidatorRange::Positive) } -pub fn arg_validator_positive_or_zero_f64(v: String) -> Result<(), String> { +pub fn arg_validator_positive_or_zero_f64(v: &str) -> Result<(), String> { arg_validator_f64_impl(v, ValidatorRange::PositiveNotZero) } -pub fn arg_validator_f64(v: String) -> Result<(), String> { +pub fn arg_validator_f64(v: &str) -> Result<(), String> { arg_validator_f64_impl(v, ValidatorRange::All) } -pub fn arg_validator_isize(v: String) -> Result<(), String> { +pub fn arg_validator_isize(v: &str) -> Result<(), String> { match v.parse::() { Ok(_) => Ok(()), Err(_) => Err(String::from("Value must be an integer")), } } -pub fn arg_validator_usize(v: String) -> Result<(), String> { +pub fn arg_validator_usize(v: &str) -> Result<(), String> { match v.parse::() { Ok(_) => Ok(()), Err(_) => Err(String::from("Value must be a positive integer")), } } -pub fn arg_validator_suffix(f: &impl Fn(String) -> Result<(), String>, suffix: char) -> impl Fn(String) -> Result<(), String> + '_ { +pub fn arg_validator_suffix(f: &impl Fn(&str) -> Result<(), String>, suffix: char) -> impl Fn(String) -> Result<(), String> + '_ { move |mut v| { while v.ends_with(suffix) { assert_eq!(v.pop().unwrap(), suffix); } - f(v) + f(&v) } } diff --git a/src/write_metainfo.rs b/src/write_metainfo.rs index da6948d..8b59a01 100644 --- a/src/write_metainfo.rs +++ b/src/write_metainfo.rs @@ -19,8 +19,8 @@ static METAINFO: &[u8] = br#" "#; -pub fn clap_subcommand() -> clap::App<'static, 'static> { - clap::SubCommand::with_name("write_metainfo") +pub fn clap_subcommand<'help>() -> clap::Command<'help> { + clap::Command::new("write_metainfo") } pub(crate) fn write_metainfo_impl(ufo: &OsStr) -> Result {