rclap
rclap is a Rust utility that helps you create command-line interfaces with the clap crate by reducing boilerplate code. It generates clap structures from a simple TOML configuration file, allowing you to define your application's command-line arguments, environment variables, and default values in one place.
How it works
1- Create a TOML File: Define your configuration settings in a simple TOML file, specifying the argument name, type, default value, and associated environment variable.
# Basic configuration
= { = "localhost", = "IP" }
= { = "int", = "8080", = "PORT" }
# Inline enum
= { = "Style", = ["Light", "Dark", "System"], = "Light", = "STYLE" }
# Array type
= { = "[char]", = ['R','G','B'], = "COLORS" }
# Optional values
= { = "bool", = true, = "false", = "DEBUG" }
# Nested configuration
[]
= { = "localhost:5432", = "DB_URL" }
= { = "int", = "10", = "DB_POOL_SIZE" }
2- Apply the Macro: Use the #[config] macro on an empty struct in your Rust code. The macro reads the TOML file and generates the complete clap::Parser implementation for you.
;
3- Parse and Use: Your application can then simply call MyConfig::parse() to handle all command-line and environment variable parsing.
Advanced Features
Enum Support
Define enums inline or reference external ones:
= { = "Style", = ["Light", "Dark", "System"], = "Light", = "STYLE" }
For external enums:
= { = "crate::LogLevel", = "INFO", = "LOG_LEVEL" }
Array Types
Use bracket notation for arrays:
= { = "[char]", = ['R','G','B'], = "COLORS" }
= { = "[int]", = [1,2,3], = "DIGITS" }
Optional Values
Mark fields as optional with optional = true:
= { = "bool", = true, = "false", = "DEBUG" }
= { = "string", = true, = "CACHE_DIR" }
Nested Configuration
Create inner configuration structures using section headers. The [section] creates a struct:
= { = "AppConfig" }
[]
= { = "localhost:5432", = "DB_URL" }
= { = "int", = "10", = "DB_POOL_SIZE" }
The [database] section generates a Database struct.
Example from config_with_inner.toml
iter_map() Method
Convert all configuration to a HashMap for iteration:
let config = parse;
let map = config.s.iter_map;
for in &map
Useful for:
- Dynamic config inspection
- Logging all active settings
- Serializing configuration to different formats
See main.rs for a working example.
Example Output
rclap generates clear help messages showing available options, their environment variable names, and default values:
Usage: example [OPTIONS]
Options:
--"ip" <ip> connection URL [env: IP=] [default: localhost]
--"port" <port> Server port number [env: PORT=] [default: 8080]
-h, --help Print help
Generated Code
The equivalent for the above code will be generated by the macro:
Configuration Settings
| Setting | Description |
|---|---|
| type | Data type: int, float, bool, string, path, [T] for arrays (e.g., [int], [char]) |
| env | Environment variable name for runtime override |
| default | Default value if neither env nor command line argument is set |
| doc | Documentation string displayed in help messages |
| enum | For inline enums: defines enum name and is used with variants |
| variants | Array of variant names for inline enum definitions |
| optional | Marks field as optional; value may be absent from config |
| long | Long flag name (same as clap); if not specified, the id value is used |
| short | Short flag character (same as clap) |
Secret Feature
Enable the secret feature to access secure wrapper types for passwords, tokens, and API keys. When a field is marked with secret = true, its value is hidden in Display and Debug output.
Secret Types
Secret<S> - Generic wrapper for any type implementing CloneableSecret:
use Secret;
let password: = new;
println!; // outputs: *
StringSecret - Specialized wrapper for strings:
use StringSecret;
let token = new;
println!; // outputs: *
Config Syntax
Mark fields as secrets using the secret = true attribute:
= { = "changeme", = true }
= { = "changeme", = true, = true }
= { = "123", = true, = "Int" }
To access the actual value, use the expose_secret() method:
let config = MyConfig.parse;
println!; // outputs: *
println!; // outputs: changeme
Example from config_with_secret.toml
Example Files
- config.toml
- multi_types.toml
- option.toml
- second_config.toml
- config_with_inner.toml
- config_with_secret.toml
Dependencies
The rclap utility is a macro generator that relies on two core dependencies:
clap: A powerful command-line argument parser for Rust. rclap uses it to parse command-line arguments and generate help messages.
toml: A library for parsing and handling TOML files. rclap uses it to read the configuration settings you define in your .toml file.
To use rclap, add the following to your Cargo.toml:
= { = "4.5", = ["env", "derive"] }
Optional features:
[]
= { = "1.0", = ["secrecy"] } # Enable secret wrapper types
[]
= { = "4.5", = ["env", "derive"] }
= { = "1.0", = ["derive"] } # Required for serialization
The example folder contains more working samples.