rclap 0.1.2

rclap is a Rust configuration utility to help to generate the clap structures. The main purpose is to reduce boilerplate code when using clap and expose the configuration requirements as a toml file.
Documentation

rclap

Rust Version Docs.rs

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

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

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:



   #[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 contains more working samples.

settings

name description
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 and toml. the clap library is used to parse command line arguments and must be imported


clap = { version = "4.5", features = ["env", "derive"] }