From 78742b164220de972dc1b8902133d02f102a41a9 Mon Sep 17 00:00:00 2001 From: Marcin Puc Date: Thu, 20 Jan 2022 17:05:37 +0100 Subject: [PATCH] Update arg parsing to clap v3 --- Cargo.toml | 2 +- aocf_cli/Cargo.lock | 158 +++++++++++++++++++++------------------ aocf_cli/Cargo.toml | 3 +- aocf_cli/build.rs | 13 ---- aocf_cli/src/bin/aocf.rs | 4 +- aocf_cli/src/cli.rs | 56 +++++++------- src/cli.rs | 6 +- src/lib.rs | 4 +- 8 files changed, 124 insertions(+), 122 deletions(-) delete mode 100644 aocf_cli/build.rs diff --git a/Cargo.toml b/Cargo.toml index 27ac20c..d60de02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ serde_derive = "1.0.117" serde_json = "1.0.59" tempfile = "3.1.0" libsqlite3-sys = { version = ">=0.8.0, <0.21.0", features = ["min_sqlite_version_3_7_16", "bundled"], optional = true } -structopt = "0.3.21" +clap = { version = "3.1", features = ["derive"] } atty = "0.2.14" ureq = "2.2.0" diff --git a/aocf_cli/Cargo.lock b/aocf_cli/Cargo.lock index 218a888..42c8f62 100644 --- a/aocf_cli/Cargo.lock +++ b/aocf_cli/Cargo.lock @@ -11,20 +11,12 @@ dependencies = [ "memchr", ] -[[package]] -name = "ansi_term" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" -dependencies = [ - "winapi", -] - [[package]] name = "aocf" version = "0.1.18" dependencies = [ "atty", + "clap", "diesel", "failure", "html2md", @@ -33,7 +25,6 @@ dependencies = [ "serde", "serde_derive", "serde_json", - "structopt", "tempfile", "ureq", ] @@ -44,6 +35,8 @@ version = "0.1.18" dependencies = [ "aocf", "chrono", + "clap", + "clap_complete", "crossterm", "dirs", "failure", @@ -51,7 +44,6 @@ dependencies = [ "regex", "serde", "serde_derive", - "structopt", "tempfile", "termimad", "toml", @@ -213,17 +205,50 @@ checksum = "fff857943da45f546682664a79488be82e69e43c1a7a2307679ab9afb3a66d2e" [[package]] name = "clap" -version = "2.33.3" +version = "3.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +checksum = "47582c09be7c8b32c0ab3a6181825ababb713fde6fff20fc573a3870dd45c6a0" dependencies = [ - "ansi_term", "atty", "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "lazy_static", "strsim", + "termcolor", "textwrap", - "unicode-width", - "vec_map", +] + +[[package]] +name = "clap_complete" +version = "3.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da92e6facd8d73c22745a5d3cbb59bdf8e46e3235c923e516527d8e81eec14a4" +dependencies = [ + "clap", +] + +[[package]] +name = "clap_derive" +version = "3.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213" +dependencies = [ + "os_str_bytes", ] [[package]] @@ -483,14 +508,17 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" + [[package]] name = "heck" -version = "0.3.1" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" -dependencies = [ - "unicode-segmentation", -] +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" [[package]] name = "hermit-abi" @@ -540,6 +568,16 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indexmap" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "itoa" version = "0.4.4" @@ -665,9 +703,9 @@ checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" [[package]] name = "memchr" -version = "2.3.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3197e20c7edb283f87c071ddfc7a2cca8f8e0b888c242959846a6fce03c72223" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] name = "memoffset" @@ -750,6 +788,12 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" +[[package]] +name = "os_str_bytes" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" + [[package]] name = "parking_lot" version = "0.10.2" @@ -862,18 +906,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.24" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" dependencies = [ "unicode-xid", ] [[package]] name = "quote" -version = "1.0.2" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d" dependencies = [ "proc-macro2", ] @@ -1192,39 +1236,15 @@ dependencies = [ [[package]] name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - -[[package]] -name = "structopt" -version = "0.3.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5277acd7ee46e63e5168a80734c9f6ee81b1367a7d8772a2d765df2a3705d28c" -dependencies = [ - "clap", - "lazy_static", - "structopt-derive", -] - -[[package]] -name = "structopt-derive" -version = "0.4.14" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ba9cdfda491b814720b6b06e0cac513d922fc407582032e8706e9f137976f90" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.53" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68" +checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" dependencies = [ "proc-macro2", "quote", @@ -1268,6 +1288,15 @@ dependencies = [ "utf-8", ] +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + [[package]] name = "termimad" version = "0.9.1" @@ -1284,12 +1313,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" @@ -1358,12 +1384,6 @@ dependencies = [ "smallvec", ] -[[package]] -name = "unicode-segmentation" -version = "1.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" - [[package]] name = "unicode-width" version = "0.1.8" @@ -1431,12 +1451,6 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fc439f2794e98976c88a2a2dafce96b930fe8010b0a256b3c2199a773933168" -[[package]] -name = "vec_map" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" - [[package]] name = "version_check" version = "0.1.5" diff --git a/aocf_cli/Cargo.toml b/aocf_cli/Cargo.toml index 9961fec..23e46cc 100644 --- a/aocf_cli/Cargo.toml +++ b/aocf_cli/Cargo.toml @@ -28,7 +28,8 @@ glob = "0.3.0" termimad = "0.9.1" crossterm = "0.17.7" regex = "1.4.2" -structopt = "0.3.21" +clap = { version = "3.1", features = ["derive"] } +clap_complete = "3.1" webbrowser = "0.5.5" [package.metadata.deb] diff --git a/aocf_cli/build.rs b/aocf_cli/build.rs deleted file mode 100644 index 6f7efb4..0000000 --- a/aocf_cli/build.rs +++ /dev/null @@ -1,13 +0,0 @@ -use std::env; -use std::fs; -use std::path::Path; - -fn main() { - let version = env!("CARGO_PKG_VERSION"); - let out_dir = env::var_os("OUT_DIR").unwrap(); - let dest_path = Path::new(&out_dir).join("version.rs"); - - fs::write(&dest_path, format!("static PKG_VERSION: &str = \"{}\";\n", version)).unwrap(); - - println!("cargo:rerun-if-changed=build.rs"); -} diff --git a/aocf_cli/src/bin/aocf.rs b/aocf_cli/src/bin/aocf.rs index 9dd68cf..29610eb 100644 --- a/aocf_cli/src/bin/aocf.rs +++ b/aocf_cli/src/bin/aocf.rs @@ -18,12 +18,12 @@ use tempfile::tempdir; use glob::glob; use failure::{Error, bail, format_err}; use regex::Regex; -use structopt::StructOpt; +use clap::Parser; use chrono::{Utc, Datelike}; use webbrowser; fn main() { - let opt = Aocf::from_args(); + let opt = Aocf::parse(); run(&opt).unwrap_or_else(|err| { eprintln!("error: {}", err); diff --git a/aocf_cli/src/cli.rs b/aocf_cli/src/cli.rs index 2af29bc..4a1bd29 100644 --- a/aocf_cli/src/cli.rs +++ b/aocf_cli/src/cli.rs @@ -1,53 +1,53 @@ -use structopt::{StructOpt, clap::Shell}; -use chrono::{Utc, Datelike}; - -include!(concat!(env!("OUT_DIR"), "/version.rs")); +use chrono::{Datelike, Utc}; +use clap::{Args, CommandFactory, Parser}; +use clap_complete::Shell; pub fn generate_completion(shell: Shell) { - Aocf::clap().gen_completions_to("aocf", shell, &mut std::io::stdout()); + clap_complete::generate(shell, &mut Aocf::command(), "aocf", &mut std::io::stdout()); } -#[derive(StructOpt, Debug)] -#[structopt(about = "Advent of Code Swiss army knife", version = PKG_VERSION)] +/// Advent of Code Swiss army knife +#[derive(Parser, Debug)] +#[clap(version)] pub enum Aocf { /// Switch to a specified year and day - Checkout ( AocfTimeDateOpts ), + Checkout(AocfTimeDateOpts), /// Get input data for the current problem Input { /// View in pager - #[structopt(short, long)] + #[clap(short, long)] view: bool, /// Don't use cache - #[structopt(short, long)] + #[clap(short, long)] force: bool, }, /// Get instructions for the current problem Brief { /// View pretty - #[structopt(short, long, conflicts_with = "view")] + #[clap(short, long, conflicts_with = "view")] pretty: bool, /// View in pager - #[structopt(short, long, conflicts_with = "pretty")] + #[clap(short, long, conflicts_with = "pretty")] view: bool, /// View in web browser - #[structopt(short, long, conflicts_with_all = &["pretty", "view"])] + #[clap(short, long, conflicts_with_all = &["pretty", "view"])] web: bool, /// View current day and year - #[structopt(short, long, conflicts_with = "day")] + #[clap(short, long, conflicts_with = "day")] now: bool, /// Problem day to view - #[structopt(short, long, conflicts_with = "now")] + #[clap(short, long, conflicts_with = "now")] day: Option, /// Don't use cache - #[structopt(short, long)] + #[clap(short, long)] force: bool, }, @@ -60,15 +60,15 @@ pub enum Aocf { /// Fetch brief and input data, if available Fetch { /// Don't use cache - #[structopt(short, long)] + #[clap(short, long)] force: bool, /// Use current day and year - #[structopt(short, long, conflicts_with = "day")] + #[clap(short, long, conflicts_with = "day")] now: bool, /// Problem day to use - #[structopt(short, long, conflicts_with = "now")] + #[clap(short, long, conflicts_with = "now")] day: Option, }, @@ -78,7 +78,7 @@ pub enum Aocf { /// Get summary of challenges and stars Summary { /// Specify the challenge year to view - #[structopt(short, long)] + #[clap(short, long)] year: Option, }, @@ -97,31 +97,31 @@ pub enum Aocf { /// Generate shell completion script Completion { /// Shell type - #[structopt(possible_values = &["bash", "zsh", "fish", "powershell", "elvish"])] + #[clap(arg_enum)] shell: Shell, - } + }, } -#[derive(StructOpt, Debug)] +#[derive(Args, Debug)] pub struct AocfTimeDateOpts { /// Check out current day and year - #[structopt(short, long, conflicts_with_all = &["problem-day", "problem-year", "day", "year"])] + #[clap(short, long, conflicts_with_all = &["problem-day", "problem-year", "day", "year"])] now: bool, /// Problem day - #[structopt(short, long)] + #[clap(short, long)] day: Option, /// Problem year - #[structopt(short, long)] + #[clap(short, long)] year: Option, /// Problem day - #[structopt(conflicts_with_all = &["now", "day"], required_unless_one = &["now", "day"])] + #[clap(conflicts_with_all = &["now", "day"], required_unless_present_any = &["now", "day"])] problem_day: Option, /// Problem year - #[structopt(conflicts_with_all = &["now", "year"])] + #[clap(conflicts_with_all = &["now", "year"])] problem_year: Option, } diff --git a/src/cli.rs b/src/cli.rs index 08de77a..210319a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,8 +1,8 @@ -use structopt::StructOpt; +use clap::Parser; use std::path::PathBuf; -#[derive(StructOpt, Debug)] -#[structopt(about = "Advent of Code problem\n")] +#[derive(Parser, Debug)] +#[clap(about = "Advent of Code problem\n")] pub struct AocOpts { /// File to read as input pub input: Option, diff --git a/src/lib.rs b/src/lib.rs index 51b9e8b..40877c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,7 @@ pub mod cookie; mod cli; use cli::AocOpts; -use structopt::StructOpt; +use clap::Parser; use atty::{is, Stream}; #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Serialize, Deserialize)] @@ -136,7 +136,7 @@ impl Aoc { // Process CLI args if self.parse_cli { - let opt = AocOpts::from_args(); + let opt = AocOpts::parse(); self.input_file = opt.input; }