Skip to main content

Module actions

Module actions 

Source
Expand description

§Flag Actions

Like Clap, a flag’s behaviour can be chosen with action. Only this subset of Clap’s ArgAction is supported, written as a bare name or as ArgAction::Name:

ActionField typeAbsentEach occurrence
SetTrueboolfalsesets true (the default for bool)
SetFalsebooltruesets false
Countinteger (u8, u32, i64, usize, …)0adds one, saturating at the type’s maximum

Count counts every occurrence, including repeated short flags grouped in one argument (-vvv) and mixed short and long forms (-vv --verbose). A default (default_value_t = ...) replaces the value used when the flag is absent; as in Clap, it is not a starting point for counting. Flags never take a value, so --verbose=2 is rejected as an unknown option.

use getargs_derive::GetArgs;

#[derive(GetArgs)]
struct Cli {
    /// More output, repeat for even more.
    #[arg(short, long, action = Count)]
    verbose: u8,
    /// Disable colored output.
    #[arg(long = "no-color", action = SetFalse)]
    color: bool,
}

let cli = Cli::from_args(["-vvv", "--verbose", "--no-color"].into_iter())?;
assert_eq!(cli.verbose, 4);
assert!(!cli.color);

let cli = Cli::from_args([].into_iter())?;
assert_eq!(cli.verbose, 0);
assert!(cli.color);

In the generated help, a Count flag is followed by ... (-v, --verbose...), as in Clap. See Generated Help.

§Compile-Time Diagnostics

The macro reports a compile error, pointing at the offending attribute or field, for:

  • unsupported action values, and action without a value;
  • action without short/long;
  • SetTrue/SetFalse on a non-bool field, and Count on a non-integer field;
  • value_name on a field with an action.