Skip to content

Commit

Permalink
Use clap to parse server args
Browse files Browse the repository at this point in the history
  • Loading branch information
twizmwazin committed Dec 7, 2023
1 parent efda8b7 commit c0c79f0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
1 change: 1 addition & 0 deletions crates/bh_agent_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ futures = "0.3.28"
log = "0.4.20"
env_logger = "0.10.0"
bimap = "0.6.3"
clap = { version = "4.4.11", features = ["derive"] }
25 changes: 8 additions & 17 deletions crates/bh_agent_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::net::IpAddr;
use std::str::FromStr;

use anyhow::Result;
use clap::Parser;
use futures::{future, prelude::*};
use tarpc::{
server::{self, Channel},
Expand All @@ -11,28 +10,20 @@ use tarpc::{
use bh_agent_common::BhAgentService;
use bh_agent_server::BhAgentServer;

fn parse_args() -> Result<(IpAddr, u16)> {
let args: Vec<String> = std::env::args().collect();
if args.len() != 3 {
return Err(anyhow::anyhow!("Usage: {} <ip_addr> <port>", args[0]));
}

let ip_addr = IpAddr::from_str(&args[1])?;
let port = args[2].parse::<u16>()?;

Ok((ip_addr, port))
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Args {
address: IpAddr,
port: u16,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();

let server_addr = parse_args().or_else(|e| -> Result<(IpAddr, u16)> {
eprintln!("{}", e);
std::process::exit(1);
})?;
let args = Args::parse();

let mut listener = tarpc::serde_transport::tcp::listen(&server_addr, Json::default).await?;
let mut listener = tarpc::serde_transport::tcp::listen(&(args.address, args.port), Json::default).await?;
listener.config_mut().max_frame_length(usize::MAX);
listener
// Ignore accept errors.
Expand Down

0 comments on commit c0c79f0

Please sign in to comment.