Rants, Ideas, Stuff

A blog about coding, coffee, and stuff

Announcing EnvOpt 0.0.0 - Parse environment variables by defining a struct

I’m happy to announce version 0.0.0 of envopt.

envopt is a crate to parse environment variables by defining a struct. It is heavyly inspired by the awesome structopt.

Here’s a simple example:

use envopt::EnvOpt;

#[derive(EnvOpt)]
pub enum EnvOpts {
    #[envopt(name = "FOO")]
    Foo,
    #[envopt(name = "BAR", default = "default-bar")]
    Bar,
}

pub fn main() {
    EnvOpts::validate_or_exit();

    println!("FOO: {}", EnvOpts::Foo.value_or_exit());
    println!("BAR: {}", EnvOpts::Bar.value_or_exit());
}

First we define our EnvOpts enum which derives envopt’s EnvOpt-derive. The Variants EnvOpts::Foo and EnvOpts::Bar are mapped to the environment variables FOO and BAR and BAR get’s a default of “default-bar”.

In the main() function first we validate the environment and exit with status code 64 if FOO isn’t set.

Later we access both variables and print their content.

I hope this crate helps and I’m open for any feedback!

Feel free to discus on Reddit!