diff options
-rw-r--r-- | readme.md | 16 | ||||
-rw-r--r-- | src/cli.rs | 17 | ||||
-rw-r--r-- | src/main.rs | 7 |
3 files changed, 39 insertions, 1 deletions
@@ -8,3 +8,19 @@ | |||
8 | $ crate2nix generate | 8 | $ crate2nix generate |
9 | $ nix-build | 9 | $ nix-build |
10 | ``` | 10 | ``` |
11 | |||
12 | ### usage | ||
13 | |||
14 | ``` | ||
15 | Usage | ||
16 | ----- | ||
17 | |||
18 | hedge [-h | --help] [--port <number>] [--database <path>] | ||
19 | |||
20 | Options | ||
21 | ------- | ||
22 | |||
23 | -h, --help Prints help information | ||
24 | --port Port to start the server on (default: 3000) | ||
25 | --database Path to database (default: urls.db_3) | ||
26 | ``` | ||
@@ -5,6 +5,7 @@ use std::default::Default; | |||
5 | use std::path::PathBuf; | 5 | use std::path::PathBuf; |
6 | 6 | ||
7 | pub struct Config { | 7 | pub 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 { | |||
12 | impl Default for Config { | 13 | impl 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 | ||
27 | pub static HELP_TEXT: &'static str = " | ||
28 | Usage | ||
29 | ----- | ||
30 | |||
31 | hedge [-h | --help] [--port <number>] [--database <path>] | ||
32 | |||
33 | Options | ||
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 | |||
25 | fn parse_args() -> Result<Config> { | 41 | fn 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; | |||
12 | use service::shortner_service; | 12 | use service::shortner_service; |
13 | 13 | ||
14 | mod cli; | 14 | mod cli; |
15 | use cli::CONFIG; | 15 | use cli::{CONFIG, HELP_TEXT}; |
16 | 16 | ||
17 | fn main() -> Result<()> { | 17 | fn 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(); |