rclap/
lib.rs

1// Licensed under the MIT license
2// (see LICENSE or <http://opensource.org/licenses/MIT>) All files in the project carrying such
3//! > 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.
4//!
5//! # How it works
6//!
7//! 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.
8//!
9//! ```toml
10//! port = { type = "u16", default = "8080", doc = "Server port number", env = "PORT" }
11//! ip = {  default = "localhost", doc = "connection URL", env = "URL" }
12//! ```
13//!
14//! 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.
15//!
16//! ```text
17//! use clap::Parser;
18//! use rclap::config;
19//!
20//! #[config]
21//! struct MyConfig;
22//!
23//! ```
24//!
25//! 3- Parse and Use: Your application can then simply call MyConfig::parse() to handle all command-line and environment variable parsing.
26//!
27//! ```text
28//!
29//! fn main() {
30//!     let config = MyConfig::parse();
31//!     println!("Config: {:#?}", config);
32//!     println!("{}", &config.port);
33//! }
34//! ```
35//!
36//! rclap prioritizes a hierarchical approach to configuration, allowing you to set the ip and port via either environment variables or command-line arguments. If neither is specified, the predefined default values will be used.
37//!
38//! For instance, you can use the command-line flags --ip and --port to pass values directly. This would generate a help message like the one below, which clearly shows the available options, their default values, and the corresponding environment variables.
39//!
40//! ```text
41//!
42//! Usage: example [OPTIONS]
43//!
44//! Options:
45//!       --"ip" <ip>      connection URL [env: URL=] [default: localhost]
46//!       --"port" <port>  Server port number [env: PORT=120] [default: 8080]
47//!   -h, --help           Print help
48//! ```
49pub use rclap_derive::config;