# rclap

[](https://crates.io/crates/rclap)
[](https://docs.rs/rclap)
> rclap is a configuration helper based on clap. It is designed to expose the requirement as config files, environment variables, and command line arguments with the least amount of boilerplate code.
> Macro clap generator based on toml files
# Usage
## simple toml file
```toml
port = { type = "u16", default = "8080", doc = "Server port number", env = "PORT" }
ip = { default = "localhost", doc = "connection URL", env = "URL" }
```
Then the config struct can be used by the following code
```rust,compile_fail
use clap::Parser;
use rclap::config;
#[config]
struct MyConfig;
fn main() {
let config = MyConfig::parse();
println!("Config: {:#?}", config);
println!("{}", &config.port);
}
```
The ip and port can be set by environment variables or command line arguments. The default values will be used if neither is set.
Arguments can be set by `--ip` and `--port` command line arguments and the following help message will be generated:
```
Usage: example [OPTIONS]
Options:
--"ip" <ip> connection URL [env: URL=] [default: localhost]
--"port" <port> Server port number [env: PORT=120] [default: 8080]
-h, --help Print help
```
The equivalent for the above code will be generated by the macro:
```rust,compile_fail
#[derive(Debug, Clone, PartialEq, Default, Parser)]
pub struct MyConfig {
///Server port number
#[arg(
id = "port",
default_value_t = 8080,
env = "PORT",
long = stringify!("port")
)]
pub port: u16,
///connection URL
#[arg(
id = "ip",
default_value = "localhost",
env = "URL",
long = stringify!("ip")
)]
pub ip: String,
}
```
The [example folder](./example) contains more working samples.
## settings
| **type** | if not specified, `String` type will be used |
| **env** | the environment variable name to use |
| **long** | same as in clap if not specified the id value will be used instead |
| **short** | same as in clap |
| **default** | the default value to use if neither env nor command line argument is set |
| **doc** | the documentation string to use |
## dependencies
rclap is macro generator based on [clap](https://crates.io/crates/clap) and [toml](https://crates.io/crates/toml).
the clap library is used to parse command line arguments and must be imported
```toml
clap = { version = "4.5", features = ["env", "derive"] }
```