Matthew Maurer | 32e7669 | 2020-06-02 11:15:15 -0700 | [diff] [blame] | 1 | //! How to use environment variable fallback an how it |
| 2 | //! interacts with `default_value`. |
| 3 | |
| 4 | use structopt::StructOpt; |
| 5 | |
| 6 | /// Example for allowing to specify options via environment variables. |
| 7 | #[derive(StructOpt, Debug)] |
| 8 | #[structopt(name = "env")] |
| 9 | struct 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 | |
| 23 | fn main() { |
| 24 | let opt = Opt::from_args(); |
| 25 | println!("{:#?}", opt); |
| 26 | } |