aboutsummaryrefslogtreecommitdiffstats
path: root/src/cmd.rs
blob: 6566e1890caaf45f19fe1094834ddacf785dd4ba (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
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::prelude::*;

mod play;
mod record;
mod server;
mod stream;
mod watch;

struct Command {
    name: &'static str,
    cmd: &'static dyn for<'a, 'b> Fn(clap::App<'a, 'b>) -> clap::App<'a, 'b>,
    run: &'static dyn for<'a> Fn(&clap::ArgMatches<'a>) -> Result<()>,
}

const COMMANDS: &[Command] = &[
    Command {
        name: "stream",
        cmd: &stream::cmd,
        run: &stream::run,
    },
    Command {
        name: "server",
        cmd: &server::cmd,
        run: &server::run,
    },
    Command {
        name: "watch",
        cmd: &watch::cmd,
        run: &watch::run,
    },
    Command {
        name: "record",
        cmd: &record::cmd,
        run: &record::run,
    },
    Command {
        name: "play",
        cmd: &play::cmd,
        run: &play::run,
    },
];

pub fn parse<'a>() -> Result<clap::ArgMatches<'a>> {
    let mut app = clap::App::new(crate::util::program_name()?)
        .about("Stream your terminal for other people to watch")
        .author(clap::crate_authors!())
        .version(clap::crate_version!());

    for cmd in COMMANDS {
        let subcommand = clap::SubCommand::with_name(cmd.name);
        app = app.subcommand((cmd.cmd)(subcommand));
    }

    app.get_matches_safe().context(crate::error::ParseArgs)
}

pub fn run(matches: &clap::ArgMatches<'_>) -> Result<()> {
    for cmd in COMMANDS {
        if let Some(submatches) = matches.subcommand_matches(cmd.name) {
            return (cmd.run)(submatches);
        }
    }
    (COMMANDS[0].run)(&clap::ArgMatches::<'_>::default())
}