Enhance CLI arguments with new options, validation, and detailed help messages
This commit is contained in:
@@ -1,6 +1,27 @@
|
||||
use regex::Regex;
|
||||
use std::path::PathBuf;
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
static PATH_REGEX: Lazy<Regex> = Lazy::new(|| {
|
||||
Regex::new(r"^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$").unwrap()
|
||||
});
|
||||
|
||||
pub fn path_exists(s: &str) -> Result<PathBuf, String> {
|
||||
let p = PathBuf::from(s);
|
||||
p.exists().then_some(p).ok_or("path does not exist".to_string())
|
||||
}
|
||||
|
||||
pub fn valid_possible_path(s: &str) -> Result<PathBuf, String>
|
||||
{
|
||||
let lower = s.to_ascii_lowercase();
|
||||
|
||||
if lower == "true" || lower == "false" {
|
||||
return Err("boolean literal is not allowed".into());
|
||||
}
|
||||
|
||||
if PATH_REGEX.is_match(s) {
|
||||
return Err("numeric literal is not allowed".into());
|
||||
}
|
||||
|
||||
Ok(PathBuf::from(s))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user