summaryrefslogtreecommitdiffstats
path: root/src/cli.rs
blob: 27b27428ce376be7f82922ca0fb8cc7ac2467f7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use clap;
use failure;

const _DUMMY_DEPENDENCY: &'static str = include_str!("../Cargo.toml");

pub enum Command {
    Sync,
}

pub struct Options {
    pub command: Command,
    pub username: Option<String>,
    pub api_key: Option<String>,
}

pub fn get_options() -> failure::Fallible<Options> {
    let matches = app_from_crate!()
        .subcommand(
            clap::SubCommand::with_name("sync")
                .about("Updates the local copy of track data from last.fm")
                .arg(
                    clap::Arg::with_name("username")
                        .short("u")
                        .long("username")
                        .value_name("USERNAME")
                        .required(true)
                        .help("last.fm username to fetch tracks for")
                )
                .arg(
                    clap::Arg::with_name("api-key")
                        .short("k")
                        .long("api-key")
                        .value_name("API_KEY")
                        .required(true)
                        .help("last.fm api key")
                )
        )
        .get_matches();

    let (command, sub_matches) = match matches.subcommand() {
        ("sync", Some(matches)) => (Command::Sync, matches),
        (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()),
        api_key: sub_matches.value_of("api-key").map(|s| s.to_string()),
    })
}