Executing subcommands

We use the structure of branches to match subcommands from our previous examples:

match matches.subcommand() {
(CMD_ADD, Some(matches)) => {
let token = matches.value_of("TOKEN").unwrap();
let uid = matches.value_of("UID").unwrap();
add_session(&conn, token, uid)?;
}
(CMD_REMOVE, Some(matches)) => {
let token = matches.value_of("TOKEN").unwrap();
remove_session(&conn, token)?;
}
(CMD_LIST, _) => {
println!("LIST");
let sessions = list_sessions(&conn)?;
for (token, uid) in sessions {
println!("Token: {:20} Uid: {:20}", token, uid);
}
}
_ => { matches.usage(); }
}

For the add subcommand, we extract the TOKEN and UID values from arguments and pass them to the add_session function with a reference to a Connector. For the remove subcommand, we extract only the TOKEN value and call the remove_session function with its corresponding parameters. For the list subcommand, we call the list_session function as is, because we don't need any extra parameters to get all values from a map. This returns a vector of pairs. The first item of the pair contains token, and the second contains uid. We print the values using a fixed width specifier of {:20}.

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

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