@@ -3,6 +3,8 @@ use std::fs;
33use std:: process;
44use std:: time:: Instant ;
55
6+ use super :: debug:: dump_bytes;
7+
68pub fn run ( args : Vec < String > ) {
79 if args. is_empty ( ) {
810 print_help ( ) ;
@@ -13,14 +15,10 @@ pub fn run(args: Vec<String>) {
1315 let mut output = None ;
1416
1517 let mut debug_mode = false ;
16- let mut _show_ast = false ;
17- let mut _show_token = false ;
1818 let mut show_bytes = false ;
1919 let mut dump_hex = false ;
2020 let mut dump_bin = false ;
21- let mut _dump_json = false ;
22- let mut _no_color = false ;
23- let mut _no_warn_ext = false ;
21+ let mut dump_json = false ;
2422 let mut show_stats = false ;
2523 let mut trace_enable = false ;
2624
@@ -32,20 +30,37 @@ pub fn run(args: Vec<String>) {
3230 return ;
3331 }
3432 "-o" => {
35- if i + 1 < args . len ( ) {
36- output = Some ( args [ i + 1 ] . clone ( ) ) ;
37- i += 1 ;
33+ if output . is_some ( ) {
34+ eprintln ! ( "Error: duplicate -o option" ) ;
35+ process :: exit ( 1 ) ;
3836 }
37+ let path = args
38+ . get ( i + 1 )
39+ . filter ( |s| !s. starts_with ( '-' ) )
40+ . unwrap_or_else ( || {
41+ eprintln ! ( "Error: -o requires a path (use ./ for paths starting with '-')" ) ;
42+ process:: exit ( 1 ) ;
43+ } ) ;
44+ output = Some ( path. clone ( ) ) ;
45+ i += 1 ;
3946 }
4047 "--debug-whale" => debug_mode = true ,
4148 "--bytes" => show_bytes = true ,
4249 "--dump-hex" => dump_hex = true ,
4350 "--dump-bin" => dump_bin = true ,
44- "--dump-json" => _dump_json = true ,
51+ "--dump-json" => dump_json = true ,
4552 "--stats" => show_stats = true ,
4653 "--trace" => trace_enable = true ,
4754 s if input. is_none ( ) && !s. starts_with ( '-' ) => input = Some ( s. to_string ( ) ) ,
48- _ => { }
55+ s => {
56+ let kind = if s. starts_with ( '-' ) {
57+ "unknown option"
58+ } else {
59+ "unexpected argument"
60+ } ;
61+ eprintln ! ( "Error: {kind}: {s}" ) ;
62+ process:: exit ( 1 ) ;
63+ }
4964 }
5065 i += 1 ;
5166 }
@@ -88,13 +103,17 @@ pub fn run(args: Vec<String>) {
88103 } ) ;
89104 let elapsed = start_time. elapsed ( ) ;
90105
91- if debug_mode && ( show_bytes || dump_hex || dump_bin) { /* ... same as before ... */ }
92-
93106 super :: output:: publish ( input. as_ref ( ) , output. as_ref ( ) , & out_bytes) . unwrap_or_else ( |e| {
94107 eprintln ! ( "Failed to write {output}: {e}" ) ;
95108 process:: exit ( 1 ) ;
96109 } ) ;
97110
111+ if debug_mode && ( show_bytes || dump_hex || dump_bin || dump_json) {
112+ dump_bytes (
113+ "object" , & out_bytes, show_bytes, dump_hex, dump_bin, dump_json,
114+ ) ;
115+ }
116+
98117 if debug_mode && show_stats {
99118 println ! (
100119 "== STATS ==\n Input bytes: {}\n Output bytes: {}\n Time: {} ms" ,
@@ -106,5 +125,19 @@ pub fn run(args: Vec<String>) {
106125 println ! ( "Created object file {} ({} bytes)" , output, out_bytes. len( ) ) ;
107126}
108127
109- pub fn print_help ( ) { /* ... same as before ... */
128+ pub fn print_help ( ) {
129+ println ! ( "Usage:" ) ;
130+ println ! ( " whale object <input> -o <output>" ) ;
131+ println ! ( ) ;
132+ println ! ( "Wrap raw bytes in an AMD64 ELF64 object with a .text section." ) ;
133+ println ! ( ) ;
134+ println ! ( "Options:" ) ;
135+ println ! ( " --help show this help" ) ;
136+ println ! ( " --debug-whale enable debug features" ) ;
137+ println ! ( " --bytes print object bytes (debug, first 256 bytes)" ) ;
138+ println ! ( " --dump-hex print object bytes as hex (debug, first 256 bytes)" ) ;
139+ println ! ( " --dump-bin print object bytes as binary (debug, first 256 bytes)" ) ;
140+ println ! ( " --dump-json print object size and byte head as JSON (debug)" ) ;
141+ println ! ( " --stats show stats (debug)" ) ;
142+ println ! ( " --trace trace logs" ) ;
110143}
0 commit comments