use std::path::PathBuf;
use crate::bufwrap::{file_reader, stdin_reader, BufferedInput};
use clap::{value_parser, Arg, Command};
pub struct AocCommand {
description: String,
}
impl AocCommand {
pub fn new(description: &str) -> Self {
Self {
description: description.to_string(),
}
}
pub fn parse_args(&self) -> std::io::Result<BufferedInput> {
let input_arg = Arg::new("input")
.value_parser(value_parser!(PathBuf))
.value_name("FILE")
.help("Input file (defaults to STDIN if not provided)");
let app = Command::new("").about(&self.description).arg(input_arg);
let matches = app.get_matches();
let result = match matches.get_one::<PathBuf>("input") {
Some(path) => BufferedInput::File(file_reader(path)?),
_ => BufferedInput::Stdin(stdin_reader()),
};
Ok(result)
}
}