Skip to content

Commit

Permalink
fix: validate url flags
Browse files Browse the repository at this point in the history
  • Loading branch information
uncenter committed May 30, 2024
1 parent 7d9fe65 commit 77e6338
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::{arg, Args, Parser, Subcommand, ValueEnum};
use url::Url;

#[derive(Parser)]
#[command(version, arg_required_else_help(true))]
Expand All @@ -23,7 +24,7 @@ pub enum Commands {
#[arg(long)]
name: Option<String>,

#[arg(long)]
#[arg(long, value_parser = valid_url)]
url: Option<String>,
},
Userstyles {
Expand Down Expand Up @@ -57,7 +58,7 @@ pub enum Userstyles {
#[arg(long)]
color: Option<String>,

#[arg(long)]
#[arg(long, value_parser = valid_url)]
url: Option<String>,
},
}
Expand Down Expand Up @@ -99,7 +100,7 @@ pub enum Query {
#[arg(long, num_args = 0..=1, default_missing_value = "true")]
alias: Option<String>,

#[arg(long, num_args = 0..=1, default_missing_value = "true")]
#[arg(long, num_args = 0..=1, default_missing_value = "true", value_parser = valid_url)]
url: Option<String>,

#[command(flatten)]
Expand Down Expand Up @@ -141,7 +142,7 @@ pub enum UserstylesQuery {
#[arg(long)]
color: Option<String>,

#[arg(long)]
#[arg(long, value_parser = valid_url)]
app_link: Option<String>,

#[command(flatten)]
Expand Down Expand Up @@ -170,3 +171,11 @@ pub enum OutputFormat {
Json,
Plain,
}

fn valid_url(u: &str) -> Result<String, String> {
if Url::parse(u).is_ok() {
Ok(String::from(u))
} else {
Err(format!("{} is not a valid URL", u))
}
}

0 comments on commit 77e6338

Please sign in to comment.