Skip to main content

ValueEnum

Derive Macro ValueEnum 

Source
#[derive(ValueEnum)]
{
    // Attributes available to this derive:
    #[usage]
    #[command]
    #[arg]
    #[value]
    #[group]
}
Expand description

Compile an enum into the words one value may be.

What a CLI calls an enum — --shell bash — and what the spec calls choices. The variant’s name in kebab-case is the word, unless name says otherwise:

#[derive(usage::ValueEnum)]
enum Shell {
    /// Bourne Again shell.
    Bash,
    #[usage(alias = "shell-z")]
    Zsh,
    #[usage(name = "pwsh", visible_alias = "powershell", hide = true)]
    PowerShell,
}

#[usage(ignore_case)] on the enum applies to canonical words and aliases. A variant’s doc comment becomes its per-value help. help = "..." overrides it, hide keeps the value accepted while omitting it from help and completion, alias is hidden, and visible_alias is advertised alongside the canonical word.

The derive binds canonical words and aliases directly to their variants; a separate FromStr implementation is not required. Variant cfg and cfg_attr attributes are copied to their entries in the static word tables.

A field holding one says value_enum, which is what puts the words in the spec — so help, completions and the check that rejects a wrong word all read the same list, and none of them can drift from the type.