Skip to content

Commit

Permalink
feat(cli): re-order options, help and everything
Browse files Browse the repository at this point in the history
  • Loading branch information
vic1707 committed Jun 11, 2024
1 parent de96534 commit 6704b16
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 36 deletions.
6 changes: 3 additions & 3 deletions rens-cli/src/cli/renaming.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* Modules */
mod options;
pub mod options;
/* Crate imports */
use self::options::Options;
/* Dependencies */
use clap::Subcommand;
use regex::{Regex, RegexBuilder};
use rens_common::Strategy;
/* Re-exports */
pub use self::options::{ConfirmOption, Options, OverrideOption};

#[derive(Debug, Subcommand)]
pub enum Mode {
Expand Down
37 changes: 17 additions & 20 deletions rens-cli/src/cli/renaming/options.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,54 @@
/* Modules */
mod confirmations;
mod git;
mod paths;
mod pattern;
mod recursion;
/* Built-in imports */
use std::{io, path::PathBuf};
/* Dependencies */
use clap::{ArgAction, Args, ValueHint};
use clap::{Args, ValueHint};
use rens_common::RenameTarget;
/* Re-exports */
pub use self::{
confirmations::{ConfirmOption, Confirmations, OverrideOption},
git::Options as GitOpt,
paths::Options as PathsOpt,
pattern::Options as PatternOpt,
recursion::Recursion,
};

#[derive(Debug, Args)]
#[command(next_display_order = 0)]
pub struct Options {
/// Weather to rename the file stem, extension or both.
/// Wether to rename the file stem, extension or both.
///
/// Note: filename = <stem>.<extension>
#[arg(long, short, default_value = "both", value_enum)]
pub target: RenameTarget,

#[command(flatten)]
pub recursion: Recursion,
/// Paths to the elements you want to rename.
#[arg(
required = true,
value_parser = path_exists,
value_hint = ValueHint::AnyPath,
)]
pub paths: Vec<PathBuf>,

#[command(flatten)]
pub confirmations: Confirmations,

#[command(flatten)]
pub git_opt: GitOpt,

#[command(flatten)]
pub paths_opt: PathsOpt,

#[command(flatten)]
pub pattern_opt: PatternOpt,

#[arg(
name = "ignore",
long, short,
default_value_t = false,
action = ArgAction::SetTrue,
)]
/// Parse and follow `.gitignore` (local and global), `.ignore` and `.git/info/exclude` files.
pub auto_ignore: bool,

/// Paths to the elements you want to rename.
#[arg(
required = true,
value_parser = path_exists,
value_hint = ValueHint::AnyPath,
)]
pub paths: Vec<PathBuf>,
#[command(flatten)]
pub recursion: Recursion,
}

fn path_exists(input: &str) -> io::Result<PathBuf> {
Expand Down
1 change: 1 addition & 0 deletions rens-cli/src/cli/renaming/options/confirmations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use clap::{Args, ValueEnum};

#[derive(Debug, Args)]
#[command(next_help_heading = "Confirmation Options")]
pub struct Confirmations {
/// Behavior when a renamed file already exists.
#[arg(
Expand Down
33 changes: 33 additions & 0 deletions rens-cli/src/cli/renaming/options/git.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Dependencies */
use clap::{ArgAction, Args};

#[derive(Debug, Args)]
#[group(id = "git_options")]
#[command(next_help_heading = "Git integration Options", display_order = 0)]
pub struct Options {
#[arg(
name = "ignore",
long, short,
default_value_t = false,
action = ArgAction::SetTrue,
)]
/// Parse and follow `.gitignore` (local and global), `.ignore` and `.git/info/exclude` files.
pub auto_ignore: bool,
}

#[cfg(test)]
mod tests {
use super::*;
use clap::{CommandFactory, Parser};

#[derive(Debug, Parser)]
struct TestParser {
#[command(flatten)]
pub options: Options,
}

#[test]
fn verify_conformity() {
TestParser::command().debug_assert();
}
}
3 changes: 2 additions & 1 deletion rens-cli/src/cli/renaming/options/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
use clap::{ArgAction, Args};

#[derive(Debug, Args)]
#[group(skip)]
#[group(id = "path_options")]
#[command(next_help_heading = "Path Options")]
pub struct Options {
/// Canonicalize all paths instead of using relative ones.
#[arg(
Expand Down
5 changes: 3 additions & 2 deletions rens-cli/src/cli/renaming/options/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use core::num::NonZeroUsize;
use clap::Args;

#[derive(Debug, Args)]
#[group(skip)]
#[group(id = "pattern_options")]
#[command(next_help_heading = "Pattern Options")]
pub struct Options {
/// Weather or not the pattern should be made case sensitive.
/// Wether or not the pattern should be made case sensitive.
///
/// Note: No effect if regex pattern already includes case settings at its beggining.
#[arg(long, default_value_t = false)]
Expand Down
15 changes: 8 additions & 7 deletions rens-cli/src/cli/renaming/options/recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@
use clap::{builder::ArgPredicate, ArgAction, Args};

#[derive(Debug, Args)]
#[command(next_help_heading = "Recursion Options")]
pub struct Recursion {
/// Decides if folder paths includes their children recursively.
///
/// Note: implied if --depth is used.
/// When traversing directories, include hidden files.
#[arg(
long, short,
default_value_t = false,
default_value_if("depth", ArgPredicate::IsPresent, "true"),
action = ArgAction::SetTrue,
)]
pub recursive: bool,
pub allow_hidden: bool,

/// If recursive mode is enabled, decides how deep the renaming goes.
#[arg(long, value_name = "depth")]
// Note: None gets used as `As deep as possible`.
pub depth: Option<usize>,

/// When traversing directories, include hidden files.
/// Decides if folder paths includes their children recursively.
///
/// Note: implied if --depth is used.
#[arg(
long, short,
default_value_t = false,
default_value_if("depth", ArgPredicate::IsPresent, "true"),
action = ArgAction::SetTrue,
)]
pub allow_hidden: bool,
pub recursive: bool,
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions rens-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod utils;
use std::{fs, io};
/* Crate imports */
use cli::{
renaming::{ConfirmOption, Options},
renaming::options::{ConfirmOption, GitOpt, Options},
Cli, Commands,
};
use utils::ask_for_confirm;
Expand Down Expand Up @@ -55,7 +55,7 @@ fn main() -> anyhow::Result<()> {
let (
strategy,
Options {
auto_ignore,
git_opt: GitOpt { auto_ignore },
confirmations,
paths_opt,
recursion,
Expand Down
2 changes: 1 addition & 1 deletion rens-cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Built-in imports */
use std::io::{self, Write};
/* Crate imports */
use crate::cli::renaming::OverrideOption;
use crate::cli::renaming::options::OverrideOption;

#[allow(clippy::expect_used)]
pub fn ask_for_confirm(prompt: &str) -> bool {
Expand Down

0 comments on commit 6704b16

Please sign in to comment.