diff options
author | Geobert Quach <[email protected]> | 2019-09-10 12:56:45 +0100 |
---|---|---|
committer | Geobert Quach <[email protected]> | 2019-09-10 12:56:45 +0100 |
commit | 4e94c467131a7685b0a0a52b372aa0dd76abba36 (patch) | |
tree | 7dfba21ad858ab77fd2a26ff9deec8459dfffe43 /crates | |
parent | 735845d86e6ab94891e421823158f374e2f3412a (diff) |
refactor(args): Switch to pico-args in ra_tools
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_cli/src/main.rs | 3 | ||||
-rw-r--r-- | crates/ra_tools/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_tools/src/help.rs | 56 | ||||
-rw-r--r-- | crates/ra_tools/src/main.rs | 129 |
4 files changed, 152 insertions, 38 deletions
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<()> { | |||
21 | return Ok(()); | 21 | return Ok(()); |
22 | } | 22 | } |
23 | let subcommand = subcommand.unwrap(); | 23 | let subcommand = subcommand.unwrap(); |
24 | let mut args: Vec<_> = std::env::args_os().collect(); | 24 | let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect()); |
25 | let mut matches = Arguments::from_vec(args.drain(2..).collect()); | ||
26 | 25 | ||
27 | match &*subcommand.to_string_lossy() { | 26 | match &*subcommand.to_string_lossy() { |
28 | "parse" => { | 27 | "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 | |||
8 | [dependencies] | 8 | [dependencies] |
9 | walkdir = "2.1.3" | 9 | walkdir = "2.1.3" |
10 | itertools = "0.8.0" | 10 | itertools = "0.8.0" |
11 | clap = { version = "2.32.0", default-features = false } | 11 | pico-args = "0.2.0" |
12 | quote = "1.0.2" | 12 | quote = "1.0.2" |
13 | proc-macro2 = "1.0.1" | 13 | proc-macro2 = "1.0.1" |
14 | ron = "0.5.1" | 14 | 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 @@ | |||
1 | pub fn print_global_help() { | ||
2 | println!( | ||
3 | "tasks | ||
4 | |||
5 | USAGE: | ||
6 | ra_tools <SUBCOMMAND> | ||
7 | |||
8 | FLAGS: | ||
9 | -h, --help Prints help information | ||
10 | |||
11 | SUBCOMMANDS: | ||
12 | format | ||
13 | format-hook | ||
14 | fuzz-tests | ||
15 | gen-syntax | ||
16 | gen-tests | ||
17 | install-ra | ||
18 | lint" | ||
19 | ) | ||
20 | } | ||
21 | |||
22 | pub fn print_install_ra_help() { | ||
23 | println!( | ||
24 | "ra_tools-install-ra | ||
25 | |||
26 | USAGE: | ||
27 | ra_tools.exe install-ra [FLAGS] | ||
28 | |||
29 | FLAGS: | ||
30 | --client-code | ||
31 | -h, --help Prints help information | ||
32 | --jemalloc | ||
33 | --server" | ||
34 | ) | ||
35 | } | ||
36 | |||
37 | pub fn print_no_param_subcommand_help(subcommand: &str) { | ||
38 | println!( | ||
39 | "ra_tools-{} | ||
40 | |||
41 | USAGE: | ||
42 | ra_tools {} | ||
43 | |||
44 | FLAGS: | ||
45 | -h, --help Prints help information", | ||
46 | subcommand, subcommand | ||
47 | ); | ||
48 | } | ||
49 | |||
50 | pub fn print_install_ra_conflict() { | ||
51 | println!( | ||
52 | "error: The argument `--server` cannot be used with `--client-code` | ||
53 | |||
54 | For more information try --help" | ||
55 | ) | ||
56 | } | ||
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 @@ | |||
1 | use clap::{App, Arg, SubCommand}; | 1 | mod help; |
2 | |||
3 | use core::fmt::Write; | ||
2 | use core::str; | 4 | use core::str; |
5 | use pico_args::Arguments; | ||
3 | use ra_tools::{ | 6 | use ra_tools::{ |
4 | gen_tests, generate_boilerplate, install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, | 7 | gen_tests, generate_boilerplate, install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, |
5 | Cmd, Overwrite, Result, | 8 | Cmd, Overwrite, Result, |
@@ -20,45 +23,101 @@ struct ServerOpt { | |||
20 | } | 23 | } |
21 | 24 | ||
22 | fn main() -> Result<()> { | 25 | fn main() -> Result<()> { |
23 | let matches = App::new("tasks") | 26 | let subcommand = std::env::args_os().nth(1); |
24 | .setting(clap::AppSettings::SubcommandRequiredElseHelp) | 27 | if subcommand.is_none() { |
25 | .subcommand(SubCommand::with_name("gen-syntax")) | 28 | help::print_global_help(); |
26 | .subcommand(SubCommand::with_name("gen-tests")) | 29 | return Ok(()); |
27 | .subcommand( | 30 | } |
28 | SubCommand::with_name("install-ra") | 31 | let subcommand = subcommand.unwrap(); |
29 | .arg(Arg::with_name("server").long("--server")) | 32 | let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect()); |
30 | .arg(Arg::with_name("jemalloc").long("jemalloc")) | 33 | let subcommand = &*subcommand.to_string_lossy(); |
31 | .arg(Arg::with_name("client-code").long("client-code").conflicts_with("server")), | 34 | match subcommand { |
32 | ) | 35 | "install-ra" | "install-code" => { |
33 | .alias("install-code") | 36 | if matches.contains(["-h", "--help"]) { |
34 | .subcommand(SubCommand::with_name("format")) | 37 | help::print_install_ra_help(); |
35 | .subcommand(SubCommand::with_name("format-hook")) | 38 | return Ok(()); |
36 | .subcommand(SubCommand::with_name("fuzz-tests")) | 39 | } else { |
37 | .subcommand(SubCommand::with_name("lint")) | 40 | let server = matches.contains("--server"); |
38 | .get_matches(); | 41 | let client_code = matches.contains("--client-code"); |
39 | match matches.subcommand() { | 42 | if server && client_code { |
40 | ("install-ra", Some(matches)) => { | 43 | help::print_install_ra_conflict(); |
41 | let opts = InstallOpt { | 44 | return Ok(()); |
42 | client: if matches.is_present("server") { None } else { Some(ClientOpt::VsCode) }, | 45 | } |
43 | server: if matches.is_present("client-code") { | 46 | let jemalloc = matches.contains("--jemalloc"); |
44 | None | 47 | matches.finish().or_else(handle_extra_flags)?; |
45 | } else { | 48 | let opts = InstallOpt { |
46 | Some(ServerOpt { jemalloc: matches.is_present("jemalloc") }) | 49 | client: if server { None } else { Some(ClientOpt::VsCode) }, |
47 | }, | 50 | server: if client_code { None } else { Some(ServerOpt { jemalloc: jemalloc }) }, |
48 | }; | 51 | }; |
49 | install(opts)? | 52 | install(opts)? |
53 | } | ||
54 | } | ||
55 | "gen-tests" => { | ||
56 | if matches.contains(["-h", "--help"]) { | ||
57 | help::print_no_param_subcommand_help(&subcommand); | ||
58 | return Ok(()); | ||
59 | } else { | ||
60 | gen_tests(Overwrite)? | ||
61 | } | ||
62 | } | ||
63 | "gen-syntax" => { | ||
64 | if matches.contains(["-h", "--help"]) { | ||
65 | help::print_no_param_subcommand_help(&subcommand); | ||
66 | return Ok(()); | ||
67 | } else { | ||
68 | generate_boilerplate(Overwrite)? | ||
69 | } | ||
70 | } | ||
71 | "format" => { | ||
72 | if matches.contains(["-h", "--help"]) { | ||
73 | help::print_no_param_subcommand_help(&subcommand); | ||
74 | return Ok(()); | ||
75 | } else { | ||
76 | run_rustfmt(Overwrite)? | ||
77 | } | ||
78 | } | ||
79 | "format-hook" => { | ||
80 | if matches.contains(["-h", "--help"]) { | ||
81 | help::print_no_param_subcommand_help(&subcommand); | ||
82 | return Ok(()); | ||
83 | } else { | ||
84 | install_format_hook()? | ||
85 | } | ||
86 | } | ||
87 | "lint" => { | ||
88 | if matches.contains(["-h", "--help"]) { | ||
89 | help::print_no_param_subcommand_help(&subcommand); | ||
90 | return Ok(()); | ||
91 | } else { | ||
92 | run_clippy()? | ||
93 | } | ||
50 | } | 94 | } |
51 | ("gen-tests", _) => gen_tests(Overwrite)?, | 95 | "fuzz-tests" => { |
52 | ("gen-syntax", _) => generate_boilerplate(Overwrite)?, | 96 | if matches.contains(["-h", "--help"]) { |
53 | ("format", _) => run_rustfmt(Overwrite)?, | 97 | help::print_no_param_subcommand_help(&subcommand); |
54 | ("format-hook", _) => install_format_hook()?, | 98 | return Ok(()); |
55 | ("lint", _) => run_clippy()?, | 99 | } else { |
56 | ("fuzz-tests", _) => run_fuzzer()?, | 100 | run_fuzzer()? |
57 | _ => unreachable!(), | 101 | } |
102 | } | ||
103 | _ => help::print_global_help(), | ||
58 | } | 104 | } |
59 | Ok(()) | 105 | Ok(()) |
60 | } | 106 | } |
61 | 107 | ||
108 | fn handle_extra_flags(e: pico_args::Error) -> Result<()> { | ||
109 | if let pico_args::Error::UnusedArgsLeft(flags) = e { | ||
110 | let mut invalid_flags = String::new(); | ||
111 | for flag in flags { | ||
112 | write!(&mut invalid_flags, "{}, ", flag).expect("Error on write"); | ||
113 | } | ||
114 | let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2); | ||
115 | Err(format!("Invalid flags: {}", invalid_flags).into()) | ||
116 | } else { | ||
117 | Err(e.to_string().into()) | ||
118 | } | ||
119 | } | ||
120 | |||
62 | fn install(opts: InstallOpt) -> Result<()> { | 121 | fn install(opts: InstallOpt) -> Result<()> { |
63 | if cfg!(target_os = "macos") { | 122 | if cfg!(target_os = "macos") { |
64 | fix_path_for_mac()? | 123 | fix_path_for_mac()? |