Add verbose and quiet options
This commit is contained in:
parent
baa4de3553
commit
00f7c6e44a
1 changed files with 16 additions and 1 deletions
17
src/main.rs
17
src/main.rs
|
@ -38,6 +38,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
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<dyn Error>> {
|
|||
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<dyn Error>> {
|
|||
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);
|
||||
|
|
Loading…
Add table
Reference in a new issue