summaryrefslogtreecommitdiffstats
path: root/src/cli.rs
blob: 6a75b5dac727961f8a7a2f8b5f64cff1af35cb09 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::fmt::{Debug, Formatter, Error};

extern crate clap;
use clap::{App, ArgMatches};

pub struct ModuleConfig {
    pub load : bool,
}

impl ModuleConfig {
    pub fn new() -> ModuleConfig {
        ModuleConfig {
            load: false,
        }
    }
}

pub struct CliConfig<'a> {
    pub module_configs  : Vec<ModuleConfig>,
    pub cli_matches     : ArgMatches<'a, 'a>,
}

impl<'a> CliConfig<'a> {
    pub fn new(app : clap::App<'a, 'a, 'a, 'a, 'a, 'a>) -> CliConfig<'a> {
        CliConfig {
            module_configs: vec![],
            cli_matches: app.get_matches(),
        }
    }

    /**
     * Check whether the CLI says we should run verbose
     */
    pub fn is_verbose(&self) -> bool {
        self.cli_matches.is_present("verbose") || self.is_debugging()
    }

    /**
     * Check whether the CLI says we should run in debugging
     */
    pub fn is_debugging(&self) -> bool {
        self.cli_matches.is_present("debug")
    }

    /**
     * Get the runtime path the CLI configured
     */
    pub fn get_rtp(&self) -> Option<String> {
        self.cli_matches.value_of("rtp").and_then(|s| Some(String::from(s)))
    }

    /**
     * Get the store path the CLI configured
     *
     * TODO: Implement properly. Not working by now.
     */
    pub fn store_path(&self) -> Option<String> {
        self.get_rtp().and_then(|rtp| {
            self.cli_matches
                .value_of("storepath")
                .and_then(|s| Some(rtp + s))
        })
    }

    pub fn editor(&self) -> Option<String> {
        self.cli_matches.value_of("editor").and_then(|s| Some(String::from(s)))
    }

    pub fn editor_opts(&self) -> String {
        self.cli_matches
            .value_of("editor_opts")
            .map(|s| String::from(s))
            .unwrap_or(String::from(""))
    }
}

impl<'a> Debug for CliConfig<'a> {

    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "CliConfig (verbose: {}, debugging: {}, rtp: {})",
            self.is_verbose(),
            self.is_debugging(),
            self.get_rtp().or(Some(String::from("NONE"))).unwrap())
    }

}