From 613e1196b5e89d320283c4cea44a5d450616a932 Mon Sep 17 00:00:00 2001 From: Sam Tay Date: Mon, 8 Jun 2020 13:56:51 -0700 Subject: Implement --set-api-key --- src/cli.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'src/cli.rs') diff --git a/src/cli.rs b/src/cli.rs index 2bc4553..1f67edd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -4,16 +4,12 @@ use crate::config; use crate::config::Config; use crate::error::Result; -// TODO maybe consts for these keywords? - -// TODO --set-api-key KEY -// TODO --update-sites -// TODO --install-filter-key --force // TODO --sites plural // TODO --add-site (in addition to defaults) pub struct Opts { pub list_sites: bool, pub update_sites: bool, + pub set_api_key: Option, pub query: Option, pub config: Config, } @@ -36,11 +32,18 @@ pub fn get_opts() -> Result { .long("update-sites") .help("Update cache of StackExchange sites"), ) + .arg( + Arg::with_name("set-api-key") + .long("set-api-key") + .number_of_values(1) + .takes_value(true) + .help("Set StackExchange API key"), + ) .arg( Arg::with_name("site") .long("site") .short("s") - .multiple(true) + .multiple(false) // TODO sites plural .number_of_values(1) .takes_value(true) .default_value(&config.site) @@ -67,27 +70,27 @@ pub fn get_opts() -> Result { Arg::with_name("query") .multiple(true) .index(1) - .required_unless_one(&["list-sites", "update-sites"]), + .required_unless_one(&["list-sites", "update-sites", "set-api-key"]), ) .get_matches(); Ok(Opts { list_sites: matches.is_present("list-sites"), update_sites: matches.is_present("update-sites"), + set_api_key: matches.value_of("set-api-key").map(String::from), query: matches .values_of("query") .map(|q| q.collect::>().join(" ")), config: Config { // these unwraps are safe via clap default values & validators limit: matches.value_of("limit").unwrap().parse::().unwrap(), - site: matches.value_of("site").unwrap().to_string(), - // TODO if set_api_key passed, pass it here too - ..config + site: matches.value_of("site").unwrap().to_string(), // TODO values_of + api_key: matches + .value_of("set-api-key") + .map(String::from) + .or(config.api_key), }, }) } -#[cfg(test)] -mod tests { - // TODO how can I test this now that it depends on user dir? -} +// TODO how can I test this App given https://users.rust-lang.org/t/help-with-idiomatic-rust-and-ownership-semantics/43880 -- cgit v1.2.3