Clap, enum and HashSet

⚓ Rust    📅 2026-01-20    👤 surdeus    👁️ 8      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Clap, enum and HashSet

I have a struct that looks something like this (and is part of a clap subcommand):

#[derive(Debug, Args)]
pub struct MkUserCmd {
  /// Grant account permission PERM.
  #[arg(short, long,
    value_name = "PERM",
    value_parser = clap::value_parser!(Perm),
    action = ArgAction::Append,
  )]
  pub permission: HashSet<Perm>,

  /// User's name.
  pub name: String
}

Perm is a ValueEnum enum that's just a bunch of permissions.

The idea is to allow the user to pass permissions that will be granted to the new user, but I want to put them in a HashSet.

Everything builds successfully, but when I run the parser it says it is missing arguments to the --permission flag (even if it isn't passed at all). If I pass --permission with a valid permission I get a downcast panic.

I have used Vec for these kind of things previously, and that has worked fine, and I will fall back to that -- but I'm curious if this can be made to work. I get the feeling that this should be possible.

2 posts - 2 participants

Read full topic

🏷️ Rust_feed