From 274973d09b2a30d123c64f865a4e25ed6c633c28 Mon Sep 17 00:00:00 2001 From: Bauke Date: Fri, 10 Nov 2023 14:33:18 +0100 Subject: [PATCH] Add the match subcommand. --- source/cli/mod.rs | 24 ++++++++++++++++++++++++ source/cli/run.rs | 13 ++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) 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); + } + } + }, } }