pub struct Named { /* private fields */ }
Expand description

A named thing used to create Flag, Switch or Argument.

Implementations

Add a short name to a flag/switch/argument

You can specify it multiple times, items past the first one represent hidden aliases.

let switch: Parser<bool> =
    short('f')
        .short('F')
        .long("flag")
        .help("a flag that does a thing")
        .switch();

Add a long name to a flag/switch/argument

You can specify it multiple times, items past the first one will become hidden aliases.

let switch: Parser<bool> =
    short('f')
        .long("flag")
        .long("Flag")
        .help("a flag that does a thing")
        .switch();

Environment variable fallback

If named value is not present - try to fallback to this environment variable. You can specify it multiple times, items past the first one will become hidden aliases.

let key = short('k')
           .long("key")
           .env("API_KEY")
           .help("Use this API key to access the API")
           .argument("KEY");

Add a help message to a flag/switch/argument

let switch: Parser<bool> =
    short('f')
        .long("flag")
        .help("a flag that does a thing")
        .switch();

Simple boolean flag

Parser produces true if flag is present in a command line or false otherwise

let switch: Parser<bool> =
    short('f')
        .long("flag")
        .help("a flag that does a thing")
        .switch();

Flag with custom present/absent values

Parser produces present if flag is present in a command line or absent otherwise

#[derive(Clone)]
enum Flag {
    Absent,
    Present,
}
let switch: Parser<Flag> =
    short('f')
        .long("flag")
        .help("a flag that does a thing")
        .flag(Flag::Present, Flag::Absent);

Required flag with custom value

Parser produces a value if present and fails otherwise. Designed to be used with combination of other parser(s).

#[derive(Clone)]
enum Decision {
    On,
    Off,
    Undecided
}
let on = long("on").req_flag(Decision::On);
let off = long("off").req_flag(Decision::Off);
// Requires user to specify either `--on` or `--off`
let state: Parser<Decision> = on.or_else(off).fallback(Decision::Undecided);
// counts how many times flag `-v` is given on a command line
let verbosity: Parser<usize> = short('v').req_flag(()).many().map(|v| v.len());

Named argument that can be encoded as String

Argument must be present (but can be made into Option using optional) and it must contain only valid unicode characters. For OS specific encoding see argument_os.

let arg = short('n').long("name").argument("NAME");

Named argument in OS specific encoding

Argument must be present but can be made into Option using optional. If you prefer to panic on non utf8 encoding see argument.

let arg = short('n').long("name").argument_os("NAME");

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.