From 20b9556e7af517e9f86a25cb13344d2bd555c448 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Tue, 15 Jul 2025 17:01:17 +0200 Subject: [PATCH] Refactor filter command line option handling --- rewatch/src/cli.rs | 11 ++++++++--- rewatch/src/main.rs | 14 ++------------ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/rewatch/src/cli.rs b/rewatch/src/cli.rs index 5f593009df..98039653c1 100644 --- a/rewatch/src/cli.rs +++ b/rewatch/src/cli.rs @@ -2,6 +2,11 @@ use std::{ffi::OsString, ops::Deref}; use clap::{Args, Parser, Subcommand}; use clap_verbosity_flag::InfoLevel; +use regex::Regex; + +fn parse_regex(s: &str) -> Result { + Regex::new(s) +} /// ReScript - Fast, Simple, Fully Typed JavaScript from the Future #[derive(Parser, Debug)] @@ -39,8 +44,8 @@ pub struct FilterArg { /// /// Filter allows for a regex to be supplied which will filter the files to be compiled. For /// instance, to filter out test files for compilation while doing feature work. - #[arg(short, long)] - pub filter: Option, + #[arg(short, long, value_parser = parse_regex)] + pub filter: Option, } #[derive(Args, Debug, Clone)] @@ -187,7 +192,7 @@ impl Deref for FolderArg { } impl Deref for FilterArg { - type Target = Option; + type Target = Option; fn deref(&self) -> &Self::Target { &self.filter diff --git a/rewatch/src/main.rs b/rewatch/src/main.rs index a81c3e1bb6..933534f871 100644 --- a/rewatch/src/main.rs +++ b/rewatch/src/main.rs @@ -1,7 +1,6 @@ use anyhow::Result; use clap::Parser; use log::LevelFilter; -use regex::Regex; use std::{io::Write, path::Path}; use rewatch::{build, cli, cmd, lock, watcher}; @@ -31,13 +30,8 @@ fn main() -> Result<()> { cli::Command::Build(build_args) => { let _lock = get_lock(&build_args.folder); - let filter = build_args - .filter - .as_ref() - .map(|filter| Regex::new(&filter).expect("Could not parse regex")); - match build::build( - &filter, + &build_args.filter, Path::new(&build_args.folder as &str), show_progress, build_args.no_timing, @@ -60,12 +54,8 @@ fn main() -> Result<()> { cli::Command::Watch(watch_args) => { let _lock = get_lock(&watch_args.folder); - let filter = watch_args - .filter - .as_ref() - .map(|filter| Regex::new(&filter).expect("Could not parse regex")); watcher::start( - &filter, + &watch_args.filter, show_progress, &watch_args.folder, (*watch_args.after_build).clone(),