如何从解析器派生结构中获取匹配项?

How to Get Matches From a Parser Derived Struct?

文档让我感到困惑。我有点卡在如何从解析器派生结构中获取匹配项上。我该怎么做呢?这是我的参数结构的样子。

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
pub struct Args {
    /// Host address
    #[clap(short, long)]
    pub host: String,
    /// Database username
    #[clap(short, long)]
    pub username: String,
    /// Database password
    #[clap(short='P', long)]
    pub password: String,
    /// Database name
    #[clap(short='d', long)]
    pub database: String,
    /// Database port number
    #[clap(short, long, default_value_t = 3306)]
    pub port: u32,
}

您可以使用的 clap::Parser trait has a method parse,它将参数读入该类型的结构,或在出错时退出进程。

use clap::Parser;

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
pub struct Args {
    /// Host address
    #[clap(short, long)]
    pub host: String,
    ...
}

fn main() {
  let args = Args::parse();
  println!("got host: {}", args.host);
}