From 08aa198b009def18d3f43e19375381a0a9ba5b07 Mon Sep 17 00:00:00 2001 From: Overlord Date: Mon, 2 Mar 2026 20:37:31 +0100 Subject: [PATCH] Add path validation utility and integrate with CLI --- Cargo.toml | 5 +---- src/cli/flags.rs | 13 +++++++++---- src/lib.rs | 1 + src/main.rs | 4 ++-- src/util/fs.rs | 6 ++++++ src/util/mod.rs | 1 + 6 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 src/util/fs.rs create mode 100644 src/util/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 79ee7eb..6c1d6d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,6 @@ [package] name = "pmp" -description = """\ - PHP Macro Pre-processor (PMP) \ - A php lexer, parser and macro pre-processor written in Rust. \ -""" +description = "PHP Macro Pre-processor (PMP) written in Rust." version = "0.1.0-alpha" edition = "2024" diff --git a/src/cli/flags.rs b/src/cli/flags.rs index 97760ee..e763160 100644 --- a/src/cli/flags.rs +++ b/src/cli/flags.rs @@ -1,13 +1,18 @@ -use clap::Parser; use std::{ ffi::OsStr, path::PathBuf }; +use clap::{ + Parser, + ValueHint::FilePath, +}; + +use crate::util::fs; #[derive(Parser, Debug)] #[command(name = "pmp", about, version)] pub struct Args { - #[arg(long)] + #[arg(long, value_hint = FilePath, value_parser = fs::path_exists)] php: Option, #[arg(long, short = 'd', hide = true)] @@ -15,8 +20,8 @@ pub struct Args { #[arg(long, short = 'v')] verbose: bool, - #[arg(default_value_os = OsStr::new("."))] - files: Vec, + #[arg(default_value_os = OsStr::new("."), value_hint = FilePath, value_parser = fs::path_exists)] + directory: Option, #[arg(last = true)] passthrough: Vec, } diff --git a/src/lib.rs b/src/lib.rs index 4f77372..714f64a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ pub mod cli; +pub mod util; diff --git a/src/main.rs b/src/main.rs index 30fa7e3..fcbf818 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ use pmp::{ cli::Args }; -fn main() +fn main() { let args = Args::parse(); - println!("{:?}", args); + println!("{:#?}", args); } diff --git a/src/util/fs.rs b/src/util/fs.rs new file mode 100644 index 0000000..2611755 --- /dev/null +++ b/src/util/fs.rs @@ -0,0 +1,6 @@ +use std::path::PathBuf; + +pub fn path_exists(s: &str) -> Result { + let p = PathBuf::from(s); + p.exists().then_some(p).ok_or("path does not exist".to_string()) +} diff --git a/src/util/mod.rs b/src/util/mod.rs new file mode 100644 index 0000000..d521fbd --- /dev/null +++ b/src/util/mod.rs @@ -0,0 +1 @@ +pub mod fs;