Skip to main content

Flag

Struct Flag 

Source
pub struct Flag<'a> {
Show 20 fields pub key: u64, pub binding_key: u64, pub binding_type: Option<BindingType>, pub name: &'a str, pub longs: &'a [&'a str], pub shorts: &'a [u8], pub negate: Option<&'a str>, pub takes_value: bool, pub variadic: bool, pub var_max: Option<u32>, pub delimiter: Option<u8>, pub allow_hyphen_values: bool, pub allow_negative_numbers: bool, pub value_terminator: Option<&'a [u8]>, pub require_equals: bool, pub value_optional: bool, pub bool_value: bool, pub default_missing: Option<&'a [u8]>, pub global: bool, pub action: ArgAction,
}
Expand description

A flag, addressed by any of its long or short forms.

Fields§

§key: u64

Caller-assigned identifier, echoed back in Event::Flag. This is how generated code knows which field to assign without any string comparison. See Command::key on why it is this wide.

§binding_key: u64

Compatibility key for mirroring a redeclared child global into an ancestor field.

Zero means no typed binding contract is declared. Derive-generated tables hash the binding shape and portable metadata so only equivalent bindings receive the same event.

§binding_type: Option<BindingType>

Resolved Rust value type for a derive-generated binding.

This is separate from Self::binding_key because token spellings are not type identities: an imported alias and a fully qualified path can name the same type.

§name: &'a str

Unused by binding, kept so a table entry can carry its own name for diagnostics.

§longs: &'a [&'a str]

Long forms, written without the leading --.

§shorts: &'a [u8]

Short forms, as single bytes.

Should be ASCII. A cluster like -xyz is walked one byte at a time, so a non-ASCII short can never be matched, and the remainder after a value-taking one — which becomes its value — would begin in the middle of a character. #[derive(Cli)] rejects a non-ASCII short; a table written by hand should keep to it. Nothing is unsound if it does not: the value would simply be cut in a place that makes no sense, and on Windows would then fail to convert.

§negate: Option<&'a str>

A long form that sets the flag to false, written without the --.

§takes_value: bool

Whether the flag takes a value.

§variadic: bool

Whether one occurrence of this flag keeps taking values, until a flag-like token or the end of the command line.

This is the spec’s variadic flag argument (--include <pattern>...). It is not the spec’s flag-level var=#true, which means the flag may be repeated and takes one value each time — repetition needs nothing from the parser, since it already reports every occurrence separately. Conflating the two makes a merely repeatable flag greedy enough to eat a positional.

§var_max: Option<u32>

How many values one variadic occurrence may take, after which the next word belongs to whatever comes next.

Only for variadic. A merely repeatable flag — the spec’s var=#true — is bounded on how many times it was given, which no single token can decide, so that bound stays with the metadata and is checked after the parse.

§delimiter: Option<u8>

The byte that makes one word several values, if the flag declares one.

Here rather than with the metadata for the same reason var_max is: it decides where a word lands. A bound counts values, and a delimiter is what makes a word stop being one of them — --include a,b,c is three, so a var_max of two is already past its bound on the single word it was entitled to take. Binding cannot count without it.

§allow_hyphen_values: bool

Whether a detached value may itself look like a flag.

The default is to refuse: --jobs --force is far more likely a forgotten value than a jobs of "--force". Declared, the next token is taken whatever it looks like — including -- — which is clap’s allow_hyphen_values and the spec’s property of the same name. A variadic occurrence still stops collecting at a later flag-like token, so a second occurrence of the flag is not eaten as a value.

§allow_negative_numbers: bool

Whether a detached value may be a negative number while other flag-like tokens still stop collection or report as flags.

§value_terminator: Option<&'a [u8]>

A token that ends one variadic occurrence without becoming a value.

§require_equals: bool

Whether the value must be attached with =.

--flag=value is accepted and --flag value is not, which is clap’s require_equals and the spec’s property of the same name. A short’s attached form (-i9229, -i=9229) still binds: only the following word is refused.

§value_optional: bool

Whether this value-taking flag may be present without a value.

A missing value emits the flag event with value: None; bindings such as Option<Option<T>> can therefore distinguish an absent flag from a bare flag and from a flag with an explicit value.

§bool_value: bool

Whether a boolean long flag accepts an attached true or false value.

This does not make the flag value-taking in the ordinary sense: a detached word is never consumed, and help keeps rendering a switch. Only --flag=true and --flag=false opt into an explicit boolean value.

§default_missing: Option<&'a [u8]>

Value used when the flag is present but no value is given.

clap’s default_missing_value and the spec’s default_missing. --color binds this, --color=never binds never, and an absent flag is not bound. Combined with Self::require_equals, a following word is still refused.

§global: bool

Whether the flag is recognized by every command beneath the one that declares it.

§action: ArgAction

Whether this declared flag binds a field or requests a built-in response.

Implementations§

Source§

impl Flag<'_>

Source

pub const BOOL: Flag<'static>

A value-less flag, for use with struct update syntax.

Source

pub const VALUE: Flag<'static>

A flag that takes a value, for use with struct update syntax.

Trait Implementations§

Source§

impl<'a> Clone for Flag<'a>

Source§

fn clone(&self) -> Flag<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for Flag<'a>

Source§

impl<'a> Debug for Flag<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'a> Eq for Flag<'a>

Source§

impl<'a> PartialEq for Flag<'a>

Source§

fn eq(&self, other: &Flag<'a>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'a> StructuralPartialEq for Flag<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Flag<'a>

§

impl<'a> RefUnwindSafe for Flag<'a>

§

impl<'a> Send for Flag<'a>

§

impl<'a> Sync for Flag<'a>

§

impl<'a> Unpin for Flag<'a>

§

impl<'a> UnsafeUnpin for Flag<'a>

§

impl<'a> UnwindSafe for Flag<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.