From 4e94c467131a7685b0a0a52b372aa0dd76abba36 Mon Sep 17 00:00:00 2001 From: Geobert Quach Date: Tue, 10 Sep 2019 12:56:45 +0100 Subject: refactor(args): Switch to pico-args in ra_tools --- 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 ++++++++++++++++++++++++++++++++------------ 4 files changed, 152 insertions(+), 38 deletions(-) create mode 100644 crates/ra_tools/src/help.rs (limited to 'crates') diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 9e6f869c1..a9a9fbddd 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 b94a0b18d..848ca408d 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 000000000..5bfe65734 --- /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 33badf290..5410edea9 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" => { + 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()? + } } - ("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!(), + "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()? -- cgit v1.2.3