use std::fs::read_to_string;
-use std::io::{stdout, IsTerminal, Write};
use std::path::PathBuf;
use anyhow::{anyhow, Result};
le: bool,
/// Input file.
- #[arg(required = true)]
- input: PathBuf,
+ #[arg(required = true, name = "input")]
+ input_file_name: PathBuf,
+
+ /// Output file.
+ #[arg(required = true, name = "output")]
+ output_file_name: PathBuf,
}
fn main() -> Result<()> {
- let Args { be, le, input } = Args::parse();
- if stdout().is_terminal() {
- return Err(anyhow!(
- "not writing binary data to a terminal; redirect to a file"
- ));
- }
+ let Args {
+ be,
+ le,
+ input_file_name,
+ output_file_name,
+ } = Args::parse();
let endian = match (be, le) {
(false, false) | (true, false) => Endian::Big,
(false, true) => Endian::Little,
(true, true) => return Err(anyhow!("can't use both `--be` and `--le`")),
};
- let input = read_to_string(&input)?;
- let output = sack(&input, endian)?;
- stdout().write(&output)?;
+
+ let input_file_str = input_file_name.to_string_lossy();
+ let input = read_to_string(&input_file_name)
+ .map_err(|err| anyhow!("{input_file_str}: read failed ({err})"))?;
+
+ let output = sack(&input, Some(&input_file_str), endian)?;
+
+ let output_file_str = output_file_name.to_string_lossy();
+ std::fs::write(&output_file_name, output)
+ .map_err(|err| anyhow!("{output_file_str}: write failed ({err})"))?;
+
Ok(())
}