Parsing arguments

Our tool expects two subcommands: add and list. Let's add them to a clap::App instance. Like all previous examples, we also added a --database argument to set the connection URL. Look at the following code:

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("ADDR")
.help("Sets an address of db connection")
.takes_value(true),
)
.subcommand(SubCommand::with_name(CMD_ADD).about("add user to the table")
.arg(Arg::with_name("USER_ID")
.help("Sets the id of a user")
.required(true)
.index(1))
.arg(Arg::with_name("ACTIVITY")
.help("Sets the activity of a user")
.required(true)
.index(2)))
.subcommand(SubCommand::with_name(CMD_LIST).about("print activities list of users"))
.get_matches();

The add subcommand expects two parameters: USER_ID and ACTIVITY. Both are represented as String type values in the Activity struct. We will require these arguments, but we'll get any provided values without any restrictions. The list subcommand has no extra arguments.

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

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