use std::{
fs::File,
io::{stdout, Write},
path::PathBuf,
};
use argh::FromArgValue;
use eyre::{Context, Result};
pub enum OutputArg {
Stdout,
File(PathBuf),
}
impl FromArgValue for OutputArg {
fn from_arg_value(value: &str) -> Result<Self, String> {
if value == "-" {
Ok(OutputArg::Stdout)
} else {
Ok(OutputArg::File(value.into()))
}
}
}
impl OutputArg {
pub fn get_output_stream(&self) -> Result<Box<dyn Write>> {
let stream: Box<dyn Write> =
match self {
OutputArg::Stdout => Box::new(stdout()),
OutputArg::File(path) => Box::new(File::create(path).wrap_err_with(|| {
format!("Error opening destination file {}", path.display())
})?),
};
Ok(stream)
}
}