diff options
author | Geobert Quach <[email protected]> | 2019-09-10 16:17:11 +0100 |
---|---|---|
committer | Geobert Quach <[email protected]> | 2019-09-10 16:17:11 +0100 |
commit | aa894ace1fc4600dbc94bccb05532154ee2288d8 (patch) | |
tree | f40d8456b8af4a7315da5fb8f07aaeaca7c371a6 /crates/ra_cli/src | |
parent | 4e94c467131a7685b0a0a52b372aa0dd76abba36 (diff) |
refactor(args): Apply comments
Diffstat (limited to 'crates/ra_cli/src')
-rw-r--r-- | crates/ra_cli/src/help.rs | 48 | ||||
-rw-r--r-- | crates/ra_cli/src/main.rs | 138 |
2 files changed, 79 insertions, 107 deletions
diff --git a/crates/ra_cli/src/help.rs b/crates/ra_cli/src/help.rs index bf31472ac..5171578f0 100644 --- a/crates/ra_cli/src/help.rs +++ b/crates/ra_cli/src/help.rs | |||
@@ -1,6 +1,4 @@ | |||
1 | pub fn print_global_help() { | 1 | pub const GLOBAL_HELP: &str = "ra-cli |
2 | println!( | ||
3 | "ra-cli | ||
4 | 2 | ||
5 | USAGE: | 3 | USAGE: |
6 | ra_cli <SUBCOMMAND> | 4 | ra_cli <SUBCOMMAND> |
@@ -13,13 +11,9 @@ SUBCOMMANDS: | |||
13 | analysis-stats | 11 | analysis-stats |
14 | highlight | 12 | highlight |
15 | parse | 13 | parse |
16 | symbols" | 14 | symbols"; |
17 | ) | ||
18 | } | ||
19 | 15 | ||
20 | pub fn print_analysis_bench_help() { | 16 | pub const ANALYSIS_BENCH_HELP: &str = "ra_cli-analysis-bench |
21 | println!( | ||
22 | "ra_cli-analysis-bench | ||
23 | 17 | ||
24 | USAGE: | 18 | USAGE: |
25 | ra_cli analysis-bench [FLAGS] [OPTIONS] [PATH] | 19 | ra_cli analysis-bench [FLAGS] [OPTIONS] [PATH] |
@@ -33,13 +27,9 @@ OPTIONS: | |||
33 | --highlight <PATH> Hightlight this file | 27 | --highlight <PATH> Hightlight this file |
34 | 28 | ||
35 | ARGS: | 29 | ARGS: |
36 | <PATH> Project to analyse" | 30 | <PATH> Project to analyse"; |
37 | ) | ||
38 | } | ||
39 | 31 | ||
40 | pub fn print_analysis_stats_help() { | 32 | pub const ANALYSIS_STATS_HELP: &str = "ra-cli-analysis-stats |
41 | println!( | ||
42 | "ra-cli-analysis-stats | ||
43 | 33 | ||
44 | USAGE: | 34 | USAGE: |
45 | ra_cli analysis-stats [FLAGS] [OPTIONS] [PATH] | 35 | ra_cli analysis-stats [FLAGS] [OPTIONS] [PATH] |
@@ -53,44 +43,30 @@ OPTIONS: | |||
53 | -o <ONLY> | 43 | -o <ONLY> |
54 | 44 | ||
55 | ARGS: | 45 | ARGS: |
56 | <PATH>" | 46 | <PATH>"; |
57 | ) | ||
58 | } | ||
59 | 47 | ||
60 | pub fn print_highlight_help() { | 48 | pub const HIGHLIGHT_HELP: &str = "ra-cli-highlight |
61 | println!( | ||
62 | "ra-cli-highlight | ||
63 | 49 | ||
64 | USAGE: | 50 | USAGE: |
65 | ra_cli highlight [FLAGS] | 51 | ra_cli highlight [FLAGS] |
66 | 52 | ||
67 | FLAGS: | 53 | FLAGS: |
68 | -h, --help Prints help information | 54 | -h, --help Prints help information |
69 | -r, --rainbow" | 55 | -r, --rainbow"; |
70 | ) | ||
71 | } | ||
72 | 56 | ||
73 | pub fn print_symbols_help() { | 57 | pub const SYMBOLS_HELP: &str = "ra-cli-symbols |
74 | println!( | ||
75 | "ra-cli-symbols | ||
76 | 58 | ||
77 | USAGE: | 59 | USAGE: |
78 | ra_cli highlight [FLAGS] | 60 | ra_cli highlight [FLAGS] |
79 | 61 | ||
80 | FLAGS: | 62 | FLAGS: |
81 | -h, --help Prints help inforamtion" | 63 | -h, --help Prints help inforamtion"; |
82 | ) | ||
83 | } | ||
84 | 64 | ||
85 | pub fn print_parse_help() { | 65 | pub const PARSE_HELP: &str = "ra-cli-parse |
86 | println!( | ||
87 | "ra-cli-parse | ||
88 | 66 | ||
89 | USAGE: | 67 | USAGE: |
90 | ra_cli parse [FLAGS] | 68 | ra_cli parse [FLAGS] |
91 | 69 | ||
92 | FLAGS: | 70 | FLAGS: |
93 | -h, --help Prints help inforamtion | 71 | -h, --help Prints help inforamtion |
94 | --no-dump" | 72 | --no-dump"; |
95 | ) | ||
96 | } | ||
diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index a9a9fbddd..e6334cf56 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs | |||
@@ -15,105 +15,101 @@ type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>; | |||
15 | fn main() -> Result<()> { | 15 | fn main() -> Result<()> { |
16 | Logger::with_env().start()?; | 16 | Logger::with_env().start()?; |
17 | 17 | ||
18 | let subcommand = std::env::args_os().nth(1); | 18 | let subcommand = match std::env::args_os().nth(1) { |
19 | if subcommand.is_none() { | 19 | None => { |
20 | help::print_global_help(); | 20 | eprintln!("{}", help::GLOBAL_HELP); |
21 | return Ok(()); | 21 | return Ok(()); |
22 | } | 22 | } |
23 | let subcommand = subcommand.unwrap(); | 23 | Some(s) => s, |
24 | }; | ||
24 | let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect()); | 25 | let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect()); |
25 | 26 | ||
26 | match &*subcommand.to_string_lossy() { | 27 | match &*subcommand.to_string_lossy() { |
27 | "parse" => { | 28 | "parse" => { |
28 | if matches.contains(["-h", "--help"]) { | 29 | if matches.contains(["-h", "--help"]) { |
29 | help::print_parse_help(); | 30 | eprintln!("{}", help::PARSE_HELP); |
30 | return Ok(()); | 31 | return Ok(()); |
31 | } else { | 32 | } |
32 | let no_dump = matches.contains("--no-dump"); | 33 | let no_dump = matches.contains("--no-dump"); |
33 | matches.finish().or_else(handle_extra_flags)?; | 34 | matches.finish().or_else(handle_extra_flags)?; |
34 | 35 | ||
35 | let _p = profile("parsing"); | 36 | let _p = profile("parsing"); |
36 | let file = file()?; | 37 | let file = file()?; |
37 | if !no_dump { | 38 | if !no_dump { |
38 | println!("{:#?}", file.syntax()); | 39 | println!("{:#?}", file.syntax()); |
39 | } | ||
40 | std::mem::forget(file); | ||
41 | } | 40 | } |
41 | std::mem::forget(file); | ||
42 | } | 42 | } |
43 | "symbols" => { | 43 | "symbols" => { |
44 | if matches.contains(["-h", "--help"]) { | 44 | if matches.contains(["-h", "--help"]) { |
45 | help::print_symbols_help(); | 45 | eprintln!("{}", help::SYMBOLS_HELP); |
46 | return Ok(()); | 46 | return Ok(()); |
47 | } else { | 47 | } |
48 | matches.finish().or_else(handle_extra_flags)?; | 48 | matches.finish().or_else(handle_extra_flags)?; |
49 | let file = file()?; | 49 | let file = file()?; |
50 | for s in file_structure(&file) { | 50 | for s in file_structure(&file) { |
51 | println!("{:?}", s); | 51 | println!("{:?}", s); |
52 | } | ||
53 | } | 52 | } |
54 | } | 53 | } |
55 | "highlight" => { | 54 | "highlight" => { |
56 | if matches.contains(["-h", "--help"]) { | 55 | if matches.contains(["-h", "--help"]) { |
57 | help::print_highlight_help(); | 56 | eprintln!("{}", help::HIGHLIGHT_HELP); |
58 | return Ok(()); | 57 | return Ok(()); |
59 | } else { | ||
60 | let rainbow_opt = matches.contains(["-r", "--rainbow"]); | ||
61 | matches.finish().or_else(handle_extra_flags)?; | ||
62 | let (analysis, file_id) = Analysis::from_single_file(read_stdin()?); | ||
63 | let html = analysis.highlight_as_html(file_id, rainbow_opt).unwrap(); | ||
64 | println!("{}", html); | ||
65 | } | 58 | } |
59 | let rainbow_opt = matches.contains(["-r", "--rainbow"]); | ||
60 | matches.finish().or_else(handle_extra_flags)?; | ||
61 | let (analysis, file_id) = Analysis::from_single_file(read_stdin()?); | ||
62 | let html = analysis.highlight_as_html(file_id, rainbow_opt).unwrap(); | ||
63 | println!("{}", html); | ||
66 | } | 64 | } |
67 | "analysis-stats" => { | 65 | "analysis-stats" => { |
68 | if matches.contains(["-h", "--help"]) { | 66 | if matches.contains(["-h", "--help"]) { |
69 | help::print_analysis_stats_help(); | 67 | eprintln!("{}", help::ANALYSIS_STATS_HELP); |
70 | return Ok(()); | 68 | return Ok(()); |
71 | } else { | ||
72 | let verbose = matches.contains(["-v", "--verbose"]); | ||
73 | let memory_usage = matches.contains("--memory-usage"); | ||
74 | let path = matches.value_from_str("--path")?.unwrap_or("".to_string()); | ||
75 | let only = matches.value_from_str(["-o", "--only"])?.map(|v: String| v.to_owned()); | ||
76 | matches.finish().or_else(handle_extra_flags)?; | ||
77 | analysis_stats::run( | ||
78 | verbose, | ||
79 | memory_usage, | ||
80 | path.as_ref(), | ||
81 | only.as_ref().map(String::as_ref), | ||
82 | )?; | ||
83 | } | 69 | } |
70 | let verbose = matches.contains(["-v", "--verbose"]); | ||
71 | let memory_usage = matches.contains("--memory-usage"); | ||
72 | let path: String = matches.value_from_str("--path")?.unwrap_or_default(); | ||
73 | let only = matches.value_from_str(["-o", "--only"])?.map(|v: String| v.to_owned()); | ||
74 | matches.finish().or_else(handle_extra_flags)?; | ||
75 | analysis_stats::run( | ||
76 | verbose, | ||
77 | memory_usage, | ||
78 | path.as_ref(), | ||
79 | only.as_ref().map(String::as_ref), | ||
80 | )?; | ||
84 | } | 81 | } |
85 | "analysis-bench" => { | 82 | "analysis-bench" => { |
86 | if matches.contains(["-h", "--help"]) { | 83 | if matches.contains(["-h", "--help"]) { |
87 | help::print_analysis_bench_help(); | 84 | eprintln!("{}", help::ANALYSIS_BENCH_HELP); |
88 | return Ok(()); | 85 | return Ok(()); |
89 | } else { | ||
90 | let verbose = matches.contains(["-v", "--verbose"]); | ||
91 | let path = matches.value_from_str("--path")?.unwrap_or("".to_string()); | ||
92 | let highlight_path = matches.value_from_str("--highlight")?; | ||
93 | let complete_path = matches.value_from_str("--complete")?; | ||
94 | if highlight_path.is_some() && complete_path.is_some() { | ||
95 | panic!("either --highlight or --complete must be set, not both") | ||
96 | } | ||
97 | let op = if let Some(path) = highlight_path { | ||
98 | let path: String = path; | ||
99 | analysis_bench::Op::Highlight { path: path.into() } | ||
100 | } else if let Some(path_line_col) = complete_path { | ||
101 | let path_line_col: String = path_line_col; | ||
102 | let (path_line, column) = rsplit_at_char(path_line_col.as_str(), ':')?; | ||
103 | let (path, line) = rsplit_at_char(path_line, ':')?; | ||
104 | analysis_bench::Op::Complete { | ||
105 | path: path.into(), | ||
106 | line: line.parse()?, | ||
107 | column: column.parse()?, | ||
108 | } | ||
109 | } else { | ||
110 | panic!("either --highlight or --complete must be set") | ||
111 | }; | ||
112 | matches.finish().or_else(handle_extra_flags)?; | ||
113 | analysis_bench::run(verbose, path.as_ref(), op)?; | ||
114 | } | 86 | } |
87 | let verbose = matches.contains(["-v", "--verbose"]); | ||
88 | let path: String = matches.value_from_str("--path")?.unwrap_or_default(); | ||
89 | let highlight_path = matches.value_from_str("--highlight")?; | ||
90 | let complete_path = matches.value_from_str("--complete")?; | ||
91 | if highlight_path.is_some() && complete_path.is_some() { | ||
92 | panic!("either --highlight or --complete must be set, not both") | ||
93 | } | ||
94 | let op = if let Some(path) = highlight_path { | ||
95 | let path: String = path; | ||
96 | analysis_bench::Op::Highlight { path: path.into() } | ||
97 | } else if let Some(path_line_col) = complete_path { | ||
98 | let path_line_col: String = path_line_col; | ||
99 | let (path_line, column) = rsplit_at_char(path_line_col.as_str(), ':')?; | ||
100 | let (path, line) = rsplit_at_char(path_line, ':')?; | ||
101 | analysis_bench::Op::Complete { | ||
102 | path: path.into(), | ||
103 | line: line.parse()?, | ||
104 | column: column.parse()?, | ||
105 | } | ||
106 | } else { | ||
107 | panic!("either --highlight or --complete must be set") | ||
108 | }; | ||
109 | matches.finish().or_else(handle_extra_flags)?; | ||
110 | analysis_bench::run(verbose, path.as_ref(), op)?; | ||
115 | } | 111 | } |
116 | _ => help::print_global_help(), | 112 | _ => eprintln!("{}", help::GLOBAL_HELP), |
117 | } | 113 | } |
118 | Ok(()) | 114 | Ok(()) |
119 | } | 115 | } |
@@ -122,7 +118,7 @@ fn handle_extra_flags(e: pico_args::Error) -> Result<()> { | |||
122 | if let pico_args::Error::UnusedArgsLeft(flags) = e { | 118 | if let pico_args::Error::UnusedArgsLeft(flags) = e { |
123 | let mut invalid_flags = String::new(); | 119 | let mut invalid_flags = String::new(); |
124 | for flag in flags { | 120 | for flag in flags { |
125 | write!(&mut invalid_flags, "{}, ", flag).expect("Error on write"); | 121 | write!(&mut invalid_flags, "{}, ", flag)?; |
126 | } | 122 | } |
127 | let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2); | 123 | let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2); |
128 | Err(format!("Invalid flags: {}", invalid_flags).into()) | 124 | Err(format!("Invalid flags: {}", invalid_flags).into()) |