Skip to main content

grip/
args.rs

1// Copyright 2026 Umberto Gotti <umberto.gotti@umbertogotti.dev>
2// Licensed under the MIT License
3// SPDX-License-Identifier: MIT
4
5use std::ffi::OsString;
6use std::path::PathBuf;
7
8use clap::Parser;
9
10#[derive(Debug, Clone, Parser)]
11#[command(name = "cargo-grip4rust")]
12#[command(version = env!("CARGO_PKG_VERSION"))]
13#[command(about = "Measure Rust testability")]
14pub struct Args {
15    #[arg(default_value = ".")]
16    pub path: PathBuf,
17
18    #[arg(long)]
19    pub json: bool,
20
21    #[arg(long, alias = "min-score")]
22    pub threshold: Option<u32>,
23
24    #[arg(long)]
25    pub verbose: bool,
26}
27
28impl Args {
29    pub fn parse_from_args<I, T>(args: I) -> Self
30    where
31        I: IntoIterator<Item = T>,
32        T: Into<OsString> + Clone,
33    {
34        Self::parse_from(args)
35    }
36
37    pub fn parse_cargo() -> Self {
38        let raw: Vec<OsString> = std::env::args_os().collect();
39        if raw.len() > 1 && raw[1].to_string_lossy() == "grip4rust" {
40            let mut filtered = vec![raw[0].clone()];
41            filtered.extend(raw.into_iter().skip(2));
42            Self::parse_from(filtered)
43        } else {
44            Self::parse()
45        }
46    }
47}