feat: allow omitting target path

This commit is contained in:
alexpasmantier 2024-07-23 19:06:26 +02:00
parent 2ead12a33c
commit 6cc18a84e9
4 changed files with 12 additions and 15 deletions

2
Cargo.lock generated
View File

@ -297,7 +297,7 @@ dependencies = [
[[package]]
name = "grip-grab"
version = "0.2.12"
version = "0.2.13"
dependencies = [
"anyhow",
"clap",

View File

@ -1,6 +1,6 @@
[package]
name = "grip-grab"
version = "0.2.12"
version = "0.2.13"
edition = "2021"
authors = ["Alexandre Pasmantier <alex.pasmant@gmail.com>"]
license = "Apache-2.0"

View File

@ -31,11 +31,11 @@ source ~/.zshrc
```plaintext
A somewhat faster, more lightweight, ripgrep-inspired alternative.
Usage: gg [OPTIONS] <PATTERN|--patterns <PATTERNS>> <PATH>
Usage: gg [OPTIONS] [PATTERN] [PATH]
Arguments:
[PATTERN] a regex pattern to search for
<PATH> path in which to search recursively
[PATH] path in which to search recursively
Options:
-e, --patterns <PATTERNS>

View File

@ -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<String>,
/// 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<String>,
/// path in which to search recursively
#[arg(index = 2)]
pub path: PathBuf,
#[arg(index = 2, num_args = 1)]
pub path: Option<PathBuf>,
/// paths to ignore when recursively walking target directory
#[clap(short = 'I', long)]
@ -81,6 +76,8 @@ pub struct PostProcessedCli {
pub filter_filetypes: Vec<String>,
}
const DEFAULT_PATH: &str = ".";
pub fn process_cli_args(cli: Cli) -> anyhow::Result<PostProcessedCli> {
Ok(PostProcessedCli {
patterns: if !cli.patterns.is_empty() {
@ -88,7 +85,7 @@ pub fn process_cli_args(cli: Cli) -> anyhow::Result<PostProcessedCli> {
} 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,