pub struct Flag {
pub name: String,
pub short: Option<char>,
pub description: String,
pub required: bool,
pub takes_value: bool,
pub default: Option<String>,
pub choices: Option<Vec<String>>,
pub repeatable: bool,
pub env: Option<String>,
}Expand description
A named flag accepted by a command, e.g. --verbose or -v.
Flags can be boolean (no value) or value-taking (--output json). Boolean
flags are stored as "true" in crate::ParsedCommand::flags when
present. Use Flag::builder to construct instances.
§Examples
let flag = Flag::builder("verbose")
.short('v')
.description("Enable verbose output")
.build()
.unwrap();
assert_eq!(flag.name, "verbose");
assert_eq!(flag.short, Some('v'));
assert!(!flag.takes_value);Fields§
§name: StringThe long flag name, used as --name on the command line and as the key
in crate::ParsedCommand::flags.
short: Option<char>Optional single-character short form, used as -c on the command line.
description: StringHuman-readable description shown in help output.
required: boolWhether the parser returns an error when this flag is absent.
takes_value: boolWhether the flag consumes the following token (or =value) as its value.
When false the flag is boolean: its presence sets the value to
"true".
default: Option<String>Value substituted when the flag is not provided (optional flags only).
choices: Option<Vec<String>>If set, the flag value must be one of these strings (case-sensitive).
Only meaningful when takes_value is true.
repeatable: boolIf true, this flag may appear multiple times in an invocation.
For boolean flags: occurrences are counted; stored as a numeric string
(e.g., -v -v -v → "3").
For value-taking flags: values are collected into a JSON array string
(e.g., --tag a --tag b → ["a","b"]).
env: Option<String>Environment variable to check when the flag is absent from the command line.
Lookup order: CLI argv → env var → default → required error.
Implementations§
Source§impl Flag
impl Flag
Sourcepub fn builder(name: impl Into<String>) -> FlagBuilder
pub fn builder(name: impl Into<String>) -> FlagBuilder
Create a new FlagBuilder with the given long flag name.
§Arguments
name— The long flag name (without the--prefix). Must be non-empty after trimming (enforced byFlagBuilder::build).
§Examples
let flag = Flag::builder("dry-run").build().unwrap();
assert_eq!(flag.name, "dry-run");