diff --git a/src/main.rs b/src/main.rs index e434bc0..b6a9b7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,7 @@ Options: --stdout Same as `--output -` (Deprecated, here for compatibility). --check Run in 'check' mode. Exits with 0 if input is formatted correctly. Exits with 1 if formatting is required. + --verbose increase verbosity for non errors -v, --version Prints the current version. -h, --help Prints this help. --get-global-config-path Prints the path of the global configuration file. @@ -77,7 +78,7 @@ enum Output { } impl Output { - fn write(&self, input: &Input, formatted: &str) -> Result<(), ()> { + fn write(&self, input: &Input, formatted: &str, verbose: bool) -> Result<(), ()> { match self { Output::None => { // this is not stdout by the check after parsing the arguments that sets the output @@ -95,21 +96,31 @@ impl Output { .unwrap_or_else(|err| panic!("Couldn't open file: {path:?}: {err}")); file.write_all(formatted.as_bytes()) .unwrap_or_else(|err| panic!("Failed to write to file {path:?}: {err}")); - println!("file: {path:?} overwritten."); + if verbose { + println!("file: {path:?} overwritten."); + }; } Output::Check => { if input.content != formatted { - println!("{} needs formatting.", input.name); + if verbose { + println!("{} needs formatting.", input.name); + } return Err(()); } else { - println!("{} is already formatted.", input.name); + if verbose { + println!("{} is already formatted.", input.name); + } } } Output::Stdout => { - println!("=== {:?} ===", input.name); + if verbose { + println!("=== {:?} ===", input.name); + }; stdout() .write_all(formatted.as_bytes()) - .unwrap_or_else(|err| panic!("Couldn't write to stdout: {err}")); + .unwrap_or_else(|err| { + panic!("Couldn't write to stdout: {}", err); + }); } Output::File(output) => { let mut file = File::options() @@ -131,6 +142,7 @@ fn main() -> Result<(), lexopt::Error> { let mut parser = lexopt::Parser::from_env(); let mut inputs = Inputs::Stdin; let mut output = Output::None; + let mut verbose = false; while let Some(arg) = parser.next()? { match arg { Long("version") | Short('v') => { @@ -182,6 +194,9 @@ fn main() -> Result<(), lexopt::Error> { Output::File(value) }; } + Long("verbose") => { + verbose = true; + } Long("check") => { output = Output::Check; } @@ -231,7 +246,7 @@ fn main() -> Result<(), lexopt::Error> { for input in inputs.read() { let formatted = format(&input.content, config); - match output.write(&input, &formatted) { + match output.write(&input, &formatted, verbose) { Ok(()) => {} Err(()) => { exit_status = 1;