aboutsummaryrefslogtreecommitdiff
path: root/src/cli.rs
blob: 79753d46896a5ca10e85a337ce1b5f03135c5f05 (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
use anyhow::Result;
use lazy_static::lazy_static;

use std::default::Default;
use std::path::PathBuf;

pub struct Config {
    pub help: bool,
    pub port: u16,
    pub db_path: PathBuf,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            help: false,
            port: 3000,
            db_path: "./urls.db_3".into(),
        }
    }
}

lazy_static! {
    pub static ref CONFIG: Config = parse_args().unwrap_or(Default::default());
}

pub static HELP_TEXT: &'static str = "
Usage
-----

isostatic [-h | --help] [--port <number>] [--database <path>]

Options
-------

    -h, --help       Prints help information
        --port       Port to start the server on (default: 3000)
        --database   Path to database (default: urls.db_3)
";

fn parse_args() -> Result<Config> {
    let mut _a = pico_args::Arguments::from_env();
    return Ok(Config {
        help: _a.contains(["-h", "--help"]),
        port: _a
            .opt_value_from_fn("--port", str::parse::<u16>)?
            .unwrap_or(7878),
        db_path: _a
            .opt_value_from_str("--database")?
            .unwrap_or(PathBuf::from("./urls.db_3")),
    });
}