2021-02-28 14:06:05 +00:00
|
|
|
//! [Clap] struct used to parse command line arguments.
|
|
|
|
|
2021-02-04 11:22:19 +00:00
|
|
|
use std::path::PathBuf;
|
2021-02-14 16:20:48 +00:00
|
|
|
|
2021-03-01 09:21:00 +00:00
|
|
|
use crate::extension_set::ExtensionSet;
|
2021-03-01 10:20:46 +00:00
|
|
|
use clap::{Clap, AppSettings};
|
2021-02-10 09:20:22 +00:00
|
|
|
use smartstring::{LazyCompact, SmartString};
|
|
|
|
|
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
pub enum OutputFormat {
|
2021-03-01 10:20:46 +00:00
|
|
|
/// A Bourne shell compatible script.
|
2021-02-10 09:20:22 +00:00
|
|
|
Script,
|
2021-03-01 10:20:46 +00:00
|
|
|
/// Plain text.
|
2021-02-14 17:12:27 +00:00
|
|
|
Text,
|
2021-02-10 09:20:22 +00:00
|
|
|
}
|
2021-02-04 11:22:19 +00:00
|
|
|
|
2021-02-28 15:23:18 +00:00
|
|
|
// TODO: convert this to macro style?: https://docs.rs/clap/3.0.0-beta.2/clap/index.html#using-macros
|
2021-02-28 14:06:05 +00:00
|
|
|
|
2021-02-05 09:24:08 +00:00
|
|
|
#[derive(Clap, Debug)]
|
2021-03-01 10:20:46 +00:00
|
|
|
#[clap(
|
|
|
|
version = option_env!("CARGO_PKG_VERSION").unwrap_or("???"),
|
|
|
|
author = option_env!("CARGO_PKG_AUTHORS").unwrap_or("Lynnesbian"),
|
|
|
|
about = option_env!("CARGO_PKG_DESCRIPTION").unwrap_or("File Info Fixer"),
|
|
|
|
before_help = "Copyright © 2021 Lynnesbian under the GPL3 (or later) License.",
|
|
|
|
before_long_help = "Copyright © 2021 Lynnesbian\n\
|
|
|
|
This program is free software: you can redistribute it and/or modify \
|
|
|
|
it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 \
|
|
|
|
of the License, or (at your option) any later version.",
|
|
|
|
setting(AppSettings::ColoredHelp)
|
|
|
|
)]
|
2021-02-04 11:22:19 +00:00
|
|
|
pub struct Parameters {
|
|
|
|
/// Only examine files with these extensions (Comma-separated list)
|
2021-02-21 14:15:09 +00:00
|
|
|
#[clap(
|
|
|
|
short,
|
|
|
|
long,
|
|
|
|
use_delimiter = true,
|
|
|
|
require_delimiter = true,
|
|
|
|
required_unless_present = "ext-set"
|
|
|
|
)]
|
2021-02-18 11:43:24 +00:00
|
|
|
pub exts: Option<Vec<SmartString<LazyCompact>>>,
|
|
|
|
|
2021-03-01 09:18:20 +00:00
|
|
|
/// Use a preset list of extensions as the search filter
|
2021-02-18 11:43:24 +00:00
|
|
|
#[clap(short = 'E', long, arg_enum, required_unless_present = "exts")]
|
|
|
|
pub ext_set: Option<ExtensionSet>,
|
2021-02-04 11:22:19 +00:00
|
|
|
|
|
|
|
/// Don't skip hidden files and directories
|
2021-02-05 09:24:08 +00:00
|
|
|
#[clap(short, long)]
|
2021-02-04 11:22:19 +00:00
|
|
|
pub scan_hidden: bool,
|
|
|
|
|
2021-03-01 10:20:46 +00:00
|
|
|
/// Output format to use
|
2021-02-14 17:12:27 +00:00
|
|
|
#[clap(short, long, default_value = "script", arg_enum)]
|
2021-02-10 09:20:22 +00:00
|
|
|
pub output_format: OutputFormat,
|
2021-02-06 03:24:13 +00:00
|
|
|
|
|
|
|
/// 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))]
|
2021-02-04 11:22:19 +00:00
|
|
|
pub dirs: PathBuf,
|
|
|
|
}
|
2021-02-28 12:16:54 +00:00
|
|
|
|
|
|
|
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!()
|
|
|
|
}
|
|
|
|
}
|
2021-02-28 14:06:05 +00:00
|
|
|
}
|