11个版本
0.1.12 | 2023年8月8日 |
---|---|
0.1.10 | 2023年1月11日 |
0.1.9 | 2022年9月22日 |
0.1.8 | 2022年6月29日 |
0.1.1 | 2020年1月31日 |
#12 in #positional-arguments
234,139 每月下载量
在 327 个crate中使用了(通过 argh)
110KB
2K SLoC
Argh
Argh是一个基于派生的、针对代码大小优化的意见派参数解析器
基于派生的、针对代码大小和符合Fuchsia命令行工具规范进行优化的参数解析
该库的公共API主要包括 FromArgs
派生和 from_env
函数,它们可以用于从当前程序的命令行参数生成顶级 FromArgs
类型。
基本示例
use argh::FromArgs;
#[derive(FromArgs)]
/// Reach new heights.
struct GoUp {
/// whether or not to jump
#[argh(switch, short = 'j')]
jump: bool,
/// how high to go
#[argh(option)]
height: usize,
/// an optional nickname for the pilot
#[argh(option)]
pilot_nickname: Option<String>,
}
fn main() {
let up: GoUp = argh::from_env();
}
./some_bin --help
将输出以下内容
Usage: cmdname [-j] --height <height> [--pilot-nickname <pilot-nickname>]
Reach new heights.
Options:
-j, --jump whether or not to jump
--height how high to go
--pilot-nickname an optional nickname for the pilot
--help display usage information
生成的程序可以使用以下任何方式使用
./some_bin --高度 5
./some_bin -j --高度 5
./some_bin --跳跃 --高度 5 --飞行员昵称Wes
类似于 jump
的开关是可选的,如果提供了,则会被设置为true。
类似于 height
和 pilot_nickname
的选项可以是必需的、可选的或重复的,具体取决于它们是否包含在 Option
或 Vec
中。可以使用 #[argh(default = "<your_code_here>")]
属性提供默认值,在这种情况下,选项被视为可选的。
use argh::FromArgs;
fn default_height() -> usize {
5
}
#[derive(FromArgs)]
/// Reach new heights.
struct GoUp {
/// an optional nickname for the pilot
#[argh(option)]
pilot_nickname: Option<String>,
/// an optional height
#[argh(option, default = "default_height()")]
height: usize,
/// an optional direction which is "up" by default
#[argh(option, default = "String::from(\"only up\")")]
direction: String,
}
fn main() {
let up: GoUp = argh::from_env();
}
自定义选项类型可以通过实现 FromArgValue
特性进行反序列化(所有 FromStr
类型自动实现此特性)。如果需要更复杂的解析,可以通过 from_str_fn
属性提供一个自定义的 <T, String>
函数。
use argh::FromArgs;
#[derive(FromArgs)]
/// Goofy thing.
struct FiveStruct {
/// always five
#[argh(option, from_str_fn(always_five))]
five: usize,
}
fn always_five(_value: &str) -> Result<usize, String> {
Ok(5)
}
可以使用 #[argh(positional)]
来声明位置参数。这些参数将按照在结构体中的声明顺序进行解析。
use argh::FromArgs;
#[derive(FromArgs, PartialEq, Debug)]
/// A command with positional arguments.
struct WithPositional {
#[argh(positional)]
first: String,
}
最后一个位置参数可以包含默认值,或者被包裹在 Option
或 Vec
中,以表示可选或重复的位置参数。
也支持子命令。要使用子命令,需要为每个子命令声明一个单独的 FromArgs
类型,以及一个枚举,该枚举的每个情况对应一个命令。
use argh::FromArgs;
#[derive(FromArgs, PartialEq, Debug)]
/// Top-level command.
struct TopLevel {
#[argh(subcommand)]
nested: MySubCommandEnum,
}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand)]
enum MySubCommandEnum {
One(SubCommandOne),
Two(SubCommandTwo),
}
#[derive(FromArgs, PartialEq, Debug)]
/// First subcommand.
#[argh(subcommand, name = "one")]
struct SubCommandOne {
#[argh(option)]
/// how many x
x: usize,
}
#[derive(FromArgs, PartialEq, Debug)]
/// Second subcommand.
#[argh(subcommand, name = "two")]
struct SubCommandTwo {
#[argh(switch)]
/// whether to fooey
fooey: bool,
}
注意:这不是一个官方支持的产品。
如何调试 argh
扩展宏的代码
可以使用 cargo-expand 包调试 argh::FromArgs
扩展宏。
在 examples/simple_example.rs
中展开扩展宏
请参阅 argh/examples/simple_example.rs,了解我们希望扩展的示例结构体。
首先,通过运行 cargo install cargo-expand
安装 cargo-expand
。注意这需要 Rust 的夜间构建版本。
安装完成后,运行 cargo expand
,并在 argh
包中查看展开后的代码。
依赖项
~0.4–1MB
~23K SLoC