From 6d4abd89b2dfcdce0bd59d627a31e92fcc745c94 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 8 Nov 2018 02:23:16 -0500 Subject: better options handling --- src/cli.rs | 24 +++++++++++------------- src/cmd/sync.rs | 7 ++----- src/main.rs | 6 +++--- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 0c140e7..1976d93 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,15 +1,12 @@ const _DUMMY_DEPENDENCY: &'static str = include_str!("../Cargo.toml"); pub enum Command { - Sync, + Sync { + username: String, + } } -pub struct Options { - pub command: Command, - pub username: Option, -} - -pub fn get_options() -> failure::Fallible { +pub fn get_options() -> failure::Fallible { let matches = app_from_crate!() .subcommand( clap::SubCommand::with_name("sync") @@ -22,14 +19,15 @@ pub fn get_options() -> failure::Fallible { ) .get_matches(); - let (command, sub_matches) = match matches.subcommand() { - ("sync", Some(matches)) => (Command::Sync, matches), + let command = match matches.subcommand() { + ("sync", Some(matches)) => { + Command::Sync { + username: matches.value_of("username").unwrap().to_string(), + } + }, (name, Some(_)) => bail!("unknown subcommand: {}", name), (_, None) => bail!("no subcommand given"), }; - Ok(Options { - command: command, - username: sub_matches.value_of("username").map(|s| s.to_string()), - }) + Ok(command) } diff --git a/src/cmd/sync.rs b/src/cmd/sync.rs index 9dfb11f..3f80202 100644 --- a/src/cmd/sync.rs +++ b/src/cmd/sync.rs @@ -1,13 +1,10 @@ -use cli; use db; use lastfm; use paths; -pub fn run(opts: &cli::Options) -> failure::Fallible<()> { +pub fn run(username: &str) -> failure::Fallible<()> { let db = db::DB::new(&paths::db_path()?)?; - let lastfm = lastfm::LastFMClient::new( - opts.username.as_ref().unwrap() - )?; + let lastfm = lastfm::LastFMClient::new(username)?; let from = db.most_recent_timestamp()?.map(|x| x + 1); let to_fetch = lastfm.track_count(from)?; diff --git a/src/main.rs b/src/main.rs index d7d80fd..4638adf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,9 +19,9 @@ mod paths; mod db; fn run() -> failure::Fallible<()> { - let opts = cli::get_options()?; - match opts.command { - cli::Command::Sync => cmd::sync::run(&opts), + let command = cli::get_options()?; + match command { + cli::Command::Sync { username } => cmd::sync::run(&username), } } -- cgit v1.2.3