@@ -15,105 +15,101 @@ type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
15
15
fn main ( ) -> Result < ( ) > {
16
16
Logger :: with_env ( ) . start ( ) ?;
17
17
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
+ } ;
24
25
let mut matches = Arguments :: from_vec ( std:: env:: args_os ( ) . skip ( 2 ) . collect ( ) ) ;
25
26
26
27
match & * subcommand. to_string_lossy ( ) {
27
28
"parse" => {
28
29
if matches. contains ( [ "-h" , "--help" ] ) {
29
- help:: print_parse_help ( ) ;
30
+ eprintln ! ( "{}" , help:: PARSE_HELP ) ;
30
31
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) ?;
34
35
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( ) ) ;
41
40
}
41
+ std:: mem:: forget ( file) ;
42
42
}
43
43
"symbols" => {
44
44
if matches. contains ( [ "-h" , "--help" ] ) {
45
- help:: print_symbols_help ( ) ;
45
+ eprintln ! ( "{}" , help:: SYMBOLS_HELP ) ;
46
46
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) ;
53
52
}
54
53
}
55
54
"highlight" => {
56
55
if matches. contains ( [ "-h" , "--help" ] ) {
57
- help:: print_highlight_help ( ) ;
56
+ eprintln ! ( "{}" , help:: HIGHLIGHT_HELP ) ;
58
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
65
"analysis-stats" => {
68
66
if matches. contains ( [ "-h" , "--help" ] ) {
69
- help:: print_analysis_stats_help ( ) ;
67
+ eprintln ! ( "{}" , help:: ANALYSIS_STATS_HELP ) ;
70
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
82
"analysis-bench" => {
86
83
if matches. contains ( [ "-h" , "--help" ] ) {
87
- help:: print_analysis_bench_help ( ) ;
84
+ eprintln ! ( "{}" , help:: ANALYSIS_BENCH_HELP ) ;
88
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
114
Ok ( ( ) )
119
115
}
@@ -122,7 +118,7 @@ fn handle_extra_flags(e: pico_args::Error) -> Result<()> {
122
118
if let pico_args:: Error :: UnusedArgsLeft ( flags) = e {
123
119
let mut invalid_flags = String :: new ( ) ;
124
120
for flag in flags {
125
- write ! ( & mut invalid_flags, "{}, " , flag) . expect ( "Error on write" ) ;
121
+ write ! ( & mut invalid_flags, "{}, " , flag) ? ;
126
122
}
127
123
let ( invalid_flags, _) = invalid_flags. split_at ( invalid_flags. len ( ) - 2 ) ;
128
124
Err ( format ! ( "Invalid flags: {}" , invalid_flags) . into ( ) )
0 commit comments