diff --git a/data/help.txt b/data/help.txt index bf3f71e8..c3b27ae3 100644 --- a/data/help.txt +++ b/data/help.txt @@ -13,6 +13,7 @@ EXAMPLE: FLAGS: -h, --help Prints help information -V, --version Prints version information + -c, --stdout Prints result as standard output OPTIONS: Elements: diff --git a/src/cli.rs b/src/cli.rs index b7167ae7..f83abf39 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -174,9 +174,12 @@ pub fn prepare_app<'a, 'b>() -> App<'a, 'b> { .index(1) .validator(is_svg)) .arg(Arg::with_name("out-file") - .required_if(KEYS[Key::Stdout], "false") + .required_unless(KEYS[Key::Stdout]) .index(2) .validator(is_svg)) + .arg(Arg::with_name(KEYS[Key::Stdout]) + .short("c") + .long(KEYS[Key::Stdout])) // elements .arg(gen_flag!(Key::RemoveComments, "true")) @@ -242,7 +245,6 @@ pub fn prepare_app<'a, 'b>() -> App<'a, 'b> { .arg(gen_flag!(Key::Multipass, "false")) .arg(gen_flag!(Key::CopyOnError, "false")) .arg(gen_flag!(Key::Quiet, "false")) - .arg(gen_flag!(Key::Stdout, "false")) } fn is_svg(val: String) -> Result<(), String> { diff --git a/src/main.rs b/src/main.rs index 0610a71f..1e08d6f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,7 +61,8 @@ fn main() { let cleaning_opt = cli::gen_cleaning_options(&args); let in_file = args.value_of("in-file").unwrap(); - let out_file = args.value_of("out-file").unwrap_or(""); + let out_file = args.value_of("out-file"); + let stdout_enabled = args.is_present("stdout"); if !Path::new(in_file).exists() { writeln!(stderr(), "Error: Input file does not exist.").unwrap(); @@ -73,10 +74,10 @@ fn main() { let on_err = || { // copy original file to destination - if out_file.is_empty() && cli::get_flag(&args, Key::CopyOnError) { + if out_file.is_some() && cli::get_flag(&args, Key::CopyOnError) { // copy a file only when paths are different - if in_file != out_file { - try_msg!(fs::copy(in_file, out_file)); + if in_file != out_file.unwrap() { + try_msg!(fs::copy(in_file, out_file.unwrap())); } } @@ -136,18 +137,18 @@ fn main() { } // save buffer - if cli::get_flag(&args, Key::Stdout) { + if stdout_enabled { try_msg!(cleaner::write_stdout(&buf[..])); } - if !out_file.is_empty() { + if let Some(out_file) = out_file { try_msg!(cleaner::save_file(&buf[..], out_file)); } // unwrap is safe, because 'save_file' will fail on write error, // so file is totally exist - if !cli::get_flag(&args, Key::Stdout) && !out_file.is_empty() && !cli::get_flag(&args, Key::Quiet) { - let out_size = fs::File::open(out_file).unwrap().metadata().unwrap().len() as f64; + if !stdout_enabled && out_file.is_some() && !cli::get_flag(&args, Key::Quiet) { + let out_size = fs::File::open(out_file.unwrap()).unwrap().metadata().unwrap().len() as f64; let ratio = 100.0 - out_size / (raw.len() as f64) * 100.0; println!("Your image is {:.2}% smaller now.", ratio); }