diff --git a/Cargo.lock b/Cargo.lock index 54bd036..87e4e1c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -297,7 +297,7 @@ dependencies = [ [[package]] name = "grip-grab" -version = "0.2.12" +version = "0.2.13" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 6759c20..f09e7d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "grip-grab" -version = "0.2.12" +version = "0.2.13" edition = "2021" authors = ["Alexandre Pasmantier "] license = "Apache-2.0" diff --git a/README.md b/README.md index 2be0229..ab4ffc2 100644 --- a/README.md +++ b/README.md @@ -31,11 +31,11 @@ source ~/.zshrc ```plaintext A somewhat faster, more lightweight, ripgrep-inspired alternative. -Usage: gg [OPTIONS] > +Usage: gg [OPTIONS] [PATTERN] [PATH] Arguments: [PATTERN] a regex pattern to search for - path in which to search recursively + [PATH] path in which to search recursively Options: -e, --patterns diff --git a/src/cli.rs b/src/cli.rs index 7a69421..8d18f52 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,29 +1,24 @@ use std::path::PathBuf; use crate::{printer::PrintMode, utils}; -use clap::{ArgGroup, Parser}; +use clap::Parser; #[derive(Parser, Debug)] #[command(name = "grip-grab")] #[command(bin_name = "gg")] #[command(version, about = "A somewhat faster, more lightweight, ripgrep-inspired alternative.", long_about = None, arg_required_else_help=true)] -#[command(group( - ArgGroup::new("pattern_group") - .args(&["pattern", "patterns"]) - .required(true) -))] pub struct Cli { /// a regex pattern to search for - #[arg(index = 1, num_args = 0..=1, group = "pattern_group")] + #[arg(index = 1, num_args = 1, required_unless_present = "patterns")] pub pattern: Option, /// you can specify multiple patterns using -e "pattern1" -e "pattern2" etc. - #[arg(short = 'e', long, group = "pattern_group")] + #[arg(short = 'e', long, required_unless_present = "pattern")] patterns: Vec, /// path in which to search recursively - #[arg(index = 2)] - pub path: PathBuf, + #[arg(index = 2, num_args = 1)] + pub path: Option, /// paths to ignore when recursively walking target directory #[clap(short = 'I', long)] @@ -81,6 +76,8 @@ pub struct PostProcessedCli { pub filter_filetypes: Vec, } +const DEFAULT_PATH: &str = "."; + pub fn process_cli_args(cli: Cli) -> anyhow::Result { Ok(PostProcessedCli { patterns: if !cli.patterns.is_empty() { @@ -88,7 +85,7 @@ pub fn process_cli_args(cli: Cli) -> anyhow::Result { } else { vec![cli.pattern.unwrap()] }, - path: utils::resolve_path(cli.path), + path: utils::resolve_path(cli.path.unwrap_or(PathBuf::from(DEFAULT_PATH))), ignored_paths: utils::resolve_paths(cli.ignore_paths), max_results: cli.max_results, n_threads: cli.n_threads,