diff options
Diffstat (limited to 'crates/ra_tools/src')
-rw-r--r-- | crates/ra_tools/src/help.rs | 45 | ||||
-rw-r--r-- | crates/ra_tools/src/main.rs | 117 |
2 files changed, 130 insertions, 32 deletions
diff --git a/crates/ra_tools/src/help.rs b/crates/ra_tools/src/help.rs new file mode 100644 index 000000000..6dde6c2d2 --- /dev/null +++ b/crates/ra_tools/src/help.rs | |||
@@ -0,0 +1,45 @@ | |||
1 | pub const GLOBAL_HELP: &str = "tasks | ||
2 | |||
3 | USAGE: | ||
4 | ra_tools <SUBCOMMAND> | ||
5 | |||
6 | FLAGS: | ||
7 | -h, --help Prints help information | ||
8 | |||
9 | SUBCOMMANDS: | ||
10 | format | ||
11 | format-hook | ||
12 | fuzz-tests | ||
13 | gen-syntax | ||
14 | gen-tests | ||
15 | install-ra | ||
16 | lint"; | ||
17 | |||
18 | pub const INSTALL_RA_HELP: &str = "ra_tools-install-ra | ||
19 | |||
20 | USAGE: | ||
21 | ra_tools.exe install-ra [FLAGS] | ||
22 | |||
23 | FLAGS: | ||
24 | --client-code | ||
25 | -h, --help Prints help information | ||
26 | --jemalloc | ||
27 | --server"; | ||
28 | |||
29 | pub fn print_no_param_subcommand_help(subcommand: &str) { | ||
30 | eprintln!( | ||
31 | "ra_tools-{} | ||
32 | |||
33 | USAGE: | ||
34 | ra_tools {} | ||
35 | |||
36 | FLAGS: | ||
37 | -h, --help Prints help information", | ||
38 | subcommand, subcommand | ||
39 | ); | ||
40 | } | ||
41 | |||
42 | pub const INSTALL_RA_CONFLICT: &str = | ||
43 | "error: The argument `--server` cannot be used with `--client-code` | ||
44 | |||
45 | For more information try --help"; | ||
diff --git a/crates/ra_tools/src/main.rs b/crates/ra_tools/src/main.rs index 33badf290..f96f1875f 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,95 @@ struct ServerOpt { | |||
20 | } | 23 | } |
21 | 24 | ||
22 | fn main() -> Result<()> { | 25 | fn main() -> Result<()> { |
23 | let matches = App::new("tasks") | 26 | let subcommand = match std::env::args_os().nth(1) { |
24 | .setting(clap::AppSettings::SubcommandRequiredElseHelp) | 27 | None => { |
25 | .subcommand(SubCommand::with_name("gen-syntax")) | 28 | eprintln!("{}", help::GLOBAL_HELP); |
26 | .subcommand(SubCommand::with_name("gen-tests")) | 29 | return Ok(()); |
27 | .subcommand( | 30 | } |
28 | SubCommand::with_name("install-ra") | 31 | Some(s) => s, |
29 | .arg(Arg::with_name("server").long("--server")) | 32 | }; |
30 | .arg(Arg::with_name("jemalloc").long("jemalloc")) | 33 | let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect()); |
31 | .arg(Arg::with_name("client-code").long("client-code").conflicts_with("server")), | 34 | let subcommand = &*subcommand.to_string_lossy(); |
32 | ) | 35 | match subcommand { |
33 | .alias("install-code") | 36 | "install-ra" | "install-code" => { |
34 | .subcommand(SubCommand::with_name("format")) | 37 | if matches.contains(["-h", "--help"]) { |
35 | .subcommand(SubCommand::with_name("format-hook")) | 38 | eprintln!("{}", help::INSTALL_RA_HELP); |
36 | .subcommand(SubCommand::with_name("fuzz-tests")) | 39 | return Ok(()); |
37 | .subcommand(SubCommand::with_name("lint")) | 40 | } |
38 | .get_matches(); | 41 | let server = matches.contains("--server"); |
39 | match matches.subcommand() { | 42 | let client_code = matches.contains("--client-code"); |
40 | ("install-ra", Some(matches)) => { | 43 | if server && client_code { |
44 | eprintln!("{}", help::INSTALL_RA_CONFLICT); | ||
45 | return Ok(()); | ||
46 | } | ||
47 | let jemalloc = matches.contains("--jemalloc"); | ||
48 | matches.finish().or_else(handle_extra_flags)?; | ||
41 | let opts = InstallOpt { | 49 | let opts = InstallOpt { |
42 | client: if matches.is_present("server") { None } else { Some(ClientOpt::VsCode) }, | 50 | client: if server { None } else { Some(ClientOpt::VsCode) }, |
43 | server: if matches.is_present("client-code") { | 51 | server: if client_code { None } else { Some(ServerOpt { jemalloc: jemalloc }) }, |
44 | None | ||
45 | } else { | ||
46 | Some(ServerOpt { jemalloc: matches.is_present("jemalloc") }) | ||
47 | }, | ||
48 | }; | 52 | }; |
49 | install(opts)? | 53 | install(opts)? |
50 | } | 54 | } |
51 | ("gen-tests", _) => gen_tests(Overwrite)?, | 55 | "gen-tests" => { |
52 | ("gen-syntax", _) => generate_boilerplate(Overwrite)?, | 56 | if matches.contains(["-h", "--help"]) { |
53 | ("format", _) => run_rustfmt(Overwrite)?, | 57 | help::print_no_param_subcommand_help(&subcommand); |
54 | ("format-hook", _) => install_format_hook()?, | 58 | return Ok(()); |
55 | ("lint", _) => run_clippy()?, | 59 | } |
56 | ("fuzz-tests", _) => run_fuzzer()?, | 60 | gen_tests(Overwrite)? |
57 | _ => unreachable!(), | 61 | } |
62 | "gen-syntax" => { | ||
63 | if matches.contains(["-h", "--help"]) { | ||
64 | help::print_no_param_subcommand_help(&subcommand); | ||
65 | return Ok(()); | ||
66 | } | ||
67 | generate_boilerplate(Overwrite)? | ||
68 | } | ||
69 | "format" => { | ||
70 | if matches.contains(["-h", "--help"]) { | ||
71 | help::print_no_param_subcommand_help(&subcommand); | ||
72 | return Ok(()); | ||
73 | } | ||
74 | run_rustfmt(Overwrite)? | ||
75 | } | ||
76 | "format-hook" => { | ||
77 | if matches.contains(["-h", "--help"]) { | ||
78 | help::print_no_param_subcommand_help(&subcommand); | ||
79 | return Ok(()); | ||
80 | } | ||
81 | install_format_hook()? | ||
82 | } | ||
83 | "lint" => { | ||
84 | if matches.contains(["-h", "--help"]) { | ||
85 | help::print_no_param_subcommand_help(&subcommand); | ||
86 | return Ok(()); | ||
87 | } | ||
88 | run_clippy()? | ||
89 | } | ||
90 | "fuzz-tests" => { | ||
91 | if matches.contains(["-h", "--help"]) { | ||
92 | help::print_no_param_subcommand_help(&subcommand); | ||
93 | return Ok(()); | ||
94 | } | ||
95 | run_fuzzer()? | ||
96 | } | ||
97 | _ => eprintln!("{}", help::GLOBAL_HELP), | ||
58 | } | 98 | } |
59 | Ok(()) | 99 | Ok(()) |
60 | } | 100 | } |
61 | 101 | ||
102 | fn handle_extra_flags(e: pico_args::Error) -> Result<()> { | ||
103 | if let pico_args::Error::UnusedArgsLeft(flags) = e { | ||
104 | let mut invalid_flags = String::new(); | ||
105 | for flag in flags { | ||
106 | write!(&mut invalid_flags, "{}, ", flag)?; | ||
107 | } | ||
108 | let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2); | ||
109 | Err(format!("Invalid flags: {}", invalid_flags).into()) | ||
110 | } else { | ||
111 | Err(e.to_string().into()) | ||
112 | } | ||
113 | } | ||
114 | |||
62 | fn install(opts: InstallOpt) -> Result<()> { | 115 | fn install(opts: InstallOpt) -> Result<()> { |
63 | if cfg!(target_os = "macos") { | 116 | if cfg!(target_os = "macos") { |
64 | fix_path_for_mac()? | 117 | fix_path_for_mac()? |