diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-08-01 17:47:51 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-08-01 17:47:51 +0100 |
commit | e4d0f19b01e5ba5007073f126b7eb0276c79024f (patch) | |
tree | 21be486aaf2aa28843ea1adc705507d7bdc41b00 | |
parent | cf17366df97d460f246f69d06fbfb129074d1a5c (diff) | |
parent | 562491b16bcf54a4cde4a61e4fd078af1532cccc (diff) |
Merge #5638
5638: Simplify argument parsing r=matklad a=lnicola
and use `Display` when printing errors.
Co-authored-by: Laurențiu Nicola <[email protected]>
-rw-r--r-- | crates/rust-analyzer/src/bin/args.rs | 32 | ||||
-rw-r--r-- | crates/rust-analyzer/src/bin/main.rs | 19 |
2 files changed, 27 insertions, 24 deletions
diff --git a/crates/rust-analyzer/src/bin/args.rs b/crates/rust-analyzer/src/bin/args.rs index f16e35d86..d3081e88b 100644 --- a/crates/rust-analyzer/src/bin/args.rs +++ b/crates/rust-analyzer/src/bin/args.rs | |||
@@ -44,15 +44,16 @@ pub(crate) enum Command { | |||
44 | ProcMacro, | 44 | ProcMacro, |
45 | RunServer, | 45 | RunServer, |
46 | Version, | 46 | Version, |
47 | Help, | ||
47 | } | 48 | } |
48 | 49 | ||
49 | impl Args { | 50 | impl Args { |
50 | pub(crate) fn parse() -> Result<Result<Args, HelpPrinted>> { | 51 | pub(crate) fn parse() -> Result<Args> { |
51 | let mut matches = Arguments::from_env(); | 52 | let mut matches = Arguments::from_env(); |
52 | 53 | ||
53 | if matches.contains("--version") { | 54 | if matches.contains("--version") { |
54 | matches.finish().or_else(handle_extra_flags)?; | 55 | matches.finish().or_else(handle_extra_flags)?; |
55 | return Ok(Ok(Args { verbosity: Verbosity::Normal, command: Command::Version })); | 56 | return Ok(Args { verbosity: Verbosity::Normal, command: Command::Version }); |
56 | } | 57 | } |
57 | 58 | ||
58 | let verbosity = match ( | 59 | let verbosity = match ( |
@@ -68,15 +69,16 @@ impl Args { | |||
68 | (false, true, true) => bail!("Invalid flags: -q conflicts with -v"), | 69 | (false, true, true) => bail!("Invalid flags: -q conflicts with -v"), |
69 | }; | 70 | }; |
70 | 71 | ||
72 | let help = Ok(Args { verbosity, command: Command::Help }); | ||
71 | let subcommand = match matches.subcommand()? { | 73 | let subcommand = match matches.subcommand()? { |
72 | Some(it) => it, | 74 | Some(it) => it, |
73 | None => { | 75 | None => { |
74 | if matches.contains(["-h", "--help"]) { | 76 | if matches.contains(["-h", "--help"]) { |
75 | print_subcommands(); | 77 | print_subcommands(); |
76 | return Ok(Err(HelpPrinted)); | 78 | return help; |
77 | } | 79 | } |
78 | matches.finish().or_else(handle_extra_flags)?; | 80 | matches.finish().or_else(handle_extra_flags)?; |
79 | return Ok(Ok(Args { verbosity, command: Command::RunServer })); | 81 | return Ok(Args { verbosity, command: Command::RunServer }); |
80 | } | 82 | } |
81 | }; | 83 | }; |
82 | let command = match subcommand.as_str() { | 84 | let command = match subcommand.as_str() { |
@@ -93,7 +95,7 @@ FLAGS: | |||
93 | -h, --help Prints help information | 95 | -h, --help Prints help information |
94 | --no-dump" | 96 | --no-dump" |
95 | ); | 97 | ); |
96 | return Ok(Err(HelpPrinted)); | 98 | return help; |
97 | } | 99 | } |
98 | 100 | ||
99 | let no_dump = matches.contains("--no-dump"); | 101 | let no_dump = matches.contains("--no-dump"); |
@@ -112,7 +114,7 @@ USAGE: | |||
112 | FLAGS: | 114 | FLAGS: |
113 | -h, --help Prints help inforamtion" | 115 | -h, --help Prints help inforamtion" |
114 | ); | 116 | ); |
115 | return Ok(Err(HelpPrinted)); | 117 | return help; |
116 | } | 118 | } |
117 | 119 | ||
118 | matches.finish().or_else(handle_extra_flags)?; | 120 | matches.finish().or_else(handle_extra_flags)?; |
@@ -132,7 +134,7 @@ FLAGS: | |||
132 | -h, --help Prints help information | 134 | -h, --help Prints help information |
133 | -r, --rainbow" | 135 | -r, --rainbow" |
134 | ); | 136 | ); |
135 | return Ok(Err(HelpPrinted)); | 137 | return help; |
136 | } | 138 | } |
137 | 139 | ||
138 | let rainbow = matches.contains(["-r", "--rainbow"]); | 140 | let rainbow = matches.contains(["-r", "--rainbow"]); |
@@ -166,7 +168,7 @@ OPTIONS: | |||
166 | ARGS: | 168 | ARGS: |
167 | <PATH>" | 169 | <PATH>" |
168 | ); | 170 | ); |
169 | return Ok(Err(HelpPrinted)); | 171 | return help; |
170 | } | 172 | } |
171 | 173 | ||
172 | let randomize = matches.contains("--randomize"); | 174 | let randomize = matches.contains("--randomize"); |
@@ -220,7 +222,7 @@ OPTIONS: | |||
220 | ARGS: | 222 | ARGS: |
221 | <PATH> Project to analyse" | 223 | <PATH> Project to analyse" |
222 | ); | 224 | ); |
223 | return Ok(Err(HelpPrinted)); | 225 | return help; |
224 | } | 226 | } |
225 | 227 | ||
226 | let path: PathBuf = matches.opt_value_from_str("--project")?.unwrap_or_default(); | 228 | let path: PathBuf = matches.opt_value_from_str("--project")?.unwrap_or_default(); |
@@ -266,7 +268,7 @@ FLAGS: | |||
266 | ARGS: | 268 | ARGS: |
267 | <PATH>" | 269 | <PATH>" |
268 | ); | 270 | ); |
269 | return Ok(Err(HelpPrinted)); | 271 | return help; |
270 | } | 272 | } |
271 | 273 | ||
272 | let load_output_dirs = matches.contains("--load-output-dirs"); | 274 | let load_output_dirs = matches.contains("--load-output-dirs"); |
@@ -302,7 +304,7 @@ FLAGS: | |||
302 | ARGS: | 304 | ARGS: |
303 | <RULE> A structured search replace rule" | 305 | <RULE> A structured search replace rule" |
304 | ); | 306 | ); |
305 | return Ok(Err(HelpPrinted)); | 307 | return help; |
306 | } | 308 | } |
307 | let mut rules = Vec::new(); | 309 | let mut rules = Vec::new(); |
308 | while let Some(rule) = matches.free_from_str()? { | 310 | while let Some(rule) = matches.free_from_str()? { |
@@ -329,7 +331,7 @@ FLAGS: | |||
329 | ARGS: | 331 | ARGS: |
330 | <PATTERN> A structured search pattern" | 332 | <PATTERN> A structured search pattern" |
331 | ); | 333 | ); |
332 | return Ok(Err(HelpPrinted)); | 334 | return help; |
333 | } | 335 | } |
334 | let debug_snippet = matches.opt_value_from_str("--debug")?; | 336 | let debug_snippet = matches.opt_value_from_str("--debug")?; |
335 | let mut patterns = Vec::new(); | 337 | let mut patterns = Vec::new(); |
@@ -340,10 +342,10 @@ ARGS: | |||
340 | } | 342 | } |
341 | _ => { | 343 | _ => { |
342 | print_subcommands(); | 344 | print_subcommands(); |
343 | return Ok(Err(HelpPrinted)); | 345 | return help; |
344 | } | 346 | } |
345 | }; | 347 | }; |
346 | Ok(Ok(Args { verbosity, command })) | 348 | Ok(Args { verbosity, command }) |
347 | } | 349 | } |
348 | } | 350 | } |
349 | 351 | ||
@@ -371,8 +373,6 @@ SUBCOMMANDS: | |||
371 | ) | 373 | ) |
372 | } | 374 | } |
373 | 375 | ||
374 | pub(crate) struct HelpPrinted; | ||
375 | |||
376 | fn handle_extra_flags(e: pico_args::Error) -> Result<()> { | 376 | fn handle_extra_flags(e: pico_args::Error) -> Result<()> { |
377 | if let pico_args::Error::UnusedArgsLeft(flags) = e { | 377 | if let pico_args::Error::UnusedArgsLeft(flags) = e { |
378 | let mut invalid_flags = String::new(); | 378 | let mut invalid_flags = String::new(); |
diff --git a/crates/rust-analyzer/src/bin/main.rs b/crates/rust-analyzer/src/bin/main.rs index ff8234495..fc7f8b01d 100644 --- a/crates/rust-analyzer/src/bin/main.rs +++ b/crates/rust-analyzer/src/bin/main.rs | |||
@@ -3,7 +3,7 @@ | |||
3 | //! Based on cli flags, either spawns an LSP server, or runs a batch analysis | 3 | //! Based on cli flags, either spawns an LSP server, or runs a batch analysis |
4 | mod args; | 4 | mod args; |
5 | 5 | ||
6 | use std::convert::TryFrom; | 6 | use std::{convert::TryFrom, process}; |
7 | 7 | ||
8 | use lsp_server::Connection; | 8 | use lsp_server::Connection; |
9 | use ra_project_model::ProjectManifest; | 9 | use ra_project_model::ProjectManifest; |
@@ -14,18 +14,20 @@ use rust_analyzer::{ | |||
14 | }; | 14 | }; |
15 | use vfs::AbsPathBuf; | 15 | use vfs::AbsPathBuf; |
16 | 16 | ||
17 | use crate::args::HelpPrinted; | ||
18 | |||
19 | #[cfg(all(feature = "mimalloc"))] | 17 | #[cfg(all(feature = "mimalloc"))] |
20 | #[global_allocator] | 18 | #[global_allocator] |
21 | static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; | 19 | static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; |
22 | 20 | ||
23 | fn main() -> Result<()> { | 21 | fn main() { |
22 | if let Err(err) = try_main() { | ||
23 | eprintln!("{}", err); | ||
24 | process::exit(101); | ||
25 | } | ||
26 | } | ||
27 | |||
28 | fn try_main() -> Result<()> { | ||
24 | setup_logging()?; | 29 | setup_logging()?; |
25 | let args = match args::Args::parse()? { | 30 | let args = args::Args::parse()?; |
26 | Ok(it) => it, | ||
27 | Err(HelpPrinted) => return Ok(()), | ||
28 | }; | ||
29 | match args.command { | 31 | match args.command { |
30 | args::Command::RunServer => run_server()?, | 32 | args::Command::RunServer => run_server()?, |
31 | args::Command::ProcMacro => ra_proc_macro_srv::cli::run()?, | 33 | args::Command::ProcMacro => ra_proc_macro_srv::cli::run()?, |
@@ -45,6 +47,7 @@ fn main() -> Result<()> { | |||
45 | cli::search_for_patterns(patterns, debug_snippet)?; | 47 | cli::search_for_patterns(patterns, debug_snippet)?; |
46 | } | 48 | } |
47 | args::Command::Version => println!("rust-analyzer {}", env!("REV")), | 49 | args::Command::Version => println!("rust-analyzer {}", env!("REV")), |
50 | args::Command::Help => {} | ||
48 | } | 51 | } |
49 | Ok(()) | 52 | Ok(()) |
50 | } | 53 | } |