1
Fork 0

Add the match subcommand.

This commit is contained in:
Bauke 2023-11-10 14:33:18 +01:00
parent 72eaa1da8b
commit 274973d09b
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 36 additions and 1 deletions

View File

@ -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,
},
}

View File

@ -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);
}
}
},
}
}