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]] [[package]]
name = "grip-grab" name = "grip-grab"
version = "0.2.12" version = "0.2.13"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",

View File

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

View File

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

View File

@ -1,29 +1,24 @@
use std::path::PathBuf; use std::path::PathBuf;
use crate::{printer::PrintMode, utils}; use crate::{printer::PrintMode, utils};
use clap::{ArgGroup, Parser}; use clap::Parser;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(name = "grip-grab")] #[command(name = "grip-grab")]
#[command(bin_name = "gg")] #[command(bin_name = "gg")]
#[command(version, about = "A somewhat faster, more lightweight, ripgrep-inspired alternative.", long_about = None, arg_required_else_help=true)] #[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 { pub struct Cli {
/// a regex pattern to search for /// 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>, pub pattern: Option<String>,
/// you can specify multiple patterns using -e "pattern1" -e "pattern2" etc. /// 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>, patterns: Vec<String>,
/// path in which to search recursively /// path in which to search recursively
#[arg(index = 2)] #[arg(index = 2, num_args = 1)]
pub path: PathBuf, pub path: Option<PathBuf>,
/// paths to ignore when recursively walking target directory /// paths to ignore when recursively walking target directory
#[clap(short = 'I', long)] #[clap(short = 'I', long)]
@ -81,6 +76,8 @@ pub struct PostProcessedCli {
pub filter_filetypes: Vec<String>, pub filter_filetypes: Vec<String>,
} }
const DEFAULT_PATH: &str = ".";
pub fn process_cli_args(cli: Cli) -> anyhow::Result<PostProcessedCli> { pub fn process_cli_args(cli: Cli) -> anyhow::Result<PostProcessedCli> {
Ok(PostProcessedCli { Ok(PostProcessedCli {
patterns: if !cli.patterns.is_empty() { patterns: if !cli.patterns.is_empty() {
@ -88,7 +85,7 @@ pub fn process_cli_args(cli: Cli) -> anyhow::Result<PostProcessedCli> {
} else { } else {
vec![cli.pattern.unwrap()] 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), ignored_paths: utils::resolve_paths(cli.ignore_paths),
max_results: cli.max_results, max_results: cli.max_results,
n_threads: cli.n_threads, n_threads: cli.n_threads,