Add path validation utility and integrate with CLI

This commit is contained in:
2026-03-02 20:37:31 +01:00
parent e8cf10eee3
commit 08aa198b00
6 changed files with 20 additions and 10 deletions

View File

@@ -1,9 +1,6 @@
[package] [package]
name = "pmp" name = "pmp"
description = """\ description = "PHP Macro Pre-processor (PMP) written in Rust."
PHP Macro Pre-processor (PMP) \
A php lexer, parser and macro pre-processor written in Rust. \
"""
version = "0.1.0-alpha" version = "0.1.0-alpha"
edition = "2024" edition = "2024"

View File

@@ -1,13 +1,18 @@
use clap::Parser;
use std::{ use std::{
ffi::OsStr, ffi::OsStr,
path::PathBuf path::PathBuf
}; };
use clap::{
Parser,
ValueHint::FilePath,
};
use crate::util::fs;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(name = "pmp", about, version)] #[command(name = "pmp", about, version)]
pub struct Args { pub struct Args {
#[arg(long)] #[arg(long, value_hint = FilePath, value_parser = fs::path_exists)]
php: Option<PathBuf>, php: Option<PathBuf>,
#[arg(long, short = 'd', hide = true)] #[arg(long, short = 'd', hide = true)]
@@ -15,8 +20,8 @@ pub struct Args {
#[arg(long, short = 'v')] #[arg(long, short = 'v')]
verbose: bool, verbose: bool,
#[arg(default_value_os = OsStr::new("."))] #[arg(default_value_os = OsStr::new("."), value_hint = FilePath, value_parser = fs::path_exists)]
files: Vec<PathBuf>, directory: Option<PathBuf>,
#[arg(last = true)] #[arg(last = true)]
passthrough: Vec<String>, passthrough: Vec<String>,
} }

View File

@@ -1 +1,2 @@
pub mod cli; pub mod cli;
pub mod util;

View File

@@ -1,8 +1,8 @@
use pmp::{ cli::Args }; use pmp::{ cli::Args };
fn main() fn main()
{ {
let args = Args::parse(); let args = Args::parse();
println!("{:?}", args); println!("{:#?}", args);
} }

6
src/util/fs.rs Normal file
View File

@@ -0,0 +1,6 @@
use std::path::PathBuf;
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())
}

1
src/util/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod fs;