blob: 04770894683b22f9b3f579e621ef9cba1e2627c9 [file] [log] [blame]
Matthew Maurer32e76692020-06-02 11:15:15 -07001//! How to use environment variable fallback an how it
2//! interacts with `default_value`.
3
4use structopt::StructOpt;
5
6/// Example for allowing to specify options via environment variables.
7#[derive(StructOpt, Debug)]
8#[structopt(name = "env")]
9struct Opt {
10 // Use `env` to enable specifying the option with an environment
11 // variable. Command line arguments take precedence over env.
12 /// URL for the API server
13 #[structopt(long, env = "API_URL")]
14 api_url: String,
15
16 // The default value is used if neither argument nor environment
17 // variable is specified.
18 /// Number of retries
19 #[structopt(long, env = "RETRIES", default_value = "5")]
20 retries: u32,
21}
22
23fn main() {
24 let opt = Opt::from_args();
25 println!("{:#?}", opt);
26}