Skip to content

Commit 20b9556

Browse files
committed
Refactor filter command line option handling
1 parent 736159e commit 20b9556

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

rewatch/src/cli.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ use std::{ffi::OsString, ops::Deref};
22

33
use clap::{Args, Parser, Subcommand};
44
use clap_verbosity_flag::InfoLevel;
5+
use regex::Regex;
6+
7+
fn parse_regex(s: &str) -> Result<Regex, regex::Error> {
8+
Regex::new(s)
9+
}
510

611
/// ReScript - Fast, Simple, Fully Typed JavaScript from the Future
712
#[derive(Parser, Debug)]
@@ -39,8 +44,8 @@ pub struct FilterArg {
3944
///
4045
/// Filter allows for a regex to be supplied which will filter the files to be compiled. For
4146
/// instance, to filter out test files for compilation while doing feature work.
42-
#[arg(short, long)]
43-
pub filter: Option<String>,
47+
#[arg(short, long, value_parser = parse_regex)]
48+
pub filter: Option<Regex>,
4449
}
4550

4651
#[derive(Args, Debug, Clone)]
@@ -187,7 +192,7 @@ impl Deref for FolderArg {
187192
}
188193

189194
impl Deref for FilterArg {
190-
type Target = Option<String>;
195+
type Target = Option<Regex>;
191196

192197
fn deref(&self) -> &Self::Target {
193198
&self.filter

rewatch/src/main.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use anyhow::Result;
22
use clap::Parser;
33
use log::LevelFilter;
4-
use regex::Regex;
54
use std::{io::Write, path::Path};
65

76
use rewatch::{build, cli, cmd, lock, watcher};
@@ -31,13 +30,8 @@ fn main() -> Result<()> {
3130
cli::Command::Build(build_args) => {
3231
let _lock = get_lock(&build_args.folder);
3332

34-
let filter = build_args
35-
.filter
36-
.as_ref()
37-
.map(|filter| Regex::new(&filter).expect("Could not parse regex"));
38-
3933
match build::build(
40-
&filter,
34+
&build_args.filter,
4135
Path::new(&build_args.folder as &str),
4236
show_progress,
4337
build_args.no_timing,
@@ -60,12 +54,8 @@ fn main() -> Result<()> {
6054
cli::Command::Watch(watch_args) => {
6155
let _lock = get_lock(&watch_args.folder);
6256

63-
let filter = watch_args
64-
.filter
65-
.as_ref()
66-
.map(|filter| Regex::new(&filter).expect("Could not parse regex"));
6757
watcher::start(
68-
&filter,
58+
&watch_args.filter,
6959
show_progress,
7060
&watch_args.folder,
7161
(*watch_args.after_build).clone(),

0 commit comments

Comments
 (0)