Skip to main content

flag_default

Function flag_default 

Source
pub fn flag_default<K: AsRef<OsStr>>(key: K, default: bool) -> bool
Expand description

Get the value of the given environment variable as a flag using a default if not set

The flag will be considered true if the environment variable isn’t set and the the default is set to true or if the variablse is set and the value is any value other than 0 or a case insensitive version of false.

The flag will be considered false if the environment variable isn’t set and the the default is set to false or if the variablse is set and the value is a 0 or a case insensitive version of false.

§Examples

use fungus::prelude::*;

// Unset variables will be default to the given value
assert!(!sys::flag_default("FOOBAR", false));
assert!(sys::flag_default("FOOBAR", true));

// Disabled variables will always be `false` despite default
sys::set_var("FOOBAR", "0");
assert!(!sys::flag_default("FOOBAR", false));
assert!(!sys::flag_default("FOOBAR", true));

// Enabled variables will always be `true` despite default
sys::set_var("FOOBAR", "1");
assert!(sys::flag_default("FOOBAR", false));
assert!(sys::flag_default("FOOBAR", true));