Add the match subcommand.
This commit is contained in:
parent
72eaa1da8b
commit
274973d09b
|
@ -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,
|
||||
},
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue