Skip to main content

Occurrence

Derive Macro Occurrence 

Source
#[derive(Occurrence)]
{
    // Attributes available to this derive:
    #[arg]
    #[winnow_args]
}
Expand description

Derive Occurrence for an enum: one variant per flag or positional, kept in command-line order by an #[arg(sequence)] field of an Args struct.

A variant is a unit (a switch) or holds one value type; a Spanned<T> also records the offset and whether the value was attached. A variant with no short or long is --variant-name. Doc comments are the help text. A spelling used twice, here or in the parent, is a compile error.

ⓘ
use winnow_args::{Args, Occurrence};

#[derive(Occurrence, Debug, PartialEq)]
enum Item {
    /// Link what follows only if needed.
    #[arg(long)]
    AsNeeded,
    /// Search for a library.
    #[arg(short = 'l', prefix)]
    Library(String),
    #[arg(positional, value_name = "FILE")]
    Input(String),
}

#[derive(Args, Debug)]
struct Ld {
    #[arg(short, long)]
    output: Option<String>,
    #[arg(sequence)]
    items: Vec<Item>,
}

let ld = Ld::parse_from(["a.o", "--as-needed", "-lm", "-o", "out", "b.o"])?;
assert_eq!(ld.output.as_deref(), Some("out"));
assert_eq!(
    ld.items,
    [
        Item::Input("a.o".into()),
        Item::AsNeeded,
        Item::Library("m".into()),
        Item::Input("b.o".into()),
    ]
);

Enum options, the default of every value variant:

  • allow_hyphen_values, keep_equals: as for a field.

Variant names:

  • short = 'x', long, long = "name": as for a field; a second long is another name.
  • alias = "…", alias("…", …): more long names.
  • two_dashes, prefix: as for a field.

Variant values, as for a field:

  • value_name = "…", require_equals, keep_equals, allow_hyphen_values, allow_negative_numbers, default_missing = "…".

Variant roles, at most one of each in an enum:

  • positional: each plain word is that variant; its value_name defaults to the variant’s name.
  • unknown: takes a flag no variant names, whole, for a parent with sequence, unknown.
  • bundle: takes a word of several short flags (-sS), whole, ahead of its letters.
  • skip: built by the program, never parsed.