[英]How to create a custom derive macro for clap structs in rust?
我正在尝试在 rust 中创建某种简单的 CLI 计费应用程序以供练习。 它是一种数据库应用程序。 我有许多操作,我需要用户能够根据将对其进行操作的列的值进行过滤。
对于下面的示例,有删除和显示命令:
#[derive(Debug, Args)]
struct DeleteCommand {
/// The ID of the bill
#[clap(short, long, value_parser)]
id: Option<String>,
/// The name of the bill
#[clap(short, long, value_parser)]
name: Option<String>,
/// The value of the bill
#[clap(short, long, value_parser)]
value: Option<String>,
/// The amount of the billB
#[clap(short, long, value_parser)]
amount: Option<String>,
/// Datetime
#[clap(short, long, value_parser)]
datetime: Option<String>,
/// Currency
#[clap(short, long, value_parser)]
currency: Option<String>,
/// Recipient
#[clap(short, long, value_parser)]
recipient: Option<String>,
/// Situation
#[clap(short, long, value_parser)]
situation: Option<String>,
/// Hard
#[clap(short = 'H', long)]
hard: bool,
}
#[derive(Debug, Args)]
struct ShowCommand {
#[clap(subcommand)]
subcommand: ShowSubcommand,
/// The ID of the bill
#[clap(short, long, value_parser)]
id: Option<String>,
/// The name of the bill
#[clap(short, long, value_parser)]
name: Option<String>,
/// The value of the bill
#[clap(short, long, value_parser)]
value: Option<String>,
/// The amount of the bill
#[clap(short, long, value_parser)]
amount: Option<String>,
/// Datetime
#[clap(short, long, value_parser)]
datetime: Option<String>,
/// Currency
#[clap(short, long, value_parser)]
currency: Option<String>,
/// Datetime
#[clap(short, long, value_parser)]
recipient: Option<String>,
/// Datetime
#[clap(short, long, value_parser)]
situation: Option<String>,
/// Head
#[clap(long, value_parser)]
head: Option<u32>,
/// Tail
#[clap(long, value_parser)]
tail: Option<u32>,
/// Order by
#[clap(short, long, value_parser)]
orderby: Option<String>,
}
如您所见,两个结构之间有许多公共字段,因为两者都需要过滤数据库中的项目。 我只想创建一个派生自定义宏,让我可以简单地在结构中重复这些行,而无需一次又一次地编写它:
/// The ID of the bill
#[clap(short, long, value_parser)]
id: Option<String>,
/// The name of the bill
#[clap(short, long, value_parser)]
name: Option<String>,
/// The value of the bill
#[clap(short, long, value_parser)]
value: Option<String>,
/// The amount of the bill
#[clap(short, long, value_parser)]
amount: Option<String>,
/// Datetime
#[clap(short, long, value_parser)]
datetime: Option<String>,
/// Currency
#[clap(short, long, value_parser)]
currency: Option<String>,
/// Recipient
#[clap(short, long, value_parser)]
recipient: Option<String>,
/// Situation
#[clap(short, long, value_parser)]
situation: Option<String>,
我还需要重复 /// 行,因为它将成为 CLI 中每个参数的描述。
最小可重现示例:
这就是我所拥有的
use clap::{Parser, Subcommand, Args};
#[derive(Debug, Parser)]
pub struct UserInput {
#[clap(subcommand)]
command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
/// A command description
A(CommandA),
/// B command description
B(CommandB),
}
#[derive(Debug, Args)]
struct CommandA {
/// The value of column x to be filtered in the database
#[clap(short, long, value_parser)]
x_value: Option<String>,
/// The value of column y to be filtered in the database
#[clap(short, long, value_parser)]
y_value: Option<String>,
/// Some particular parameter of this command
#[clap(short, long, value_parser)]
particular: Option<String>,
}
#[derive(Debug, Args)]
struct CommandB {
/// The value of x to be filtered in the database
#[clap(short, long, value_parser)]
x_value: Option<String>,
/// The value of y to be filtered in the database
#[clap(short, long, value_parser)]
y_value: Option<String>,
/// Some particular parameter of this command
#[clap(short, long, value_parser)]
particular: Option<String>,
}
fn main() {
let user_input: UserInput = UserInput::parse();
print!("{:#?}", user_input)
}
这就是我要的:
use clap::{Parser, Subcommand, Args};
#[derive(Debug, Parser)]
pub struct UserInput {
#[clap(subcommand)]
command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
/// A command description
A(CommandA),
/// B command description
B(CommandB),
}
#[derive(Debug, Args, Filter)]
struct CommandA {
/// Some particular parameter command A
#[clap(short, long, value_parser)]
particular: Option<String>,
}
#[derive(Debug, Args, Filter)]
struct CommandB {
/// Some particular parameter command B
#[clap(short, long, value_parser)]
particular: Option<String>,
}
fn main() {
let user_input: UserInput = UserInput::parse();
print!("{:#?}", user_input)
}
等待行为:
[user@host appname]$ appname a --help
p1-a
A command description
USAGE:
p1 a [OPTIONS]
OPTIONS:
-x, --x-value <X_VALUE> The value of column X to be filtered in the database
-y, --y-value <Y_VALUE> The value of column Y to be filtered in the database
-h, --help Print help information
-p, --particular <PARTICULAR> Some particular parameter of command A
编辑:我正在使用 clap 3.2.22,但我会尝试将我的代码移动到最新版本。
您可以像这样使用组合和#[clap(flatten)]
来实现您想要的效果:
use clap::{Args, Parser, Subcommand};
#[derive(Debug, Args)]
struct CommonArgs {
/// The value of column x to be filtered in the database
#[clap(short, long, value_parser)]
x_value: Option<String>,
/// The value of column y to be filtered in the database
#[clap(short, long, value_parser)]
y_value: Option<String>,
}
#[derive(Debug, Args)]
struct ArgsA {
#[clap(flatten)]
common_args: CommonArgs,
/// Hard
#[clap(short = 'H', long)]
particular_a: bool,
}
#[derive(Debug, Args)]
struct ArgsB {
// #[clap(subcommand)]
// subcommand: ShowSubcommand,
#[clap(flatten)]
commmon_args: CommonArgs,
/// Head
#[clap(long)]
particular_b: Option<u32>,
}
#[derive(Debug, Parser)]
pub struct UserInput {
#[clap(subcommand)]
command: MyCommand,
}
#[derive(Debug, Subcommand)]
enum MyCommand {
/// A command description
A(ArgsA),
/// B command description
B(ArgsB),
}
fn main() {
dbg!(UserInput::parse_from("playground b --help".split(' ')));
}
唯一的区别是您可以在访问时多一级间接访问。
这种“缺点”带来的好处是您可以更轻松地传递 arguments 的部分内容,例如为ArgsA
和ArgsB
实施AsRef<CommonArgs>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.