Add path validation utility and integrate with CLI
This commit is contained in:
@@ -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"
|
||||||
|
|||||||
@@ -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>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
pub mod cli;
|
pub mod cli;
|
||||||
|
pub mod util;
|
||||||
|
|||||||
@@ -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
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