62 lines
1.7 KiB
Rust
62 lines
1.7 KiB
Rust
//! [Clap] struct used to parse command line arguments.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use crate::extension_set::ExtensionSet;
|
|
use clap::{Clap};
|
|
use smartstring::{LazyCompact, SmartString};
|
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
pub enum OutputFormat {
|
|
Script,
|
|
Text,
|
|
}
|
|
|
|
// TODO: convert this to macro style?: https://docs.rs/clap/3.0.0-beta.2/clap/index.html#using-macros
|
|
|
|
#[derive(Clap, Debug)]
|
|
#[clap(version = option_env!("CARGO_PKG_VERSION").unwrap_or("???"))]
|
|
pub struct Parameters {
|
|
/// Only examine files with these extensions (Comma-separated list)
|
|
#[clap(
|
|
short,
|
|
long,
|
|
use_delimiter = true,
|
|
require_delimiter = true,
|
|
required_unless_present = "ext-set"
|
|
)]
|
|
pub exts: Option<Vec<SmartString<LazyCompact>>>,
|
|
|
|
/// Use a preset list of extensions as the search filter
|
|
#[clap(short = 'E', long, arg_enum, required_unless_present = "exts")]
|
|
pub ext_set: Option<ExtensionSet>,
|
|
|
|
/// Don't skip hidden files and directories
|
|
#[clap(short, long)]
|
|
pub scan_hidden: bool,
|
|
|
|
/// Output format to use. See "--help formats" for more information.
|
|
#[clap(short, long, default_value = "script", arg_enum)]
|
|
pub output_format: OutputFormat,
|
|
|
|
/// Directory to process
|
|
// TODO: right now this can only take a single directory - should this be improved?
|
|
#[clap(name = "DIR", default_value = ".", parse(from_os_str))]
|
|
pub dirs: PathBuf,
|
|
}
|
|
|
|
impl Parameters {
|
|
pub fn extensions(&self) -> Vec<&str> {
|
|
if let Some(exts) = &self.exts {
|
|
// extensions supplied like "-e png,jpg,jpeg"
|
|
exts.iter().map(|s| s.as_str()).collect()
|
|
} else if let Some(exts) = &self.ext_set {
|
|
// extensions supplied like "-E images"
|
|
exts.extensions()
|
|
} else {
|
|
// neither -E nor -e was passed - this should be impossible
|
|
unreachable!()
|
|
}
|
|
}
|
|
}
|