diff options
author | Jesse Luehrs <doy@tozt.net> | 2018-11-07 10:01:18 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2018-11-07 10:01:18 -0500 |
commit | 1e1a47061c455a5324d8a62fa9bcee9ecdf9c4cb (patch) | |
tree | 905d0a1f8bdc8995e924a559f1dee313a612079a /src | |
parent | 2f5bc1cb9f31d431d017d173728fd1e782c97c8e (diff) | |
download | lastfm-query-1e1a47061c455a5324d8a62fa9bcee9ecdf9c4cb.tar.gz lastfm-query-1e1a47061c455a5324d8a62fa9bcee9ecdf9c4cb.zip |
better command line parsing
Diffstat (limited to 'src')
-rw-r--r-- | src/cli.rs | 28 | ||||
-rw-r--r-- | src/main.rs | 12 |
2 files changed, 33 insertions, 7 deletions
diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..4e1d7cb --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,28 @@ +use clap; + +const _DUMMY_DEPENDENCY: &'static str = include_str!("../Cargo.toml"); + +pub struct Options { + pub username: String, + pub api_key: String, +} + +pub fn get_options() -> Options { + let matches = app_from_crate!() + .arg( + clap::Arg::with_name("username") + .required(true) + .help("last.fm username to fetch tracks for") + ) + .arg( + clap::Arg::with_name("api_key") + .required(true) + .help("last.fm api key") + ) + .get_matches(); + + Options { + username: matches.value_of("username").unwrap().to_string(), + api_key: matches.value_of("api_key").unwrap().to_string(), + } +} diff --git a/src/main.rs b/src/main.rs index 3434827..c4c255e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#[macro_use] +extern crate clap; extern crate directories; extern crate failure; extern crate indicatif; @@ -8,6 +10,7 @@ extern crate serde_json; #[macro_use] extern crate serde_derive; +mod cli; mod error; mod exporter; mod lastfm; @@ -15,16 +18,11 @@ mod paths; mod db; fn main() { - let args: Vec<_> = std::env::args().collect(); - if args.len() < 3 { - panic!("usage: {} USERNAME API_KEY", args[0]); - } - let username = &args[1]; - let api_key = &args[2]; + let opts = cli::get_options(); let db = db::DB::new(&paths::dbpath()) .expect("failed to create db"); - let lastfm = lastfm::LastFMClient::new(api_key, username); + let lastfm = lastfm::LastFMClient::new(&opts.api_key, &opts.username); let exporter = exporter::Exporter::new(&db, &lastfm); let to_fetch = exporter.tracks_to_sync().unwrap(); |