diff --git a/source/cli/mod.rs b/source/cli/mod.rs index 7448bac..b24aab6 100644 --- a/source/cli/mod.rs +++ b/source/cli/mod.rs @@ -51,6 +51,13 @@ pub enum MainSubcommand { #[arg(short, long, default_value = "bautils.log")] file: String, }, + + /// The match subcommand. + Match { + /// The match subcommands. + #[command(subcommand)] + command: MatchSubcommand, + }, } /// The arguments subcommands. @@ -110,3 +117,20 @@ pub enum FileSubcommand { file: PathBuf, }, } + +/// The match subcommands. +#[derive(Debug, Subcommand)] +pub enum MatchSubcommand { + /// Match a given string to a regular expression pattern, if the pattern + /// matches the exit code will be 0, otherwise it will be 1. Regular + /// expression parsing failures will intentionally cause a panic. + Regex { + /// The regular expression to match with. + #[arg(short, long)] + pattern: String, + + /// The string to test. + #[arg()] + string: String, + }, +} diff --git a/source/cli/run.rs b/source/cli/run.rs index 30574ef..8bbad2e 100644 --- a/source/cli/run.rs +++ b/source/cli/run.rs @@ -4,7 +4,7 @@ use { crate::{ cli::{ ArgumentsSubcommand, Cli, DirectorySubcommand, FileSubcommand, - MainSubcommand::*, Parser, + MainSubcommand::*, MatchSubcommand, Parser, }, logging::append_line_to_file, }, @@ -87,5 +87,16 @@ pub fn run() { println!("{}", log_line); append_line_to_file(&file, &log_line).unwrap(); } + + Match { + command: match_subcommand, + } => match match_subcommand { + MatchSubcommand::Regex { pattern, string } => { + let regex = regex::RegexBuilder::new(&pattern).build().unwrap(); + if !regex.is_match(&string) { + std::process::exit(1); + } + } + }, } }