cliconf
Dead-simple configuration for Rust CLI tools.
How it Works
Define a flag that your program accepts:
let mut flags = new;
flags.add;
Add one or more locations of config files:
flags.add_config_file;
flags.add_home_config_file;
Load flags:
// Load from config files, environment, and program args
flags.load?;
// Collect positional arguments (non-flags)
let positionals = flags.positionals;
Get values:
let name = flags.get_string;
println!;
Using Flags
Flags are always processed in the following order:
- Configuration files (processed in the order they are added)
- Environment variables
- Command-line arguments
The following configuration methods all produce the same result:
- Configuration files
- Environment variables
HELLO_NAME="john"
- Command-line arguments
# or, using the shorthand
Flags processed later-on in the cycle take precedence, so command-line arguments will override environment variables, which will override config files:
HELLO_NAME=from_environment
# Outputs: "Hello, from_args!"
Types of Flags
There are 9 types of flag values: Bool String Int64 Int128 Float64
StringArray Int64Array Int128Array Float64Array. Set a flag's
default_value to select one.
The Rust types for each are as follows:
| FlagValue:: | Type |
|---|---|
| Bool | bool |
| String | String |
| Int64 | i64 |
| Int128 | i128 |
| Float64 | f64 |
| StringArray | Vec<String> |
| Int64Array | Vec<i64> |
| Int128Array | Vec<i128> |
| Float64Array | Vec<f64> |
All flags must have default values. This is to ensure that your flags are always the correct type and that your program always has good opinionated defaults.
To get values for each type of flag:
// Single Values
let my_bool = flags.get_bool;
let my_string = flags.get_string;
let my_int64 = flags.get_i64;
let my_int128 = flags.get_i128;
let my_float64 = flags.get_f64;
// Arrays
let my_string_array = flags.get_string_array;
let my_int64_array = flags.get_i64_array;
let my_int128_array = flags.get_i128_array;
let my_float64_array = flags.get_f64_array;