diff --git a/src/cli.rs b/src/cli.rs index cfbc9f7..16191ef 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -7,9 +7,19 @@ use url::Url; #[command(version, verbatim_doc_comment)] pub struct Opts { /// URL to the repository to be fetched + #[arg( + required_unless_present = "command", + default_value = "x:", // placeholder value, will not be accessed + hide_default_value = true + )] pub url: Url, /// the revision or reference to be fetched + #[arg( + required_unless_present = "command", + default_value_t, + hide_default_value = true + )] pub rev: String, /// specify the fetcher function instead of inferring from the URL @@ -23,6 +33,14 @@ pub struct Opts { /// output in json format #[arg(short, long)] pub json: bool, + + /// List all available fetchers + #[arg(short, long, group = "command")] + pub list_fetchers: bool, + + /// List all fetchers that can be generated without --fetcher + #[arg(short = 'L', long, group = "command")] + pub list_possible_fetchers: bool, } #[derive(Clone, Debug, ValueEnum)] diff --git a/src/main.rs b/src/main.rs index 94c27ec..d637842 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ mod cli; mod fetcher; use anyhow::{bail, Result}; -use clap::Parser; +use clap::{Parser, ValueEnum}; use url::Host; use crate::{ @@ -13,11 +13,24 @@ use crate::{ }, }; -use std::io::stdout; +use std::io::{stdout, Write}; fn main() -> Result<()> { let opts = Opts::parse(); + if opts.list_fetchers || opts.list_possible_fetchers { + let mut out = stdout().lock(); + for fetcher in FetcherFunction::value_variants() { + if matches!(fetcher, FetcherFunction::Fetchhg) && opts.list_possible_fetchers { + continue; + } + if let Some(fetcher) = fetcher.to_possible_value() { + writeln!(out, "{}", fetcher.get_name())?; + } + } + return Ok(()); + } + let fetcher: FetcherDispatch = match (opts.fetcher, opts.url.host()) { (None | Some(FetcherFunction::FetchFromGitHub), Some(Host::Domain("github.com"))) => { FetchFromGitHub(None).into()