From 9c71fb1a39964a8cb5696315c9a998a4872d7aab Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 24 Aug 2020 13:46:44 +0530 Subject: add help text --- readme.md | 16 ++++++++++++++++ src/cli.rs | 17 +++++++++++++++++ src/main.rs | 7 ++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index da6c0e2..48a59ff 100644 --- a/readme.md +++ b/readme.md @@ -8,3 +8,19 @@ $ crate2nix generate $ nix-build ``` + +### usage + +``` +Usage +----- + +hedge [-h | --help] [--port ] [--database ] + +Options +------- + + -h, --help Prints help information + --port Port to start the server on (default: 3000) + --database Path to database (default: urls.db_3) +``` 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; use std::path::PathBuf; pub struct Config { + pub help: bool, pub port: u16, pub db_path: PathBuf, } @@ -12,6 +13,7 @@ pub struct Config { impl Default for Config { fn default() -> Self { Config { + help: false, port: 3000, db_path: "./urls.db_3".into(), } @@ -22,9 +24,24 @@ lazy_static! { pub static ref CONFIG: Config = parse_args().unwrap_or(Default::default()); } +pub static HELP_TEXT: &'static str = " +Usage +----- + +hedge [-h | --help] [--port ] [--database ] + +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 { 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::)? .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; use service::shortner_service; mod cli; -use cli::CONFIG; +use cli::{CONFIG, HELP_TEXT}; fn main() -> Result<()> { pretty_env_logger::init(); + if CONFIG.help { + println!("{}", HELP_TEXT); + return Ok(()); + } + init_db(&CONFIG.db_path)?; smol::run(async { let addr = ([127, 0, 0, 1], CONFIG.port).into(); -- cgit v1.2.3