From e46c5e08b1da380aa22ff958efef69fa06908f30 Mon Sep 17 00:00:00 2001 From: vms Date: Fri, 17 Jul 2020 15:03:46 +0300 Subject: [PATCH] enable optional arguments with cargo build (#10) --- .circleci/config.yml | 4 ++-- tools/cli/src/args.rs | 8 ++++---- tools/cli/src/build.rs | 7 ++----- tools/cli/src/main.rs | 17 ++++++----------- 4 files changed, 14 insertions(+), 22 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 756e0e6f..bcf17f7e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -54,12 +54,12 @@ jobs: cd examples/ipfs_node/wasm/ipfs_node cargo fmt --all -- --check --color always cargo wasi build - cargo clippy -v + cargo clippy -v --target wasm32-wasi cd ../ipfs_rpc cargo fmt --all -- --check --color always cargo wasi build - cargo clippy -v + cargo clippy -v --target wasm32-wasi # cd ../../../../tools/wit_embedder # cargo fmt --all -- --check --color always diff --git a/tools/cli/src/args.rs b/tools/cli/src/args.rs index 36cca3aa..c8eaa5f6 100644 --- a/tools/cli/src/args.rs +++ b/tools/cli/src/args.rs @@ -25,15 +25,15 @@ pub const IN_WASM_PATH: &str = "in-wasm-path"; pub fn build<'a, 'b>() -> App<'a, 'b> { SubCommand::with_name("build") .about("build provided Rust project to Wasm") - .args(&[Arg::with_name(IN_WASM_PATH) - .takes_value(true) - .short("i") - .help("path to a Cargo.toml file")]) + .setting(clap::AppSettings::TrailingVarArg) + .setting(clap::AppSettings::AllowLeadingHyphen) + .arg(Arg::from_usage("[optional]... 'cargo build arguments'").multiple(true)) } pub fn show_wit<'a, 'b>() -> App<'a, 'b> { SubCommand::with_name("show") .about("show WIT in provided Wasm file") + .setting(clap::AppSettings::ArgRequiredElseHelp) .args(&[Arg::with_name(IN_WASM_PATH) .required(true) .takes_value(true) diff --git a/tools/cli/src/build.rs b/tools/cli/src/build.rs index 7300d4ba..9e1d4ee9 100644 --- a/tools/cli/src/build.rs +++ b/tools/cli/src/build.rs @@ -17,7 +17,6 @@ use crate::Result; use crate::errors::CLIError; -use std::path::PathBuf; use std::process::Command; #[derive(serde::Deserialize)] @@ -29,15 +28,13 @@ enum DiagnosticMessage { RunWithArgs, } -pub(crate) fn build(manifest_path: Option) -> Result<()> { +pub(crate) fn build(trailing_args: Vec<&str>) -> Result<()> { use std::io::Read; let mut cargo = Command::new("cargo"); cargo.arg("build").arg("--target").arg("wasm32-wasi"); cargo.arg("--message-format").arg("json-render-diagnostics"); - if let Some(wasm_path) = manifest_path { - cargo.arg("--manifest-path").arg(wasm_path); - } + cargo.args(trailing_args); let mut process = cargo.stdout(std::process::Stdio::piped()).spawn()?; diff --git a/tools/cli/src/main.rs b/tools/cli/src/main.rs index e77dc56b..1f4ec738 100644 --- a/tools/cli/src/main.rs +++ b/tools/cli/src/main.rs @@ -29,27 +29,22 @@ mod args; mod build; mod errors; -use clap::App; -use clap::AppSettings; - pub(crate) type Result = std::result::Result; pub fn main() -> Result<()> { - let app = App::new("CLI tool for embedding WIT to provided Wasm file") + let app = clap::App::new("CLI tool for embedding WIT to provided Wasm file") .version(args::VERSION) .author(args::AUTHORS) .about(args::DESCRIPTION) - .setting(AppSettings::ArgRequiredElseHelp) .subcommand(args::build()) .subcommand(args::show_wit()); + let arg_matches = app.get_matches(); - match app.get_matches().subcommand() { - ("build", Some(arg)) => { - let manifest_path = arg - .value_of(args::IN_WASM_PATH) - .map(std::path::PathBuf::from); + match arg_matches.subcommand() { + ("build", Some(args)) => { + let trailing_args: Vec<&str> = args.values_of("optional").unwrap_or_default().collect(); - crate::build::build(manifest_path) + crate::build::build(trailing_args) } ("show", Some(arg)) => { let wasm_path = arg.value_of(args::IN_WASM_PATH).unwrap();