From 7308121fa3efc785bf964b8ab32505ba25513fdc Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 24 Aug 2020 13:33:06 +0530 Subject: add cli opts --- src/cli.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/cli.rs (limited to 'src/cli.rs') diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..009397d --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,35 @@ +use anyhow::Result; +use lazy_static::lazy_static; + +use std::default::Default; +use std::path::PathBuf; + +pub struct Config { + pub port: u16, + pub db_path: PathBuf, +} + +impl Default for Config { + fn default() -> Self { + Config { + port: 3000, + db_path: "./urls.db_3".into(), + } + } +} + +lazy_static! { + pub static ref CONFIG: Config = parse_args().unwrap_or(Default::default()); +} + +fn parse_args() -> Result { + let mut _a = pico_args::Arguments::from_env(); + return Ok(Config { + port: _a + .opt_value_from_fn("--port", str::parse::)? + .unwrap_or(7878), + db_path: _a + .opt_value_from_str("--database")? + .unwrap_or(PathBuf::from("./urls.db_3")), + }); +} -- cgit v1.2.3