I am trying to have a CLI tool that redacts files according to some specified regexes.
In debugging, as an example:
cargo run -- folder ./tests/test_files -t emails ip
Or in production, as an example:
raf folder ./tests/test_files -t emails ip
folder
is a subcommand, the first parameter is the path to the folder
and the -t
or --types
parameter is supposed to have a list of regexes types (e.g. a regex corresponding to email, that corresponding to ip addresses and so on).
Below is a list of structs that attempted to achieve this:
use clap::{Parser, Subcommand, Args};
#[derive(Debug, Parser)]
#[clap(author, version, about, name = "raf")]
pub struct Opts {
#[clap(subcommand)]
pub cmd: FileOrFolder,
}
#[derive(Debug, Subcommand)]
pub enum FileOrFolder {
#[clap(name = "folder")]
Folder(FolderOpts),
#[clap(name = "file")]
File(FileOpts),
}
#[derive(Args, Debug)]
pub struct FolderOpts {
/// `path` of the directory in which all files should be redacted, e.g. ./tests/test_files
#[clap(parse(from_os_str))]
pub path: std::path::PathBuf,
/// The type of redaction to be applied to the files, e.g. -t sgNRIC emails
#[clap(short, long)]
pub types: Vec<String>,
}
#[derive(Args, Debug)]
pub struct FileOpts {
#[clap(parse(from_os_str))]
pub path: std::path::PathBuf,
#[clap(short, long)]
pub types: Vec<String>,
}
Basically, the field types
of structs FolderOpts
and FileOpts
is problematic.
The runtime error is:
... raf> cargo run -- folder ./tests/test_files -t emails ip
Finished dev [unoptimized + debuginfo] target(s) in 0.26s
Running `target\debug\raf.exe folder ./tests/test_files -t emails ip`
error: Found argument 'ip' which wasn't expected, or isn't valid in this context
USAGE:
raf.exe folder [OPTIONS] <PATH>
For more information try --help
error: process didn't exit successfully: `target\debug\raf.exe folder ./tests/test_files -t emails ip` (exit code: 2)
How do I make -t emails, ip
to translate to FolderOpts.types
= vec!["emails", "ip"]
?