| Crates.io | clap_args |
| lib.rs | clap_args |
| version | 0.1.8 |
| created_at | 2025-07-08 08:28:21.624542+00 |
| updated_at | 2025-12-04 06:14:50.816743+00 |
| description | Lightweight wrapper for clap to auto-handle version and help / clap 轻量封装,自动处理版本与帮助 |
| homepage | https://github.com/js0-site/rust/tree/dev/clap_args |
| repository | https://github.com/js0-site/rust.git |
| max_upload_size | |
| id | 1742195 |
| size | 24,297 |
A simplified wrapper around clap that streamlines command-line argument parsing by automatically handling version and help flags.
Cargo.toml.--vv.-h and --help flags.parse! macro simplifies initialization.Add to Cargo.toml:
[dependencies]
clap_args = { version = "*", features = ["macro"] }
use clap_args::{arg, ArgAction};
fn main() -> aok::Result<()> {
if let Some(matches) = clap_args::parse!(|cmd| {
cmd
.arg(arg!(-b --bind [BIND] "http proxy bind address").default_value("0.0.0.0:15080"))
.arg(arg!(-p --port <PORT> "listen port"))
.arg(arg!(-d --debug "enable debug mode").action(ArgAction::SetTrue))
}) {
let bind: &String = matches.get_one("bind").unwrap();
println!("bind: {bind}");
if let Some(port) = matches.get_one::<String>("port") {
println!("port: {port}");
}
if matches.get_flag("debug") {
println!("debug mode enabled");
}
}
Ok(())
}
The core design goal is to reduce boilerplate for common CLI tasks.
Call Flow:
parse! is called with a closure.parse function initializes a clap::Command with default flags (-v, --vv, -h).clap parses the arguments.None is returned.Some(ArgMatches) is returned for the user to process..
├── Cargo.toml # Project configuration
├── examples/ # Usage examples
├── readme/ # Documentation
├── src/ # Source code
│ └── lib.rs # Main library file
└── test.sh # Test script
parse! MacroInitializes the parser with the current package name and version.
clap_args::parse!(|cmd| { ... })
parse FunctionThe underlying function called by the macro.
pub fn parse(
project: impl Into<String>,
ver: impl Borrow<[u64; 3]>,
cmd_build: impl FnOnce(Command) -> Command,
) -> Option<ArgMatches>
arg!: Re-exported from clap for defining arguments.ArgAction: Re-exported from clap for defining argument actions.The concept of command-line arguments dates back to the early days of Unix in the 1970s. The argv (argument vector) convention allowed programs to receive input dynamically at runtime, a significant leap from hardcoded parameters. Over decades, parsing libraries evolved from simple loop-based checks to sophisticated frameworks like clap in Rust, which offer type safety, auto-generated help, and subcommands, reflecting the growing complexity and capability of modern CLI tools.
This project is an open-source component of js0.site ⋅ Refactoring the Internet Plan.
We are redefining the development paradigm of the Internet in a componentized way. Welcome to follow us:
clap 的简化封装,通过自动处理版本和帮助参数,简化命令行参数解析流程。
Cargo.toml 中的版本号。--vv 提供平台和目标信息。-h 和 --help 参数。parse! 宏简化初始化过程。添加到 Cargo.toml:
[dependencies]
clap_args = { version = "*", features = ["macro"] }
use clap_args::{arg, ArgAction};
fn main() -> aok::Result<()> {
if let Some(matches) = clap_args::parse!(|cmd| {
cmd
.arg(arg!(-b --bind [BIND] "http 代理绑定地址").default_value("0.0.0.0:15080"))
.arg(arg!(-p --port <PORT> "监听端口"))
.arg(arg!(-d --debug "启用调试模式").action(ArgAction::SetTrue))
}) {
let bind: &String = matches.get_one("bind").unwrap();
println!("绑定: {bind}");
if let Some(port) = matches.get_one::<String>("port") {
println!("端口: {port}");
}
if matches.get_flag("debug") {
println!("启用调试模式");
}
}
Ok(())
}
核心设计目标是减少通用 CLI 任务的样板代码。
调用流程:
parse! 并传入闭包。parse 函数初始化 clap::Command,配置默认参数(-v, --vv, -h)。clap 解析参数。None。Some(ArgMatches) 供用户处理。.
├── Cargo.toml # 项目配置
├── examples/ # 使用示例
├── readme/ # 文档
├── src/ # 源代码
│ └── lib.rs # 主库文件
└── test.sh # 测试脚本
parse! 宏使用当前包名和版本初始化解析器。
clap_args::parse!(|cmd| { ... })
parse 函数宏调用的底层函数。
pub fn parse(
project: impl Into<String>,
ver: impl Borrow<[u64; 3]>,
cmd_build: impl FnOnce(Command) -> Command,
) -> Option<ArgMatches>
arg!: 从 clap 重新导出,用于定义参数。ArgAction: 从 clap 重新导出,用于定义参数动作。命令行参数的概念可以追溯到 20 世纪 70 年代 Unix 的早期。argv(参数向量)约定允许程序在运行时动态接收输入,这是相对于硬编码参数的重大飞跃。几十年来,解析库从简单的循环检查演变为像 Rust 中的 clap 这样复杂的框架,提供类型安全、自动生成帮助和子命令支持,反映了现代 CLI 工具日益增长的复杂性和能力。
本项目为 js0.site ⋅ 重构互联网计划 的开源组件。
我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注: