mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 11:07:42 +00:00
Only exit after running all destructors and flushing stdout
This commit is contained in:
parent
9e4e445315
commit
660f41865b
@ -10,7 +10,6 @@
|
|||||||
#![feature(path_ext)]
|
#![feature(path_ext)]
|
||||||
#![feature(rustc_private)]
|
#![feature(rustc_private)]
|
||||||
#![cfg(not(test))]
|
#![cfg(not(test))]
|
||||||
#![feature(result_expect)]
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
@ -52,8 +51,11 @@ fn lookup_and_read_project_file() -> io::Result<(PathBuf, String)> {
|
|||||||
Ok((path, toml))
|
Ok((path, toml))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn execute() -> i32 {
|
||||||
let (args, write_mode) = determine_params(std::env::args());
|
let (args, write_mode) = match determine_params(std::env::args()) {
|
||||||
|
Some((args, write_mode)) => (args, write_mode),
|
||||||
|
None => return 1,
|
||||||
|
};
|
||||||
|
|
||||||
let config = match lookup_and_read_project_file() {
|
let config = match lookup_and_read_project_file() {
|
||||||
Ok((path, toml)) => {
|
Ok((path, toml)) => {
|
||||||
@ -64,15 +66,26 @@ fn main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
run(args, write_mode, Box::new(config));
|
run(args, write_mode, Box::new(config));
|
||||||
std::process::exit(0);
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage<S: Into<String>>(reason: S) {
|
fn main() {
|
||||||
print!("{}\n\r usage: rustfmt [-h Help] [--write-mode=[true/false]] <file_name>", reason.into());
|
use std::io::Write;
|
||||||
std::process::exit(1);
|
let exit_code = execute();
|
||||||
|
// Make sure standard output is flushed before we exit
|
||||||
|
std::io::stdout().flush().unwrap();
|
||||||
|
// Exit with given exit code.
|
||||||
|
//
|
||||||
|
// NOTE: This immediately terminates the process without doing any cleanup,
|
||||||
|
// so make sure to finish all necessary cleanup before this is called.
|
||||||
|
std::process::exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn determine_params<I>(args: I) -> (Vec<String>, WriteMode)
|
fn print_usage<S: Into<String>>(reason: S) {
|
||||||
|
println!("{}\n\r usage: rustfmt [-h Help] [--write-mode=[true/false]] <file_name>", reason.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn determine_params<I>(args: I) -> Option<(Vec<String>, WriteMode)>
|
||||||
where I: Iterator<Item = String>
|
where I: Iterator<Item = String>
|
||||||
{
|
{
|
||||||
let arg_prefix = "-";
|
let arg_prefix = "-";
|
||||||
@ -80,27 +93,38 @@ fn determine_params<I>(args: I) -> (Vec<String>, WriteMode)
|
|||||||
let help_mode = "-h";
|
let help_mode = "-h";
|
||||||
let long_help_mode = "--help";
|
let long_help_mode = "--help";
|
||||||
let mut write_mode = WriteMode::Replace;
|
let mut write_mode = WriteMode::Replace;
|
||||||
|
let args: Vec<String> = args.collect();
|
||||||
|
|
||||||
// The NewFile option currently isn't supported because it requires another
|
// The NewFile option currently isn't supported because it requires another
|
||||||
// parameter, but it can be added later.
|
// parameter, but it can be added later.
|
||||||
let args:Vec<String> = args.filter(|arg| {
|
if args.iter().any(|arg| {
|
||||||
if arg.starts_with(write_mode_prefix) {
|
if arg.starts_with(write_mode_prefix) {
|
||||||
write_mode = FromStr::from_str(&arg[write_mode_prefix.len()..]).expect("Unrecognized write mode");
|
write_mode = match FromStr::from_str(&arg[write_mode_prefix.len()..]) {
|
||||||
|
Ok(mode) => mode,
|
||||||
|
Err(_) => {
|
||||||
|
print_usage("Unrecognized write mode");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
false
|
false
|
||||||
} else if arg.starts_with(help_mode) || arg.starts_with(long_help_mode) {
|
} else if arg.starts_with(help_mode) || arg.starts_with(long_help_mode) {
|
||||||
usage("");
|
print_usage("");
|
||||||
false
|
|
||||||
} else if arg.starts_with(arg_prefix) {
|
|
||||||
usage("Invalid argument");
|
|
||||||
false
|
|
||||||
} else {
|
|
||||||
true
|
true
|
||||||
|
} else if arg.starts_with(arg_prefix) {
|
||||||
|
print_usage("Invalid argument");
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
}
|
}
|
||||||
}).collect();
|
}) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
if args.len() < 2 {
|
if args.len() < 2 {
|
||||||
usage("Please provide a file to be formatted");
|
print_usage("Please provide a file to be formatted");
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
(args, write_mode)
|
Some((args, write_mode))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user