Skip to content

Commit 54b7cf8

Browse files
committed
Customize CLI help flag to allow -h for host
1 parent a2983d2 commit 54b7cf8

3 files changed

Lines changed: 159 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ tokio = { version = "1.49.0", features = ["full"] }
1616
async-trait = "0.1.89"
1717
futures = "0.3.31"
1818
config = { version = "0.15.19", default-features = false, features = ["toml"] }
19+
clap = { version = "4.5.54", features = ["derive"] }
1920

2021
[dev-dependencies]
2122
tempfile = "3.10.1"

src/main.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use async_trait::async_trait;
2+
use clap::Parser;
23
use config::{Config, Environment, File};
34
use serde::Deserialize;
45
use futures::stream;
@@ -26,6 +27,23 @@ use crate::db::DB;
2627
use crate::parser as argus_parser;
2728
use crate::query::{Statement, execute_plan};
2829

30+
/// ArgusDB Server
31+
#[derive(Parser, Debug)]
32+
#[command(author, version, about, long_about = None, disable_help_flag = true)]
33+
struct Args {
34+
/// Host to bind to
35+
#[arg(short = 'h', long)]
36+
host: Option<String>,
37+
38+
/// Port to bind to
39+
#[arg(short, long)]
40+
port: Option<u16>,
41+
42+
/// Print help
43+
#[arg(long, action = clap::ArgAction::Help)]
44+
help: Option<bool>,
45+
}
46+
2947
#[derive(Debug, Deserialize)]
3048
struct Settings {
3149
#[serde(default = "default_host")]
@@ -160,13 +178,20 @@ impl PgWireServerHandlers for ArgusProcessor {
160178

161179
#[tokio::main]
162180
async fn main() {
163-
let settings = Config::builder()
181+
let args = Args::parse();
182+
183+
let mut builder = Config::builder()
164184
.add_source(File::with_name("argusdb").required(false))
165-
.add_source(Environment::with_prefix("ARGUS"))
166-
.build()
167-
.unwrap()
168-
.try_deserialize::<Settings>()
169-
.unwrap();
185+
.add_source(Environment::with_prefix("ARGUS"));
186+
187+
if let Some(host) = args.host {
188+
builder = builder.set_override("host", host).unwrap();
189+
}
190+
if let Some(port) = args.port {
191+
builder = builder.set_override("port", port).unwrap();
192+
}
193+
194+
let settings: Settings = builder.build().unwrap().try_deserialize().unwrap();
170195

171196
let db = Arc::new(Mutex::new(DB::new("argus_data")));
172197
let handler = Arc::new(ArgusHandler::new(db));

0 commit comments

Comments
 (0)