//! Argument handling and output plumbing shared by the subcommands. use std::io::Write; /// Where a subcommand writes: `o` for results, `e` for diagnostics. pub struct Out<'a> { pub o: &'a mut dyn Write, pub e: &'a mut dyn Write, } /// The arguments after the subcommand name. pub struct Args { cmd: &'static str, rest: std::collections::VecDeque, } impl Args { pub fn new(cmd: &'static str, rest: Vec) -> Self { Self { cmd, rest: rest.into(), } } #[allow(clippy::should_implement_trait)] pub fn next(&mut self) -> Option { self.rest.pop_front() } /// Put an option and its value back, to be read next (for the /// `--name=value` form). pub fn push_front(&mut self, name: String, value: String) { self.rest.push_front(value); self.rest.push_front(name); } /// The value after an option such as `--max-bytes`. pub fn value(&mut self) -> Option { self.rest.pop_front() } /// A numeric option value. pub fn number(&mut self) -> Option { self.rest.pop_front().and_then(|s| s.parse().ok()) } /// Report a usage problem; exit status 2. pub fn usage_error(&self, out: &mut Out, msg: &str, usage: &str) -> std::io::Result { writeln!(out.e, "h5rs {}: {msg}\n\n{usage}", self.cmd)?; Ok(2) } }