aboutsummaryrefslogtreecommitdiff
path: root/src/cli.rs
blob: f1563b334e5b6d99a66232bbef6bb3b20f947f1f (plain)
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
52
53
54
55
56
57
58
59
60
use std::env;

#[derive(Debug, PartialEq)]
pub enum Action {
    NewConfig,
    NewState,
    DumpTrajectory,
    DumpLearner,
    DumpAlbums,
    Run,
}

const NAME: &str = env!("CARGO_PKG_NAME");
const VERSION: &str = env!("CARGO_PKG_VERSION");
const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");

fn print_help() {
    println!(
        "\
        {NAME}: {DESCRIPTION}
Usage: {NAME} [OPTION]

Options:
  --help            Show this help message.
  --version         Show version information.
  --new-config      Write a default config file and exit.
  --new-state       Write a default state file and exit.
  --dump-trajectory Dump the play trajectory as JSON to stdout.
  --dump-learner    Dump learner weights as JSON to stdout.
  --dump-albums     Dump album list from state as JSON to stdout.
<no option>       Suggest and play the next album"
    );
}

pub fn parse() -> Option<Action> {
    let mut args = env::args();

    let _binary_name = args.next();

    match args.next().as_deref() {
        None => Some(Action::Run),
        Some("--help") | Some("-h") => {
            print_help();
            None
        }
        Some("--version") | Some("-V") | Some("-v") => {
            println!("{NAME} {VERSION}");
            None
        }
        Some("--new-config") => Some(Action::NewConfig),
        Some("--new-state") => Some(Action::NewState),
        Some("--dump-trajectory") => Some(Action::DumpTrajectory),
        Some("--dump-learner") => Some(Action::DumpLearner),
        Some("--dump-albums") => Some(Action::DumpAlbums),
        Some(unknown) => {
            eprintln!("Unknown option: {unknown}");
            std::process::exit(1);
        }
    }
}