use std::ffi::OsString;
use std::path::PathBuf;
use clap::Parser;
#[derive(Debug, Parser)]
#[command(
name = "toargv",
version,
about = "Expand configuration values into an argument template"
)]
pub struct Cli {
pub config: PathBuf,
#[arg(long, short = 'c', conflicts_with_all = ["dry_run", "exec"])]
pub check: bool,
#[arg(long, short = 'e', value_name = "PROGRAM")]
pub exec: Option<OsString>,
#[arg(short = 'n', long, requires = "exec")]
pub dry_run: bool,
#[arg(last = true, value_name = "TEMPLATE")]
pub template: Vec<String>,
}
#[derive(Debug)]
pub enum Mode<'a> {
Check,
Print {
program: Option<&'a OsString>,
},
Exec {
program: &'a OsString,
},
}
impl Cli {
pub fn mode(&self) -> Mode<'_> {
if self.check {
return Mode::Check;
}
if let Some(program) = &self.exec
&& !self.dry_run
{
return Mode::Exec { program };
}
Mode::Print {
program: if self.dry_run {
Some(
self.exec
.as_ref()
.expect("clap requires --exec when --dry-run is present"),
)
} else {
None
},
}
}
}