From 3c8303f46f0e22d356c7e2d3a0169c8c5ed71ea7 Mon Sep 17 00:00:00 2001 From: Matthew James Briggs Date: Thu, 31 Aug 2023 16:01:21 -0700 Subject: [PATCH] update clap from v2 to v4 --- Cargo.lock | 149 +++++++++++++++++++++++++---------------- Cargo.toml | 2 +- src/main.rs | 187 ++++++++++++++++++++++++++-------------------------- 3 files changed, 185 insertions(+), 153 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e502be2..120e155 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,12 +27,51 @@ dependencies = [ ] [[package]] -name = "ansi_term" -version = "0.12.1" +name = "anstream" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +checksum = "b1f58811cfac344940f1a400b6e6231ce35171f614f26439e80f8c1465c5cc0c" dependencies = [ - "winapi", + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15c4c2c83f81532e5845a733998b6971faca23490340a418e9b72a3ec9de12ea" + +[[package]] +name = "anstyle-parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd" +dependencies = [ + "anstyle", + "windows-sys", ] [[package]] @@ -49,17 +88,6 @@ dependencies = [ "serde_json", ] -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi 0.1.19", - "libc", - "winapi", -] - [[package]] name = "backtrace" version = "0.3.69" @@ -75,12 +103,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - [[package]] name = "bitflags" version = "2.4.0" @@ -117,19 +139,50 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "2.34.0" +version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +checksum = "6a13b88d2c62ff462f88e4a121f17a82c1af05693a2f192b5c38d14de73c19f6" dependencies = [ - "ansi_term", - "atty", - "bitflags 1.3.2", + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", "strsim", - "textwrap", - "unicode-width", - "vec_map", ] +[[package]] +name = "clap_derive" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.29", +] + +[[package]] +name = "clap_lex" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "colored" version = "1.9.4" @@ -215,13 +268,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" [[package]] -name = "hermit-abi" -version = "0.1.19" +name = "heck" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" @@ -245,7 +295,7 @@ version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" dependencies = [ - "hermit-abi 0.3.2", + "hermit-abi", "rustix", "windows-sys", ] @@ -363,7 +413,7 @@ version = "0.38.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0c3dde1fc030af041adc40e79c0e7fbcf431dd24870053d187d7c66e4b87453" dependencies = [ - "bitflags 2.4.0", + "bitflags", "errno", "libc", "linux-raw-sys", @@ -418,9 +468,9 @@ dependencies = [ [[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" @@ -456,15 +506,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] - [[package]] name = "toml" version = "0.7.6" @@ -505,12 +546,6 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" -[[package]] -name = "unicode-width" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" - [[package]] name = "unicode-xid" version = "0.2.4" @@ -518,10 +553,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" [[package]] -name = "vec_map" -version = "0.8.2" +name = "utf8parse" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" [[package]] name = "winapi" diff --git a/Cargo.toml b/Cargo.toml index 902976a..2758399 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ categories = ["development-tools::cargo-plugins"] license = "MIT OR Apache-2.0" [dependencies] -clap = "2.32" +clap = { version = "4", features = [ "derive" ] } toml = "0.7" regex = "1" serde = { version = "1", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index 5e347d0..4c8d072 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,120 +1,117 @@ //! Generate README.md from doc comments. -extern crate clap; - -use std::io::{self, Write}; - -use clap::{crate_version, App, AppSettings, Arg, ArgMatches, SubCommand}; +use clap::Parser; +use std::io; +use std::io::Write; mod helper; fn main() { - let matches = App::new("cargo-readme") - .version(&*format!("v{}", crate_version!())) - // We have to lie about our binary name since this will be a third party - // subcommand for cargo but we want usage strings to generated properly - .bin_name("cargo") - // Global version uses the version we supplied (Cargo.toml) for all subcommands as well - .settings(&[AppSettings::GlobalVersion, AppSettings::SubcommandRequired]) - // We use a subcommand because everything parsed after `cargo` is sent to the third party - // plugin which will then be interpreted as a subcommand/positional arg by clap - .subcommand(SubCommand::with_name("readme") - .author("Livio Ribeiro ") - .about("Generate README.md from doc comments") - .arg(Arg::with_name("INPUT") - .short("i") - .long("input") - .takes_value(true) - .help("File to read from.{n}\ - If not provided, will try to use `src/lib.rs`, then `src/main.rs`. If \ - neither file could be found, will look into `Cargo.toml` for a `[lib]`, \ - then for a single `[[bin]]`. If multiple binaries are found, an error \ - will be returned.")) - .arg(Arg::with_name("OUTPUT") - .short("o") - .long("output") - .takes_value(true) - .help("File to write to. If not provided, will output to stdout.")) - .arg(Arg::with_name("ROOT") - .short("r") - .long("project-root") - .takes_value(true) - .help("Directory to be set as project root (where `Cargo.toml` is){n}\ - Defaults to the current directory.")) - .arg(Arg::with_name("TEMPLATE") - .short("t") - .long("template") - .takes_value(true) - .conflicts_with("NO_TEMPLATE") - .help("Template used to render the output.{n}\ - Default behavior is to use `README.tpl` if it exists.")) - .arg(Arg::with_name("NO_TITLE") - .long("no-title") - .help("Do not prepend title line.{n}\ - By default, the title ('# crate-name') is prepended to the output.{n}\ - Ignored when using a template.")) - .arg(Arg::with_name("NO_BADGES") - .long("no-badges") - .help("Do not prepend badges line.{n}\ - By default, badges defined in Cargo.toml are prepended to the output.{n}\ - Ignored when using a template.")) - .arg(Arg::with_name("NO_LICENSE") - .long("no-license") - .help("Do not append license line.{n}\ - By default, the license defined in `Cargo.toml` will be prepended to the output.{n}\ - Ignored when using a template.")) - .arg(Arg::with_name("NO_TEMPLATE") - .long("no-template") - .help("Ignore template file when generating README.{n}\ - Only useful to ignore default template `README.tpl`.")) - .arg(Arg::with_name("NO_INDENT_HEADINGS") - .long("no-indent-headings") - .help("Do not add an extra level to headings.{n}\ - By default, '#' headings become '##', so the first '#' can be the crate \ - name. Use this option to prevent this behavior."))) - .get_matches(); - - if let Some(m) = matches.subcommand_matches("readme") { - match execute(m) { - Err(e) => { - io::stderr() - .write_fmt(format_args!("Error: {}\n", e)) - .expect("An error occurred while trying to show an error message"); - std::process::exit(1); - } - _ => {} + let args = Args::parse(); + let result = match &args.command { + Command::Readme(readme_args) => execute(readme_args), + }; + match result { + Err(e) => { + io::stderr() + .write_fmt(format_args!("Error: {}\n", e)) + .expect("An error occurred while trying to show an error message"); + std::process::exit(1); } + _ => {} } } +/// The command line interface for setting up a Bottlerocket TestSys cluster and running tests. +#[derive(Debug, Parser)] +#[clap(author, version, about)] +struct Args { + #[clap(subcommand)] + command: Command, +} + +#[derive(Debug, Parser)] +enum Command { + /// Generate README.md from doc comments + Readme(ReadmeArgs), +} + +/// Generate README.md from doc comments +#[derive(Debug, Parser)] +#[clap(author, version, about)] +struct ReadmeArgs { + /// Do not prepend badges line. + /// By default, badges defined in Cargo.toml are prepended to the output. + /// Ignored when using a template. + #[clap(long)] + no_badges: bool, + + /// Do not add an extra level to headings. + /// By default, '#' headings become '##', so the first '#' can be the crate name. Use this + /// option to prevent this behavior. + #[clap(long)] + no_indent_headings: bool, -/// Takes the arguments matches from clap and outputs the result, either to stdout of a file -fn execute(m: &ArgMatches) -> Result<(), String> { - // get inputs - let input = m.value_of("INPUT"); - let output = m.value_of("OUTPUT"); - let template = m.value_of("TEMPLATE"); - let add_title = !m.is_present("NO_TITLE"); - let add_badges = !m.is_present("NO_BADGES"); - let add_license = !m.is_present("NO_LICENSE"); - let no_template = m.is_present("NO_TEMPLATE"); - let indent_headings = !m.is_present("NO_INDENT_HEADINGS"); + /// Do not append license line. + /// By default, the license defined in `Cargo.toml` will be prepended to the output. + /// Ignored when using a template. + #[clap(long)] + no_license: bool, + /// Ignore template file when generating README. + /// Only useful to ignore default template `README.tpl`. + #[clap(long)] + no_template: bool, + + /// Do not prepend title line. + /// By default, the title ('# crate-name') is prepended to the output. + #[clap(long)] + no_title: bool, + + /// File to read from. + /// If not provided, will try to use `src/lib.rs`, then `src/main.rs`. If neither file + /// could be found, will look into `Cargo.toml` for a `[lib]`, then for a single `[[bin]]`. + /// If multiple binaries are found, an error will be returned. + #[clap(long, short = 'i')] + input: Option, + + /// File to write to. If not provided, will output to stdout. + #[clap(long, short = 'o')] + output: Option, + + /// Directory to be set as project root (where `Cargo.toml` is) + /// Defaults to the current directory. + #[clap(long = "project-root", short = 'r')] + root: Option, + + /// Template used to render the output. + /// Default behavior is to use `README.tpl` if it exists. + #[clap(long, short = 't')] + template: Option, +} + +// Takes the arguments matches from clap and outputs the result, either to stdout of a file +fn execute(args: &ReadmeArgs) -> Result<(), String> { // get project root - let project_root = helper::get_project_root(m.value_of("ROOT"))?; + let project_root = helper::get_project_root(args.root.as_deref())?; // get source file - let mut source = helper::get_source(&project_root, input)?; + let mut source = helper::get_source(&project_root, args.input.as_deref())?; // get destination file - let mut dest = helper::get_dest(&project_root, output)?; + let mut dest = helper::get_dest(&project_root, args.output.as_deref())?; // get template file - let mut template_file = if no_template { + let mut template_file = if args.no_template { None } else { - helper::get_template_file(&project_root, template)? + helper::get_template_file(&project_root, args.template.as_deref())? }; + let add_title = !args.no_title; + let add_badges = !args.no_badges; + let add_license = !args.no_license; + let indent_headings = !args.no_indent_headings; + // generate output let readme = cargo_readme::generate_readme( &project_root,