Add verbose and quiet options

This commit is contained in:
Nathan Fisher 2025-01-10 00:13:03 -05:00
parent baa4de3553
commit 00f7c6e44a

View file

@ -38,6 +38,8 @@ fn main() -> Result<(), Box<dyn Error>> {
opts.optflag("d", "decode", "decode instead of encoding"); opts.optflag("d", "decode", "decode instead of encoding");
opts.optflag("i", "ignore", "ignore whitespace when decoding"); opts.optflag("i", "ignore", "ignore whitespace when decoding");
opts.optopt("o", "output", "output to FILE instead of STDOUT", "FILE"); 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"); opts.optopt("w", "wrap", "wrap at columns", "COLS");
let matches = match opts.parse(&args[1..]) { let matches = match opts.parse(&args[1..]) {
Ok(m) => m, Ok(m) => m,
@ -75,8 +77,14 @@ fn main() -> Result<(), Box<dyn Error>> {
if infiles.is_empty() { if infiles.is_empty() {
infiles.push(Input::Stdin) 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 { 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 { if matches.free.len() < 2 || op == Operation::Encode {
BufWriter::new(File::open(o)?) BufWriter::new(File::open(o)?)
} else { } else {
@ -89,6 +97,13 @@ fn main() -> Result<(), Box<dyn Error>> {
Input::Stdin => BufReader::new(File::from(io::stdin().as_fd().try_clone_to_owned()?)), Input::Stdin => BufReader::new(File::from(io::stdin().as_fd().try_clone_to_owned()?)),
Input::Filename(f) => BufReader::new(File::open(f)?), 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 { match op {
Operation::Decode => { Operation::Decode => {
let decoder = Decoder::new(reader, writer, None, ignore); let decoder = Decoder::new(reader, writer, None, ignore);