2021-02-28 14:06:05 +00:00
|
|
|
//! [Clap] struct used to parse command line arguments.
|
|
|
|
|
2021-03-01 09:21:00 +00:00
|
|
|
use crate::extension_set::ExtensionSet;
|
2021-04-02 17:51:49 +00:00
|
|
|
use crate::string_type::String as StringType;
|
2021-03-25 18:46:07 +00:00
|
|
|
use cfg_if::cfg_if;
|
2021-03-11 17:44:31 +00:00
|
|
|
use clap::{AppSettings, Clap};
|
2021-04-02 17:51:49 +00:00
|
|
|
use std::path::PathBuf;
|
2021-03-25 14:28:03 +00:00
|
|
|
|
|
|
|
cfg_if! {
|
|
|
|
if #[cfg(windows)] {
|
|
|
|
const DEFAULT_FORMAT: &str = "powershell";
|
|
|
|
} else {
|
2021-04-06 15:47:40 +00:00
|
|
|
const DEFAULT_FORMAT: &str = "sh";
|
2021-03-25 14:28:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-10 09:20:22 +00:00
|
|
|
|
|
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
|
|
pub enum OutputFormat {
|
2021-03-01 10:20:46 +00:00
|
|
|
/// A Bourne shell compatible script.
|
2021-04-06 15:47:40 +00:00
|
|
|
Sh,
|
2021-03-25 14:28:03 +00:00
|
|
|
/// A PowerShell script.
|
|
|
|
PowerShell,
|
|
|
|
/// Also a PowerShell script, with different casing to allow for `fif -o powershell`.
|
|
|
|
Powershell,
|
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 {
|
2021-04-06 15:47:40 +00:00
|
|
|
/// Only examine files with these extensions (Comma-separated list).
|
|
|
|
/// This argument conflicts with `-E`.
|
2021-04-04 13:52:16 +00:00
|
|
|
#[clap(short, long, use_delimiter = true, require_delimiter = true, group = "extensions")]
|
2021-04-02 17:51:49 +00:00
|
|
|
pub exts: Option<Vec<StringType>>,
|
2021-02-18 11:43:24 +00:00
|
|
|
|
2021-04-06 15:47:40 +00:00
|
|
|
/// Use a preset list of extensions as the search filter.
|
|
|
|
/// `media` includes all extensions from the `audio`, `video`, and `images` sets. This argument conflicts with `-e`.
|
2021-04-04 13:52:16 +00:00
|
|
|
#[clap(short = 'E', long, arg_enum, group = "extensions")]
|
2021-02-18 11:43:24 +00:00
|
|
|
pub ext_set: Option<ExtensionSet>,
|
2021-02-04 11:22:19 +00:00
|
|
|
|
2021-04-06 15:47:40 +00:00
|
|
|
/// Don't skip hidden files and directories.
|
|
|
|
/// Even if this flag is not present, fif will still recurse into a hidden root directory - for example, `fif
|
|
|
|
/// ~/.hidden` will recurse into `~/.hidden` regardless of whether or not -s was passed as an argument.
|
2021-02-05 09:24:08 +00:00
|
|
|
#[clap(short, long)]
|
2021-02-04 11:22:19 +00:00
|
|
|
pub scan_hidden: bool,
|
|
|
|
|
2021-04-06 15:47:40 +00:00
|
|
|
/// Scan files without extensions.
|
|
|
|
/// By default, fif will ignore files without extensions - for example, a jpeg file named `photo` won't be considered
|
|
|
|
/// misnamed. Supplying the -S flag will cause fif to recommend renaming this file to `photo.jpg`.
|
2021-03-02 15:12:29 +00:00
|
|
|
#[clap(short = 'S', long)]
|
|
|
|
pub scan_extensionless: bool,
|
|
|
|
|
2021-04-06 15:47:40 +00:00
|
|
|
/// Output format to use.
|
|
|
|
/// By default, fif will output a PowerShell script on Windows, and a Bourne Shell script on other platforms.
|
2021-03-25 14:28:03 +00:00
|
|
|
#[clap(short, long, default_value = DEFAULT_FORMAT, arg_enum)]
|
2021-02-10 09:20:22 +00:00
|
|
|
pub output_format: OutputFormat,
|
2021-02-06 03:24:13 +00:00
|
|
|
|
2021-04-06 15:47:40 +00:00
|
|
|
/// Follow symlinks.
|
2021-04-04 13:52:16 +00:00
|
|
|
#[clap(short, long)]
|
|
|
|
pub follow_symlinks: bool,
|
|
|
|
|
2021-04-26 12:15:14 +00:00
|
|
|
/// Output verbosity. Defaults to only logging warnings and errors.
|
|
|
|
/// Can be overridden by RUST_LOG.
|
|
|
|
#[clap(short, long, parse(from_occurrences))]
|
|
|
|
pub verbose: u8,
|
|
|
|
|
2021-04-06 15:47:40 +00:00
|
|
|
/// The directory to process.
|
2021-02-06 03:24:13 +00:00
|
|
|
// 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
|
|
|
|
2021-03-02 15:12:29 +00:00
|
|
|
/// Further options relating to scanning.
|
2021-04-04 13:52:16 +00:00
|
|
|
#[derive(PartialEq, Debug)]
|
2021-03-02 15:12:29 +00:00
|
|
|
pub struct ScanOpts {
|
|
|
|
/// Whether hidden files and directories should be scanned.
|
|
|
|
pub hidden: bool,
|
|
|
|
/// Whether files without extensions should be scanned.
|
2021-03-11 17:44:31 +00:00
|
|
|
pub extensionless: bool,
|
2021-04-04 13:52:16 +00:00
|
|
|
/// Should symlinks be followed?
|
|
|
|
pub follow_symlinks: bool,
|
2021-03-02 15:12:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 12:16:54 +00:00
|
|
|
impl Parameters {
|
2021-04-04 13:52:16 +00:00
|
|
|
pub fn extensions(&self) -> Option<Vec<&str>> {
|
2021-02-28 12:16:54 +00:00
|
|
|
if let Some(exts) = &self.exts {
|
|
|
|
// extensions supplied like "-e png,jpg,jpeg"
|
2021-04-04 13:52:16 +00:00
|
|
|
Some(exts.iter().map(|s| s.as_str()).collect())
|
2021-02-28 12:16:54 +00:00
|
|
|
} else if let Some(exts) = &self.ext_set {
|
|
|
|
// extensions supplied like "-E images"
|
2021-04-04 13:52:16 +00:00
|
|
|
Some(exts.extensions())
|
2021-02-28 12:16:54 +00:00
|
|
|
} else {
|
2021-04-04 13:52:16 +00:00
|
|
|
// neither -E nor -e was passed
|
|
|
|
None
|
2021-02-28 12:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-02 15:12:29 +00:00
|
|
|
|
2021-03-11 17:44:31 +00:00
|
|
|
pub const fn get_scan_opts(&self) -> ScanOpts {
|
|
|
|
ScanOpts {
|
|
|
|
hidden: self.scan_hidden,
|
|
|
|
extensionless: self.scan_extensionless,
|
2021-04-04 13:52:16 +00:00
|
|
|
follow_symlinks: self.follow_symlinks,
|
2021-03-11 17:44:31 +00:00
|
|
|
}
|
2021-03-02 15:12:29 +00:00
|
|
|
}
|
2021-04-26 12:15:14 +00:00
|
|
|
|
|
|
|
pub fn default_verbosity(&self) -> &'static str {
|
|
|
|
match self.verbose {
|
|
|
|
0 => "warn",
|
|
|
|
1 => "info",
|
|
|
|
2 => "debug",
|
|
|
|
3 | _ => "trace",
|
|
|
|
}
|
|
|
|
}
|
2021-02-28 14:06:05 +00:00
|
|
|
}
|