Skip to main content

wtg_cli/
cli.rs

1use clap::Parser;
2
3use crate::{
4    constants,
5    error::{WtgError, WtgResult},
6    parse_input::{ParsedInput, try_parse_input},
7};
8
9#[derive(Parser, Debug)]
10#[command(
11    name = "wtg",
12    version,
13    about = constants::DESCRIPTION,
14    disable_help_flag = true,
15)]
16pub struct Cli {
17    /// The thing to identify: commit hash (c62bbcc), issue/PR (#123), file path (Cargo.toml), tag (v1.2.3), or a GitHub URL
18    #[arg(value_name = "COMMIT|ISSUE|FILE|TAG|URL")]
19    pub input: Option<String>,
20
21    /// GitHub repository URL to operate on (e.g., <https://github.com/owner/repo>)
22    #[arg(short = 'r', long, value_name = "URL")]
23    pub repo: Option<String>,
24
25    /// Allow fetching missing refs from remote into local repository
26    ///
27    /// By default, local repositories don't fetch to avoid unexpected network calls.
28    /// Use this flag to enable fetching when a commit/tag isn't found locally.
29    #[arg(long)]
30    pub fetch: bool,
31
32    /// Skip pre-release versions when finding releases
33    ///
34    /// Filters out tags with pre-release identifiers (e.g., -beta, -rc, -alpha)
35    /// when determining which release contains a commit.
36    #[arg(short = 'S', long)]
37    pub skip_prereleases: bool,
38
39    /// Specific tag/release to check against
40    ///
41    /// If provided, checks whether the input (commit, PR, issue) is contained
42    /// in this specific release/tag rather than finding the earliest release.
43    #[arg(value_name = "RELEASE")]
44    pub release: Option<String>,
45
46    /// Print help information
47    #[arg(short, long, action = clap::ArgAction::Help)]
48    help: Option<bool>,
49}
50
51impl Cli {
52    /// Parse the input and -r flag to determine the repository and query
53    pub(crate) fn parse_input(&self) -> WtgResult<ParsedInput> {
54        let input = self.input.as_ref().ok_or_else(|| WtgError::EmptyInput)?;
55
56        try_parse_input(input, self.repo.as_deref())
57    }
58}