main: exit() only at the end

std::process::exit() was used mainly to be able to return an error code,
but it's pretty brutal (no destructors calls). This commit works around
the lack of return value for main() and calls exit() only at the very
end.
This commit is contained in:
Matteo Cypriani 2016-12-08 17:52:08 -05:00
parent c3c2726537
commit 3a970c3e33
1 changed files with 14 additions and 15 deletions

View File

@ -34,6 +34,11 @@ use nrgrip::raw_audio;
fn main() { fn main() {
process::exit(main_main());
}
fn main_main() -> i32 {
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
let prog_name = &args.first().expect("Can't retrieve program's name"); let prog_name = &args.first().expect("Can't retrieve program's name");
@ -55,13 +60,15 @@ fn main() {
}; };
if options.opt_present("help") { if options.opt_present("help") {
exit_usage(&prog_name, &opts); print_usage(&prog_name, &opts);
return 0;
} }
// Get input NRG image name // Get input NRG image name
if options.free.len() != 1 { if options.free.len() != 1 {
// We need exactly one input file! // We need exactly one input file!
fail_usage(&prog_name, &opts); print_usage(&prog_name, &opts);
return 1;
} }
let img_path = &options.free[0]; let img_path = &options.free[0];
println!("NRG image path: \"{}\"", img_path); println!("NRG image path: \"{}\"", img_path);
@ -79,7 +86,7 @@ fn main() {
Ok(fd) => fd, Ok(fd) => fd,
Err(err) => { Err(err) => {
println!("Can't open image file \"{}\": {}", img_path, err); println!("Can't open image file \"{}\": {}", img_path, err);
process::exit(1); return 1;
}, },
}; };
@ -88,7 +95,7 @@ fn main() {
Ok(metadata) => metadata, Ok(metadata) => metadata,
Err(err) => { Err(err) => {
println!("Error reading \"{}\": {}", img_path, err); println!("Error reading \"{}\": {}", img_path, err);
process::exit(1); return 1;
}, },
}; };
@ -102,7 +109,7 @@ fn main() {
println!("\nExtracting cue sheet..."); println!("\nExtracting cue sheet...");
if let Err(err) = cue_sheet::write_cue_sheet(&img_path, &metadata) { if let Err(err) = cue_sheet::write_cue_sheet(&img_path, &metadata) {
println!("Error writing cue sheet: {}", err); println!("Error writing cue sheet: {}", err);
process::exit(1); return 1;
} }
println!("OK!"); println!("OK!");
} }
@ -116,19 +123,11 @@ fn main() {
} }
println!("OK!"); println!("OK!");
} }
0
} }
fn exit_usage(prog_name: &str, opts: &Options) {
print_usage(prog_name, opts);
process::exit(0);
}
fn fail_usage(prog_name: &str, opts: &Options) {
print_usage(prog_name, opts);
process::exit(1);
}
fn print_usage(prog_name: &str, opts: &Options) { fn print_usage(prog_name: &str, opts: &Options) {
let brief = format!("NRGrip - rip Nero Burning ROM audio images let brief = format!("NRGrip - rip Nero Burning ROM audio images