rs_envflag
This crate provides an easy to define flags controlled by environment variables. It is a rust, and of course much more rustacean, reimplementation of https://github.com/TimeExceed/envflag.
a str flag without default values
Here is an example about defining a str flag.
use *;
/// an example about str flag
const STR_FLAG: ;
When we run it directly, STR_FLAG will be None.
$ cargo build --examples && target/debug/examples/str
not present.
But once STR_FLAG is set, it looks like
$ cargo build --examples && STR_FLAG=abc target/debug/examples/str
abc
a str flag with default values
Also we can define default values to flags.
use *;
/// an example about str flag with default
const STR_FLAG_W_DEFAULT: String;
Then we will compile and run it.
$ cargo build --examples && target/debug/examples/str && STR_FLAG_W_DEFAULT=xyz target/debug/examples/str
xyz
i64/f64/bool flags with/without default values
We can also define i64, f64 and bool flags, either with or without, default values. Please refer to examples/ for details.
customized types and customized parsers
Now we will show how to define flags with customized types.
use *;
const X: ;
;
- Just a parser is required. It must accept 2 arguments. The 2nd argument is the value to parse. The 1st is the name of the env variable. This is not necessary for parsing values. But it is convenient to log something about parsing errors and warnings.
To define default values of env flags of customized types, more things are required.
use *;
const X_W_DEFAULT: V;
;
- Besides parsers, const default values are required.
And they must be refered by references, e.g., in this example
default=&V::DEFAULT. Vmust implement theClonetrait, so the default value will be cloned when necessary.
flag renaming
Names of env variables and those in rust can be different.
We support it by env_name attribute.
use *;
/// env is named as `XYZ` rather `ABC`.
const ABC: ;
Now, this program will response what env variable XYZ is.
$ cargo build --examples && target/debug/examples/env_rename && XYZ=xyz target/debug/examples/env_rename
not present.
xyz
crate renaming
Occasionally, crate rs_envflag have to be imported as a different name.
We also support this case by crate attribute.
Please refer to examples/crate_rename.rs for details.