Parsing arguments

Similar to the examples in the previous chapter, we have a parser for arguments. It has the following declaration:

let matches = App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.setting(AppSettings::SubcommandRequired)
.arg(
Arg::with_name("database")
.short("d")
.long("db")
.value_name("FILE")
.help("Sets a file name of a database")
.takes_value(true),
)
.subcommand(SubCommand::with_name(CMD_ADD).about("add user to the table")
.arg(Arg::with_name("NAME")
.help("Sets the name of a user")
.required(true)
.index(1))
.arg(Arg::with_name("EMAIL")
.help("Sets the email of a user")
.required(true)
.index(2)))
.subcommand(SubCommand::with_name(CMD_LIST).about("prints a list with users"))
.get_matches();

We can get --database arguments with a path to a database file. The  add subcommand requires two arguments—NAME with the name of a user, and EMAIL with their email. The list subcommand doesn't require extra arguments and will print a list of users.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.164.75