Add path validation utility and integrate with CLI
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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<PathBuf>,
|
||||
|
||||
#[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<PathBuf>,
|
||||
#[arg(default_value_os = OsStr::new("."), value_hint = FilePath, value_parser = fs::path_exists)]
|
||||
directory: Option<PathBuf>,
|
||||
#[arg(last = true)]
|
||||
passthrough: Vec<String>,
|
||||
}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub mod cli;
|
||||
pub mod util;
|
||||
|
||||
@@ -4,5 +4,5 @@ fn main()
|
||||
{
|
||||
let args = Args::parse();
|
||||
|
||||
println!("{:?}", args);
|
||||
println!("{:#?}", args);
|
||||
}
|
||||
|
||||
6
src/util/fs.rs
Normal file
6
src/util/fs.rs
Normal 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
1
src/util/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod fs;
|
||||
Reference in New Issue
Block a user