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 | |
parent | 735845d86e6ab94891e421823158f374e2f3412a (diff) |
refactor(args): Switch to pico-args in ra_tools
-rw-r--r-- | Cargo.lock | 22 | ||||
-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 |
5 files changed, 153 insertions, 59 deletions
diff --git a/Cargo.lock b/Cargo.lock index f4b5d12a3..2e06a39b6 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -185,16 +185,6 @@ dependencies = [ | |||
185 | ] | 185 | ] |
186 | 186 | ||
187 | [[package]] | 187 | [[package]] |
188 | name = "clap" | ||
189 | version = "2.33.0" | ||
190 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
191 | dependencies = [ | ||
192 | "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
193 | "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
194 | "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", | ||
195 | ] | ||
196 | |||
197 | [[package]] | ||
198 | name = "clicolors-control" | 188 | name = "clicolors-control" |
199 | version = "1.0.1" | 189 | version = "1.0.1" |
200 | source = "registry+https://github.com/rust-lang/crates.io-index" | 190 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1116,8 +1106,8 @@ dependencies = [ | |||
1116 | name = "ra_tools" | 1106 | name = "ra_tools" |
1117 | version = "0.1.0" | 1107 | version = "0.1.0" |
1118 | dependencies = [ | 1108 | dependencies = [ |
1119 | "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1120 | "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", | 1109 | "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", |
1110 | "pico-args 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1121 | "proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", | 1111 | "proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", |
1122 | "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", | 1112 | "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", |
1123 | "ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", | 1113 | "ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", |
@@ -1599,14 +1589,6 @@ version = "0.1.9" | |||
1599 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1589 | source = "registry+https://github.com/rust-lang/crates.io-index" |
1600 | 1590 | ||
1601 | [[package]] | 1591 | [[package]] |
1602 | name = "textwrap" | ||
1603 | version = "0.11.0" | ||
1604 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1605 | dependencies = [ | ||
1606 | "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1607 | ] | ||
1608 | |||
1609 | [[package]] | ||
1610 | name = "thread_local" | 1592 | name = "thread_local" |
1611 | version = "0.3.6" | 1593 | version = "0.3.6" |
1612 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1594 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1800,7 +1782,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
1800 | "checksum chalk-rust-ir 0.1.0 (git+https://github.com/rust-lang/chalk.git)" = "<none>" | 1782 | "checksum chalk-rust-ir 0.1.0 (git+https://github.com/rust-lang/chalk.git)" = "<none>" |
1801 | "checksum chalk-solve 0.1.0 (git+https://github.com/rust-lang/chalk.git)" = "<none>" | 1783 | "checksum chalk-solve 0.1.0 (git+https://github.com/rust-lang/chalk.git)" = "<none>" |
1802 | "checksum chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e8493056968583b0193c1bb04d6f7684586f3726992d6c573261941a895dbd68" | 1784 | "checksum chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e8493056968583b0193c1bb04d6f7684586f3726992d6c573261941a895dbd68" |
1803 | "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" | ||
1804 | "checksum clicolors-control 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90082ee5dcdd64dc4e9e0d37fbf3ee325419e39c0092191e0393df65518f741e" | 1785 | "checksum clicolors-control 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90082ee5dcdd64dc4e9e0d37fbf3ee325419e39c0092191e0393df65518f741e" |
1805 | "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" | 1786 | "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" |
1806 | "checksum console 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b147390a412132d75d10dd3b7b175a69cf5fd95032f7503c7091b8831ba10242" | 1787 | "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" | |||
1936 | "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" | 1917 | "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" |
1937 | "checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625" | 1918 | "checksum termios 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "72b620c5ea021d75a735c943269bb07d30c9b77d6ac6b236bc8b5c496ef05625" |
1938 | "checksum text_unit 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e08bbcb7a3adbda0eb23431206b653bdad3d8dea311e72d36bf2215e27a42579" | 1919 | "checksum text_unit 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e08bbcb7a3adbda0eb23431206b653bdad3d8dea311e72d36bf2215e27a42579" |
1939 | "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" | ||
1940 | "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" | 1920 | "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" |
1941 | "checksum threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f0c90a5f3459330ac8bc0d2f879c693bb7a2f59689c1083fc4ef83834da865" | 1921 | "checksum threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f0c90a5f3459330ac8bc0d2f879c693bb7a2f59689c1083fc4ef83834da865" |
1942 | "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" | 1922 | "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 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()? |