diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index b90cdb3d3ca..789f8773345 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -25,7 +25,7 @@ use std::fs::{self, File}; use std::io::{self, Read, Write}; use std::path::{Path, PathBuf}; -use getopts::Options; +use getopts::{Matches, Options}; /// Rustfmt operations. enum Operation { @@ -75,9 +75,14 @@ fn lookup_and_read_project_file(input_file: &Path) -> io::Result<(PathBuf, Strin Ok((path, toml)) } +fn update_config(config: &mut Config, matches: &Matches) { + config.verbose = matches.opt_present("verbose"); +} + fn execute() -> i32 { let mut opts = Options::new(); opts.optflag("h", "help", "show this message"); + opts.optflag("v", "verbose", "show progress"); opts.optopt("", "write-mode", "mode to write in (not usable when piping from stdin)", @@ -87,7 +92,15 @@ fn execute() -> i32 { "config-help", "show details of rustfmt configuration options"); - let operation = determine_operation(&opts, env::args().skip(1)); + let matches = match opts.parse(env::args().skip(1)) { + Ok(m) => m, + Err(e) => { + print_usage(&opts, &e.to_string()); + return 1; + } + }; + + let operation = determine_operation(&matches); match operation { Operation::InvalidInput(reason) => { @@ -116,7 +129,7 @@ fn execute() -> i32 { } Operation::Format(files, write_mode) => { for file in files { - let config = match lookup_and_read_project_file(&file) { + let mut config = match lookup_and_read_project_file(&file) { Ok((path, toml)) => { println!("Using rustfmt config file {} for {}", path.display(), @@ -126,6 +139,7 @@ fn execute() -> i32 { Err(_) => Default::default(), }; + update_config(&mut config, &matches); run(&file, write_mode, &config); } 0 @@ -154,14 +168,7 @@ fn print_usage(opts: &Options, reason: &str) { println!("{}", opts.usage(&reason)); } -fn determine_operation(opts: &Options, args: I) -> Operation - where I: Iterator -{ - let matches = match opts.parse(args) { - Ok(m) => m, - Err(e) => return Operation::InvalidInput(e.to_string()), - }; - +fn determine_operation(matches: &Matches) -> Operation { if matches.opt_present("h") { return Operation::Help; } diff --git a/src/config.rs b/src/config.rs index dcf0816335b..6d1c863bea8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -257,6 +257,7 @@ macro_rules! create_config { } create_config! { + verbose: bool, false, "Use verbose output"; max_width: usize, 100, "Maximum width of each line"; ideal_width: usize, 80, "Ideal width of each line"; tab_spaces: usize, 4, "Number of spaces per tab"; diff --git a/src/lib.rs b/src/lib.rs index e73ed86599d..bf1a0164f68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -300,6 +300,9 @@ fn fmt_ast(krate: &ast::Crate, let mut file_map = FileMap::new(); for (path, module) in modules::list_files(krate, parse_session.codemap()) { let path = path.to_str().unwrap(); + if config.verbose { + println!("Formatting {}", path); + } let mut visitor = FmtVisitor::from_codemap(parse_session, config, Some(mode)); visitor.format_separate_mod(module, path); file_map.insert(path.to_owned(), visitor.buffer);