Skip to content

Commit aa894ac

Browse files
author
Geobert Quach
committed
refactor(args): Apply comments
1 parent 4e94c46 commit aa894ac

File tree

4 files changed

+116
-161
lines changed

4 files changed

+116
-161
lines changed

crates/ra_cli/src/help.rs

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
pub fn print_global_help() {
2-
println!(
3-
"ra-cli
1+
pub const GLOBAL_HELP: &str = "ra-cli
42
53
USAGE:
64
ra_cli <SUBCOMMAND>
@@ -13,13 +11,9 @@ SUBCOMMANDS:
1311
analysis-stats
1412
highlight
1513
parse
16-
symbols"
17-
)
18-
}
14+
symbols";
1915

20-
pub fn print_analysis_bench_help() {
21-
println!(
22-
"ra_cli-analysis-bench
16+
pub const ANALYSIS_BENCH_HELP: &str = "ra_cli-analysis-bench
2317
2418
USAGE:
2519
ra_cli analysis-bench [FLAGS] [OPTIONS] [PATH]
@@ -33,13 +27,9 @@ OPTIONS:
3327
--highlight <PATH> Hightlight this file
3428
3529
ARGS:
36-
<PATH> Project to analyse"
37-
)
38-
}
30+
<PATH> Project to analyse";
3931

40-
pub fn print_analysis_stats_help() {
41-
println!(
42-
"ra-cli-analysis-stats
32+
pub const ANALYSIS_STATS_HELP: &str = "ra-cli-analysis-stats
4333
4434
USAGE:
4535
ra_cli analysis-stats [FLAGS] [OPTIONS] [PATH]
@@ -53,44 +43,30 @@ OPTIONS:
5343
-o <ONLY>
5444
5545
ARGS:
56-
<PATH>"
57-
)
58-
}
46+
<PATH>";
5947

60-
pub fn print_highlight_help() {
61-
println!(
62-
"ra-cli-highlight
48+
pub const HIGHLIGHT_HELP: &str = "ra-cli-highlight
6349
6450
USAGE:
6551
ra_cli highlight [FLAGS]
6652
6753
FLAGS:
6854
-h, --help Prints help information
69-
-r, --rainbow"
70-
)
71-
}
55+
-r, --rainbow";
7256

73-
pub fn print_symbols_help() {
74-
println!(
75-
"ra-cli-symbols
57+
pub const SYMBOLS_HELP: &str = "ra-cli-symbols
7658
7759
USAGE:
7860
ra_cli highlight [FLAGS]
7961
8062
FLAGS:
81-
-h, --help Prints help inforamtion"
82-
)
83-
}
63+
-h, --help Prints help inforamtion";
8464

85-
pub fn print_parse_help() {
86-
println!(
87-
"ra-cli-parse
65+
pub const PARSE_HELP: &str = "ra-cli-parse
8866
8967
USAGE:
9068
ra_cli parse [FLAGS]
9169
9270
FLAGS:
9371
-h, --help Prints help inforamtion
94-
--no-dump"
95-
)
96-
}
72+
--no-dump";

crates/ra_cli/src/main.rs

Lines changed: 67 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,105 +15,101 @@ type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
1515
fn main() -> Result<()> {
1616
Logger::with_env().start()?;
1717

18-
let subcommand = std::env::args_os().nth(1);
19-
if subcommand.is_none() {
20-
help::print_global_help();
21-
return Ok(());
22-
}
23-
let subcommand = subcommand.unwrap();
18+
let subcommand = match std::env::args_os().nth(1) {
19+
None => {
20+
eprintln!("{}", help::GLOBAL_HELP);
21+
return Ok(());
22+
}
23+
Some(s) => s,
24+
};
2425
let mut matches = Arguments::from_vec(std::env::args_os().skip(2).collect());
2526

2627
match &*subcommand.to_string_lossy() {
2728
"parse" => {
2829
if matches.contains(["-h", "--help"]) {
29-
help::print_parse_help();
30+
eprintln!("{}", help::PARSE_HELP);
3031
return Ok(());
31-
} else {
32-
let no_dump = matches.contains("--no-dump");
33-
matches.finish().or_else(handle_extra_flags)?;
32+
}
33+
let no_dump = matches.contains("--no-dump");
34+
matches.finish().or_else(handle_extra_flags)?;
3435

35-
let _p = profile("parsing");
36-
let file = file()?;
37-
if !no_dump {
38-
println!("{:#?}", file.syntax());
39-
}
40-
std::mem::forget(file);
36+
let _p = profile("parsing");
37+
let file = file()?;
38+
if !no_dump {
39+
println!("{:#?}", file.syntax());
4140
}
41+
std::mem::forget(file);
4242
}
4343
"symbols" => {
4444
if matches.contains(["-h", "--help"]) {
45-
help::print_symbols_help();
45+
eprintln!("{}", help::SYMBOLS_HELP);
4646
return Ok(());
47-
} else {
48-
matches.finish().or_else(handle_extra_flags)?;
49-
let file = file()?;
50-
for s in file_structure(&file) {
51-
println!("{:?}", s);
52-
}
47+
}
48+
matches.finish().or_else(handle_extra_flags)?;
49+
let file = file()?;
50+
for s in file_structure(&file) {
51+
println!("{:?}", s);
5352
}
5453
}
5554
"highlight" => {
5655
if matches.contains(["-h", "--help"]) {
57-
help::print_highlight_help();
56+
eprintln!("{}", help::HIGHLIGHT_HELP);
5857
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);
6558
}
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);
6664
}
6765
"analysis-stats" => {
6866
if matches.contains(["-h", "--help"]) {
69-
help::print_analysis_stats_help();
67+
eprintln!("{}", help::ANALYSIS_STATS_HELP);
7068
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-
)?;
8369
}
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+
)?;
8481
}
8582
"analysis-bench" => {
8683
if matches.contains(["-h", "--help"]) {
87-
help::print_analysis_bench_help();
84+
eprintln!("{}", help::ANALYSIS_BENCH_HELP);
8885
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)?;
11486
}
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)?;
115111
}
116-
_ => help::print_global_help(),
112+
_ => eprintln!("{}", help::GLOBAL_HELP),
117113
}
118114
Ok(())
119115
}
@@ -122,7 +118,7 @@ fn handle_extra_flags(e: pico_args::Error) -> Result<()> {
122118
if let pico_args::Error::UnusedArgsLeft(flags) = e {
123119
let mut invalid_flags = String::new();
124120
for flag in flags {
125-
write!(&mut invalid_flags, "{}, ", flag).expect("Error on write");
121+
write!(&mut invalid_flags, "{}, ", flag)?;
126122
}
127123
let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2);
128124
Err(format!("Invalid flags: {}", invalid_flags).into())

crates/ra_tools/src/help.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
pub fn print_global_help() {
2-
println!(
3-
"tasks
1+
pub const GLOBAL_HELP: &str = "tasks
42
53
USAGE:
64
ra_tools <SUBCOMMAND>
@@ -15,13 +13,9 @@ SUBCOMMANDS:
1513
gen-syntax
1614
gen-tests
1715
install-ra
18-
lint"
19-
)
20-
}
16+
lint";
2117

22-
pub fn print_install_ra_help() {
23-
println!(
24-
"ra_tools-install-ra
18+
pub const INSTALL_RA_HELP: &str = "ra_tools-install-ra
2519
2620
USAGE:
2721
ra_tools.exe install-ra [FLAGS]
@@ -30,12 +24,10 @@ FLAGS:
3024
--client-code
3125
-h, --help Prints help information
3226
--jemalloc
33-
--server"
34-
)
35-
}
27+
--server";
3628

3729
pub fn print_no_param_subcommand_help(subcommand: &str) {
38-
println!(
30+
eprintln!(
3931
"ra_tools-{}
4032
4133
USAGE:
@@ -47,10 +39,7 @@ FLAGS:
4739
);
4840
}
4941

50-
pub fn print_install_ra_conflict() {
51-
println!(
52-
"error: The argument `--server` cannot be used with `--client-code`
42+
pub const INSTALL_RA_CONFLICT: &str =
43+
"error: The argument `--server` cannot be used with `--client-code`
5344
54-
For more information try --help"
55-
)
56-
}
45+
For more information try --help";

0 commit comments

Comments
 (0)