aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-08-24 09:16:44 +0100
committerAkshay <[email protected]>2020-08-24 09:16:44 +0100
commit9c71fb1a39964a8cb5696315c9a998a4872d7aab (patch)
tree2f13a9f31e6ac51db28058b4b4077b27b8765211 /src
parent7308121fa3efc785bf964b8ab32505ba25513fdc (diff)
add help text
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs17
-rw-r--r--src/main.rs7
2 files changed, 23 insertions, 1 deletions
diff --git a/src/cli.rs b/src/cli.rs
index 009397d..e0b745d 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -5,6 +5,7 @@ use std::default::Default;
5use std::path::PathBuf; 5use std::path::PathBuf;
6 6
7pub struct Config { 7pub struct Config {
8 pub help: bool,
8 pub port: u16, 9 pub port: u16,
9 pub db_path: PathBuf, 10 pub db_path: PathBuf,
10} 11}
@@ -12,6 +13,7 @@ pub struct Config {
12impl Default for Config { 13impl Default for Config {
13 fn default() -> Self { 14 fn default() -> Self {
14 Config { 15 Config {
16 help: false,
15 port: 3000, 17 port: 3000,
16 db_path: "./urls.db_3".into(), 18 db_path: "./urls.db_3".into(),
17 } 19 }
@@ -22,9 +24,24 @@ lazy_static! {
22 pub static ref CONFIG: Config = parse_args().unwrap_or(Default::default()); 24 pub static ref CONFIG: Config = parse_args().unwrap_or(Default::default());
23} 25}
24 26
27pub static HELP_TEXT: &'static str = "
28Usage
29-----
30
31hedge [-h | --help] [--port <number>] [--database <path>]
32
33Options
34-------
35
36 -h, --help Prints help information
37 --port Port to start the server on (default: 3000)
38 --database Path to database (default: urls.db_3)
39";
40
25fn parse_args() -> Result<Config> { 41fn parse_args() -> Result<Config> {
26 let mut _a = pico_args::Arguments::from_env(); 42 let mut _a = pico_args::Arguments::from_env();
27 return Ok(Config { 43 return Ok(Config {
44 help: _a.contains(["-h", "--help"]),
28 port: _a 45 port: _a
29 .opt_value_from_fn("--port", str::parse::<u16>)? 46 .opt_value_from_fn("--port", str::parse::<u16>)?
30 .unwrap_or(7878), 47 .unwrap_or(7878),
diff --git a/src/main.rs b/src/main.rs
index 5cd3492..c1e3268 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,11 +12,16 @@ mod service;
12use service::shortner_service; 12use service::shortner_service;
13 13
14mod cli; 14mod cli;
15use cli::CONFIG; 15use cli::{CONFIG, HELP_TEXT};
16 16
17fn main() -> Result<()> { 17fn main() -> Result<()> {
18 pretty_env_logger::init(); 18 pretty_env_logger::init();
19 19
20 if CONFIG.help {
21 println!("{}", HELP_TEXT);
22 return Ok(());
23 }
24
20 init_db(&CONFIG.db_path)?; 25 init_db(&CONFIG.db_path)?;
21 smol::run(async { 26 smol::run(async {
22 let addr = ([127, 0, 0, 1], CONFIG.port).into(); 27 let addr = ([127, 0, 0, 1], CONFIG.port).into();