From 4e94c467131a7685b0a0a52b372aa0dd76abba36 Mon Sep 17 00:00:00 2001 From: Geobert Quach Date: Tue, 10 Sep 2019 12:56:45 +0100 Subject: [PATCH] refactor(args): Switch to pico-args in ra_tools --- Cargo.lock | 22 +----- crates/ra_cli/src/main.rs | 3 +- crates/ra_tools/Cargo.toml | 2 +- crates/ra_tools/src/help.rs | 56 ++++++++++++++++ crates/ra_tools/src/main.rs | 129 ++++++++++++++++++++++++++---------- 5 files changed, 153 insertions(+), 59 deletions(-) create mode 100644 crates/ra_tools/src/help.rs diff --git a/Cargo.lock b/Cargo.lock index f4b5d12a35..2e06a39b6f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -184,16 +184,6 @@ dependencies = [ "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "clap" -version = "2.33.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "clicolors-control" version = "1.0.1" @@ -1116,8 +1106,8 @@ dependencies = [ name = "ra_tools" version = "0.1.0" dependencies = [ - "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pico-args 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1598,14 +1588,6 @@ name = "text_unit" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "thread_local" version = "0.3.6" @@ -1800,7 +1782,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum chalk-rust-ir 0.1.0 (git+https://github.com/rust-lang/chalk.git)" = "" "checksum chalk-solve 0.1.0 (git+https://github.com/rust-lang/chalk.git)" = "" "checksum chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e8493056968583b0193c1bb04d6f7684586f3726992d6c573261941a895dbd68" -"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" "checksum clicolors-control 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90082ee5dcdd64dc4e9e0d37fbf3ee325419e39c0092191e0393df65518f741e" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum console 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b147390a412132d75d10dd3b7b175a69cf5fd95032f7503c7091b8831ba10242" @@ -1936,7 +1917,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" "checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625" "checksum text_unit 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e08bbcb7a3adbda0eb23431206b653bdad3d8dea311e72d36bf2215e27a42579" -"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f0c90a5f3459330ac8bc0d2f879c693bb7a2f59689c1083fc4ef83834da865" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 9e6f869c12..a9a9fbddd0 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -21,8 +21,7 @@ fn main() -> Result<()> { return Ok(()); } let subcommand = subcommand.unwrap(); - let mut args: Vec<_> = std::env::args_os().collect(); - let mut matches = Arguments::from_vec(args.drain(2..).collect()); + let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect()); match &*subcommand.to_string_lossy() { "parse" => { diff --git a/crates/ra_tools/Cargo.toml b/crates/ra_tools/Cargo.toml index b94a0b18d2..848ca408d0 100644 --- a/crates/ra_tools/Cargo.toml +++ b/crates/ra_tools/Cargo.toml @@ -8,7 +8,7 @@ publish = false [dependencies] walkdir = "2.1.3" itertools = "0.8.0" -clap = { version = "2.32.0", default-features = false } +pico-args = "0.2.0" quote = "1.0.2" proc-macro2 = "1.0.1" ron = "0.5.1" diff --git a/crates/ra_tools/src/help.rs b/crates/ra_tools/src/help.rs new file mode 100644 index 0000000000..5bfe657343 --- /dev/null +++ b/crates/ra_tools/src/help.rs @@ -0,0 +1,56 @@ +pub fn print_global_help() { + println!( + "tasks + +USAGE: + ra_tools + +FLAGS: + -h, --help Prints help information + +SUBCOMMANDS: + format + format-hook + fuzz-tests + gen-syntax + gen-tests + install-ra + lint" + ) +} + +pub fn print_install_ra_help() { + println!( + "ra_tools-install-ra + +USAGE: + ra_tools.exe install-ra [FLAGS] + +FLAGS: + --client-code + -h, --help Prints help information + --jemalloc + --server" + ) +} + +pub fn print_no_param_subcommand_help(subcommand: &str) { + println!( + "ra_tools-{} + +USAGE: + ra_tools {} + +FLAGS: + -h, --help Prints help information", + subcommand, subcommand + ); +} + +pub fn print_install_ra_conflict() { + println!( + "error: The argument `--server` cannot be used with `--client-code` + +For more information try --help" + ) +} diff --git a/crates/ra_tools/src/main.rs b/crates/ra_tools/src/main.rs index 33badf290b..5410edea9a 100644 --- a/crates/ra_tools/src/main.rs +++ b/crates/ra_tools/src/main.rs @@ -1,5 +1,8 @@ -use clap::{App, Arg, SubCommand}; +mod help; + +use core::fmt::Write; use core::str; +use pico_args::Arguments; use ra_tools::{ gen_tests, generate_boilerplate, install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, Cmd, Overwrite, Result, @@ -20,45 +23,101 @@ struct ServerOpt { } fn main() -> Result<()> { - let matches = App::new("tasks") - .setting(clap::AppSettings::SubcommandRequiredElseHelp) - .subcommand(SubCommand::with_name("gen-syntax")) - .subcommand(SubCommand::with_name("gen-tests")) - .subcommand( - SubCommand::with_name("install-ra") - .arg(Arg::with_name("server").long("--server")) - .arg(Arg::with_name("jemalloc").long("jemalloc")) - .arg(Arg::with_name("client-code").long("client-code").conflicts_with("server")), - ) - .alias("install-code") - .subcommand(SubCommand::with_name("format")) - .subcommand(SubCommand::with_name("format-hook")) - .subcommand(SubCommand::with_name("fuzz-tests")) - .subcommand(SubCommand::with_name("lint")) - .get_matches(); - match matches.subcommand() { - ("install-ra", Some(matches)) => { - let opts = InstallOpt { - client: if matches.is_present("server") { None } else { Some(ClientOpt::VsCode) }, - server: if matches.is_present("client-code") { - None - } else { - Some(ServerOpt { jemalloc: matches.is_present("jemalloc") }) - }, - }; - install(opts)? + let subcommand = std::env::args_os().nth(1); + if subcommand.is_none() { + help::print_global_help(); + return Ok(()); + } + let subcommand = subcommand.unwrap(); + let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect()); + let subcommand = &*subcommand.to_string_lossy(); + match subcommand { + "install-ra" | "install-code" => { + if matches.contains(["-h", "--help"]) { + help::print_install_ra_help(); + return Ok(()); + } else { + let server = matches.contains("--server"); + let client_code = matches.contains("--client-code"); + if server && client_code { + help::print_install_ra_conflict(); + return Ok(()); + } + let jemalloc = matches.contains("--jemalloc"); + matches.finish().or_else(handle_extra_flags)?; + let opts = InstallOpt { + client: if server { None } else { Some(ClientOpt::VsCode) }, + server: if client_code { None } else { Some(ServerOpt { jemalloc: jemalloc }) }, + }; + install(opts)? + } } - ("gen-tests", _) => gen_tests(Overwrite)?, - ("gen-syntax", _) => generate_boilerplate(Overwrite)?, - ("format", _) => run_rustfmt(Overwrite)?, - ("format-hook", _) => install_format_hook()?, - ("lint", _) => run_clippy()?, - ("fuzz-tests", _) => run_fuzzer()?, - _ => unreachable!(), + "gen-tests" => { + if matches.contains(["-h", "--help"]) { + help::print_no_param_subcommand_help(&subcommand); + return Ok(()); + } else { + gen_tests(Overwrite)? + } + } + "gen-syntax" => { + if matches.contains(["-h", "--help"]) { + help::print_no_param_subcommand_help(&subcommand); + return Ok(()); + } else { + generate_boilerplate(Overwrite)? + } + } + "format" => { + if matches.contains(["-h", "--help"]) { + help::print_no_param_subcommand_help(&subcommand); + return Ok(()); + } else { + run_rustfmt(Overwrite)? + } + } + "format-hook" => { + if matches.contains(["-h", "--help"]) { + help::print_no_param_subcommand_help(&subcommand); + return Ok(()); + } else { + install_format_hook()? + } + } + "lint" => { + if matches.contains(["-h", "--help"]) { + help::print_no_param_subcommand_help(&subcommand); + return Ok(()); + } else { + run_clippy()? + } + } + "fuzz-tests" => { + if matches.contains(["-h", "--help"]) { + help::print_no_param_subcommand_help(&subcommand); + return Ok(()); + } else { + run_fuzzer()? + } + } + _ => help::print_global_help(), } Ok(()) } +fn handle_extra_flags(e: pico_args::Error) -> Result<()> { + if let pico_args::Error::UnusedArgsLeft(flags) = e { + let mut invalid_flags = String::new(); + for flag in flags { + write!(&mut invalid_flags, "{}, ", flag).expect("Error on write"); + } + let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2); + Err(format!("Invalid flags: {}", invalid_flags).into()) + } else { + Err(e.to_string().into()) + } +} + fn install(opts: InstallOpt) -> Result<()> { if cfg!(target_os = "macos") { fix_path_for_mac()?