use std::{fs, path::PathBuf};
use clap::{Args, Parser};
use eyre::{Context, Result};
#[derive(Parser, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[command(version, about, author)]
pub struct Config {
#[command(flatten)]
pub source: Source,
}
#[derive(Args, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[group(required = true)] pub struct Source {
#[arg(short, long)]
code: Option<String>,
file: Option<PathBuf>,
}
impl Source {
pub fn get(self) -> Result<String> {
if let Some(code) = self.code {
return Ok(code);
}
if let Some(file) = &self.file {
return fs::read_to_string(file)
.wrap_err_with(|| format!("tried to read `{}`", file.display()));
}
panic!(concat!(
"configured clap to require ",
"either inline code or source file, ",
"however, both are empty. ",
"this is a clap bug, please report it at our repo first though: ",
env!("CARGO_PKG_REPOSITORY"),
));
}
}
#[must_use]
pub fn cli() -> Config {
Parser::parse()
}