Function bpaf::env

source · []
pub fn env(variable: &'static str) -> Named
Expand description

Environment variable fallback

If named value isn’t present - try to fallback to this environment variable.

You can specify it multiple times, bpaf would use items past the first one as hidden aliases.

For flag and switch environment variable being present gives the same result as the flag being present, allowing to implement things like NO_COLOR variables:

$ NO_COLOR=1 app --do-something

Combinatoric usage

You must specify either short or long key if you start the chain from env.

fn parse_string() -> impl Parser<String> {
    short('k')
        .long("key")
        .env("API_KEY")
        .help("Use this API key to access the API")
        .argument("KEY")
}

Derive usage

enum annotation takes a string literal or an expression of type &'static str.

#[derive(Debug, Clone, Bpaf)]
struct Options {
    /// Use this API key to access the API
    #[bpaf(short, long, env("API_KEY"))]
    key: String,
}

Example

$ app --help
    --key <KEY>  [env:ACCESS_KEY: N/A]
                 access key to use
$ app
// fails due to missing --key argument
$ app --key SECRET
// "SECRET"
$ KEY=TOP_SECRET app
// "TOP_SECRET"
$ KEY=TOP_SECRET app --key SECRET
// "SECRET" - argument takes a priority

See Named for more details