use clap::{Parser, Subcommand};
use std::path::PathBuf;
#[derive(Debug, Parser)]
#[command(name = "guardrail", version, about, long_about = None)]
pub struct Cli {
#[command(subcommand)]
pub command: Command,
}
#[derive(Debug, Subcommand)]
pub enum Command {
Run {
#[arg(
short,
long,
env = "GUARDRAIL_CONFIG",
default_value = "guardrail.toml"
)]
config: PathBuf,
},
Validate {
#[arg(
short,
long,
env = "GUARDRAIL_CONFIG",
default_value = "guardrail.toml"
)]
config: PathBuf,
},
Check {
text: String,
#[arg(
short,
long,
env = "GUARDRAIL_CONFIG",
default_value = "guardrail.toml"
)]
config: PathBuf,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_guardrail_config_env_var_behavior() {
std::env::remove_var("GUARDRAIL_CONFIG");
let cli = Cli::parse_from(["guardrail", "run"]);
match cli.command {
Command::Run { config } => assert_eq!(config, PathBuf::from("guardrail.toml")),
_ => panic!("expected Run command"),
}
std::env::set_var("GUARDRAIL_CONFIG", "/from/env.toml");
let cli = Cli::parse_from(["guardrail", "run"]);
match cli.command {
Command::Run { config } => assert_eq!(config, PathBuf::from("/from/env.toml")),
_ => panic!("expected Run command"),
}
let cli = Cli::parse_from(["guardrail", "run", "--config", "/from/flag.toml"]);
match cli.command {
Command::Run { config } => assert_eq!(config, PathBuf::from("/from/flag.toml")),
_ => panic!("expected Run command"),
}
let cli = Cli::parse_from(["guardrail", "validate"]);
match cli.command {
Command::Validate { config } => assert_eq!(config, PathBuf::from("/from/env.toml")),
_ => panic!("expected Validate command"),
}
let cli = Cli::parse_from(["guardrail", "check", "hello world"]);
match cli.command {
Command::Check { text, config } => {
assert_eq!(text, "hello world");
assert_eq!(config, PathBuf::from("/from/env.toml"));
}
_ => panic!("expected Check command"),
}
std::env::remove_var("GUARDRAIL_CONFIG");
}
}