Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Clap and environment variables

  • env

In order for this feature to work we need to enable both the derive and the env features of Clap so we start with:

cargo add clap -F derive -F env

If we don't enable the env feature we'll get an error: no method named env found for struct Arg in the current scope with method not found in Arg.

use clap::Parser;
//use clap::ArgAction;
//use clap::{builder::ArgPredicate, ArgAction, ValueEnum};

#[derive(Parser, Debug)]
struct Cli {
    #[arg(long, env = "DEMO_HOSTNAME")]
    hostname: String,
}

fn main() {
    let args = Cli::parse();
    println!("{args:?}");
}

In this case we can pass the hostname either as a command line flag or by setting the appropriate environmnet variable.

cargo run -- --hostname localhost
DEMO_HOSTNAME=localhost cargo run

Setting the envronment can be done on the same row (on linux and macOS) as we see in our example, but it can be also set earlier.