diff --git a/src/main.rs b/src/main.rs index 898ddd5..3cccabc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,8 @@ fn main() -> Result<(), Box> { opts.optflag("d", "decode", "decode instead of encoding"); opts.optflag("i", "ignore", "ignore whitespace when decoding"); opts.optopt("o", "output", "output to FILE instead of STDOUT", "FILE"); + opts.optflag("q", "quiet", "never print headers giving file names"); + opts.optflag("v", "verbose", "always print headers giving file names"); opts.optopt("w", "wrap", "wrap at columns", "COLS"); let matches = match opts.parse(&args[1..]) { Ok(m) => m, @@ -75,8 +77,14 @@ fn main() -> Result<(), Box> { if infiles.is_empty() { infiles.push(Input::Stdin) } + if matches.opt_present("q") && matches.opt_present("v") { + eprintln!("Error: conflicting options \"verbose\" and \"quiet\" are both present."); + print_usage(&progname, opts); + return Err("Conflicting options".into()); + } + let verbose = matches.opt_present("v") || (matches.free.len() > 1 && !matches.opt_present("q")); for f in &infiles { - let writer = if let Some(o) = matches.opt_str("o") { + let mut writer = if let Some(o) = matches.opt_str("o") { if matches.free.len() < 2 || op == Operation::Encode { BufWriter::new(File::open(o)?) } else { @@ -89,6 +97,13 @@ fn main() -> Result<(), Box> { Input::Stdin => BufReader::new(File::from(io::stdin().as_fd().try_clone_to_owned()?)), Input::Filename(f) => BufReader::new(File::open(f)?), }; + if verbose { + let fname = match f { + Input::Stdin => "Stdin", + Input::Filename(f) => f, + }; + writer.write_fmt(format_args!("==> {fname} <==\n"))?; + } match op { Operation::Decode => { let decoder = Decoder::new(reader, writer, None, ignore);