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

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

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

impl Default for Config {
    fn default() -> Self {
        Config {
            help: false,
            version: 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)
    -v, --version    Print crate version
";

fn parse_args() -> Result<Config> {
    let mut _a = pico_args::Arguments::from_env();
    return Ok(Config {
        help: _a.contains(["-h", "--help"]),
        version: _a.contains(["-v", "--version"]),
        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")),
    });
}

pub fn version() {
    println!("isostatic v{}", env!("CARGO_PKG_VERSION"));
}