0

The documentation has got me confused. I'm kind of stuck on how to get matches from a parser derived struct. How would I go about doing this? Here's what my argument struct looks like.

#[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,
}
Herohtar
  • 5,347
  • 4
  • 31
  • 41
Lord of Grok
  • 323
  • 3
  • 12

1 Answers1

1

The clap::Parser trait has a method parse that you can use, that will read the arguments into a structure of the type, or exit the process on error.

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);
}
Frxstrem
  • 38,761
  • 9
  • 79
  • 119
  • Alright, thanks for that. How about if I wanted to get an argument that has a list of values? How would I get the list of values using this parser method? – Lord of Grok Apr 13 '22 at 14:24
  • 1
    just use Vec as the type of option field: `#[clap(short, long)] pub hosts: Vec` and Clap will figure it out. – Ivan C Apr 13 '22 at 14:50