From 19ab242942a88b363eef9edf4a9aae5aed6f6c8e Mon Sep 17 00:00:00 2001 From: Hunar Roop Kahlon Date: Tue, 31 Oct 2023 14:39:21 -0700 Subject: [PATCH] refactor: use `clap` for cli parsing (#21) * chore(deps): add `clap` Signed-off-by: Hunar Roop Kahlon * refactor(cli): rework the cli args to use `clap` Signed-off-by: Hunar Roop Kahlon * chore: remove added newline in unchanged file * fix: hermit-cli help message set correct output name --------- Signed-off-by: Hunar Roop Kahlon Co-authored-by: Gavin Hayes --- hermit-cli/Cargo.lock | 225 ++++++++++++++++++ .../crates/hermitfile-parser/Cargo.toml | 1 + .../crates/hermitfile-parser/src/main.rs | 72 ++---- 3 files changed, 242 insertions(+), 56 deletions(-) diff --git a/hermit-cli/Cargo.lock b/hermit-cli/Cargo.lock index 0570e00..bde4aac 100644 --- a/hermit-cli/Cargo.lock +++ b/hermit-cli/Cargo.lock @@ -17,6 +17,60 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstream" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140" +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 = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + [[package]] name = "block-buffer" version = "0.10.4" @@ -38,6 +92,53 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac495e00dcec98c83465d5ad66c5c4fabd652fd6686e7c6269b117e729a6f17b" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c77ed9a32a62e6ca27175d00d29d05ca32e396ea1eb5fb01d8256b669cec7663" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", + "terminal_size", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.37", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "cpufeatures" version = "0.2.9" @@ -112,6 +213,16 @@ dependencies = [ "thiserror", ] +[[package]] +name = "errno" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "flate2" version = "1.0.28" @@ -132,10 +243,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermitfile-parser" version = "0.1.0" dependencies = [ + "clap", "dockerfile-parser", "serde", "serde_json", @@ -160,6 +278,12 @@ version = "0.2.149" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" +[[package]] +name = "linux-raw-sys" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f" + [[package]] name = "memchr" version = "2.6.4" @@ -264,6 +388,19 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56d84fdd47036b038fc80dd333d10b6aab10d5d31f4a366e20014def75328d33" +[[package]] +name = "rustix" +version = "0.38.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b426b0506e5d50a7d8dafcf2e81471400deb602392c7dd110815afb4eaf02a3" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "ryu" version = "1.0.15" @@ -333,6 +470,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "1.0.109" @@ -355,6 +498,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "terminal_size" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" +dependencies = [ + "rustix", + "windows-sys", +] + [[package]] name = "thiserror" version = "1.0.49" @@ -393,12 +546,84 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "zip" version = "0.6.6" diff --git a/hermit-cli/crates/hermitfile-parser/Cargo.toml b/hermit-cli/crates/hermitfile-parser/Cargo.toml index a152fdf..64af5b4 100644 --- a/hermit-cli/crates/hermitfile-parser/Cargo.toml +++ b/hermit-cli/crates/hermitfile-parser/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = { version = "4.4.7", features = ["color", "error-context", "help", "std", "suggestions", "usage", "env", "wrap_help", "derive"] } dockerfile-parser = { path = "../../../dockerfile-parser-rs" } serde = { version = "1.0.188", features = ["derive"] } serde_json = "1.0.107" diff --git a/hermit-cli/crates/hermitfile-parser/src/main.rs b/hermit-cli/crates/hermitfile-parser/src/main.rs index 1c31b0f..975b4da 100644 --- a/hermit-cli/crates/hermitfile-parser/src/main.rs +++ b/hermit-cli/crates/hermitfile-parser/src/main.rs @@ -1,12 +1,12 @@ use std::collections::HashMap; use std::io::SeekFrom; +use clap; use dockerfile_parser::{Dockerfile, Instruction}; use serde::Serialize; use std::io::Read; use std::io::Seek; use std::io::Write; -use std::os::wasi::ffi::OsStrExt; #[derive(Debug, Default, Serialize)] struct Hermitfile { @@ -189,65 +189,25 @@ fn create_hermit_executable(output_exe_name: &std::ffi::OsStr, hermit: Hermitfil zip.finish().unwrap(); } +#[derive(clap::Parser)] +#[command(about, author, bin_name = "hermit.com", long_version = None, version)] struct HermitCliArgs { + /// Name of the `Hermitfile` + #[arg(default_value = "Hermitfile", short = 'f')] hermitfile_path: std::ffi::OsString, + /// Output file + /// + /// on `unix`-like platforms, you will need to run `chmod +x ` + /// to make it executable. This is required becasue `WASI` does not have a + /// `chmod` function. + #[arg(default_value = "main.wasm", short = 'o')] output_path: std::ffi::OsString, } -#[allow(clippy::print_literal)] -fn parse_hermit_args() -> HermitCliArgs { - let args: Vec = std::env::args_os().collect(); - let mut hermitfile_path: std::ffi::OsString = "Hermitfile".into(); - let mut output_path: std::ffi::OsString = "wasm.com".into(); - - let mut args_iter = args.iter(); - args_iter.next(); - while let Some(arg) = args_iter.next() { - match (arg.len(), arg.to_str()) { - (_, Some("-o")) => { - if let Some(output_arg) = args_iter.next() { - output_path = output_arg.clone(); - } else { - panic!("-o provided without file"); - } - } - (_, Some("-f")) => { - if let Some(hermitfile_arg) = args_iter.next() { - hermitfile_path = hermitfile_arg.clone(); - } else { - panic!("-f provided without file"); - } - } - (_, Some("-h") | Some("--help")) => { - print!( - "{}", - r#"hermit.com [-f ] - -If a path to a `Hermitfile` is not provided, it tries to load `Hermitfile` from -the current directory. If an `output_path` is not provided, the hermit is -written to `wasm.com` in the currently directly. On Unix-like operating systems -you must `chmod +x wasm.com` to make it executable. This is required because -WASI does not have a `chmod` function. -"# - ); - std::process::exit(0); - } - (4.., Some(arg)) if arg.starts_with("-o=") => { - let (_, output_value) = arg.as_bytes().split_at(3); - output_path = std::ffi::OsStr::from_bytes(output_value).into(); - } - (4.., Some(arg)) if arg.starts_with("-f=") => { - let (_, output_value) = arg.as_bytes().split_at(3); - hermitfile_path = std::ffi::OsStr::from_bytes(output_value).into(); - } - _ => { - panic!("Unhandled arg {:?}", arg); - } - } - } - HermitCliArgs { - hermitfile_path, - output_path, +impl HermitCliArgs { + #[inline] + fn parse_args() -> Self { + clap::Parser::parse() } } @@ -256,7 +216,7 @@ fn main() { if let Ok(input_wd) = std::env::var("PWD") { std::env::set_current_dir(input_wd).unwrap(); } - let options = parse_hermit_args(); + let options = HermitCliArgs::parse_args(); let hermit = parse_hermitfile(&options.hermitfile_path); create_hermit_executable(&options.output_path, hermit); }