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_tools/Cargo.toml | 2 +- crates/ra_tools/src/help.rs | 56 +++++++++++++++++++ crates/ra_tools/src/main.rs | 129 ++++++++++++++++++++++++++++++++------------ 3 files changed, 151 insertions(+), 36 deletions(-) create mode 100644 crates/ra_tools/src/help.rs (limited to 'crates/ra_tools') 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 From aa894ace1fc4600dbc94bccb05532154ee2288d8 Mon Sep 17 00:00:00 2001 From: Geobert Quach Date: Tue, 10 Sep 2019 16:17:11 +0100 Subject: refactor(args): Apply comments --- crates/ra_tools/src/help.rs | 27 ++++++------------- crates/ra_tools/src/main.rs | 64 ++++++++++++++++++++------------------------- 2 files changed, 37 insertions(+), 54 deletions(-) (limited to 'crates/ra_tools') diff --git a/crates/ra_tools/src/help.rs b/crates/ra_tools/src/help.rs index 5bfe65734..6dde6c2d2 100644 --- a/crates/ra_tools/src/help.rs +++ b/crates/ra_tools/src/help.rs @@ -1,6 +1,4 @@ -pub fn print_global_help() { - println!( - "tasks +pub const GLOBAL_HELP: &str = "tasks USAGE: ra_tools @@ -15,13 +13,9 @@ SUBCOMMANDS: gen-syntax gen-tests install-ra - lint" - ) -} + lint"; -pub fn print_install_ra_help() { - println!( - "ra_tools-install-ra +pub const INSTALL_RA_HELP: &str = "ra_tools-install-ra USAGE: ra_tools.exe install-ra [FLAGS] @@ -30,12 +24,10 @@ FLAGS: --client-code -h, --help Prints help information --jemalloc - --server" - ) -} + --server"; pub fn print_no_param_subcommand_help(subcommand: &str) { - println!( + eprintln!( "ra_tools-{} USAGE: @@ -47,10 +39,7 @@ FLAGS: ); } -pub fn print_install_ra_conflict() { - println!( - "error: The argument `--server` cannot be used with `--client-code` +pub const INSTALL_RA_CONFLICT: &str = + "error: The argument `--server` cannot be used with `--client-code` -For more information try --help" - ) -} +For more information try --help"; diff --git a/crates/ra_tools/src/main.rs b/crates/ra_tools/src/main.rs index 5410edea9..f96f1875f 100644 --- a/crates/ra_tools/src/main.rs +++ b/crates/ra_tools/src/main.rs @@ -23,84 +23,78 @@ struct ServerOpt { } fn main() -> Result<()> { - let subcommand = std::env::args_os().nth(1); - if subcommand.is_none() { - help::print_global_help(); - return Ok(()); - } - let subcommand = subcommand.unwrap(); + let subcommand = match std::env::args_os().nth(1) { + None => { + eprintln!("{}", help::GLOBAL_HELP); + return Ok(()); + } + Some(s) => s, + }; 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(); + eprintln!("{}", help::INSTALL_RA_HELP); + return Ok(()); + } + let server = matches.contains("--server"); + let client_code = matches.contains("--client-code"); + if server && client_code { + eprintln!("{}", help::INSTALL_RA_CONFLICT); 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)? } + 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_tests(Overwrite)? } "gen-syntax" => { if matches.contains(["-h", "--help"]) { help::print_no_param_subcommand_help(&subcommand); return Ok(()); - } else { - generate_boilerplate(Overwrite)? } + generate_boilerplate(Overwrite)? } "format" => { if matches.contains(["-h", "--help"]) { help::print_no_param_subcommand_help(&subcommand); return Ok(()); - } else { - run_rustfmt(Overwrite)? } + run_rustfmt(Overwrite)? } "format-hook" => { if matches.contains(["-h", "--help"]) { help::print_no_param_subcommand_help(&subcommand); return Ok(()); - } else { - install_format_hook()? } + install_format_hook()? } "lint" => { if matches.contains(["-h", "--help"]) { help::print_no_param_subcommand_help(&subcommand); return Ok(()); - } else { - run_clippy()? } + run_clippy()? } "fuzz-tests" => { if matches.contains(["-h", "--help"]) { help::print_no_param_subcommand_help(&subcommand); return Ok(()); - } else { - run_fuzzer()? } + run_fuzzer()? } - _ => help::print_global_help(), + _ => eprintln!("{}", help::GLOBAL_HELP), } Ok(()) } @@ -109,7 +103,7 @@ 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"); + write!(&mut invalid_flags, "{}, ", flag)?; } let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2); Err(format!("Invalid flags: {}", invalid_flags).into()) -- cgit v1.2.3