Skip to content

Commit

Permalink
Separate clap args into its own mod
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinastone committed Dec 15, 2023
1 parent 3aaf56a commit 89f5e95
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
44 changes: 44 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use clap::CommandFactory;
pub use clap::Parser;
use clap_complete::{generate, Generator, Shell};
use std::io;
use std::num::NonZeroUsize;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None, disable_help_flag = true)]
pub struct Cli {
#[arg(long, exclusive = true)]
pub completions: Option<Shell>,

#[arg(
short,
long,
env,
default_value = "0.0.0.0",
help = "Host address to listen on"
)]
pub host: String,

#[arg(
short,
long,
env,
default_value_t = 3000,
help = "Port to listen on"
)]
pub port: u16,

#[arg(long, env, help = "Number of threads to process requests")]
pub threads: Option<NonZeroUsize>,

#[arg(long, action = clap::ArgAction::Help, help = "Print help information")]
pub help: (),
}

impl Cli {
pub fn print_completions<G: Generator>(&self, gen: G) {
let mut cmd = Self::command();
let bin_name = cmd.get_name().to_string();
generate(gen, &mut cmd, bin_name, &mut io::stdout());
}
}
44 changes: 3 additions & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use clap::{Command, CommandFactory, Parser};
use clap_complete::{generate, Generator, Shell};
use std::io;
use crate::args::*;
use std::net::ToSocketAddrs;
use std::num::NonZeroUsize;
use tokio::net::TcpListener;
use tokio::{runtime, signal};
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

mod args;
mod handler;
mod headers;
mod http;
Expand All @@ -21,41 +19,6 @@ mod service;
#[cfg(test)]
mod test;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None, disable_help_flag = true)]
struct Cli {
#[arg(
short,
long,
env,
default_value = "0.0.0.0",
help = "Host address to listen on"
)]
host: String,

#[arg(
short,
long,
env,
default_value_t = 3000,
help = "Port to listen on"
)]
port: u16,

#[arg(long, env, help = "Number of threads to process requests")]
threads: Option<NonZeroUsize>,

#[arg(long)]
completions: Option<Shell>,

#[arg(long, action = clap::ArgAction::Help, help = "Print help information")]
help: (),
}

fn print_completions<G: Generator>(gen: G, app: &mut Command) {
generate(gen, app, app.get_name().to_string(), &mut io::stdout());
}

async fn shutdown_signal() {
// Wait for the CTRL+C signal
let ctrl_c = async {
Expand Down Expand Up @@ -93,8 +56,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Cli::parse();

if let Some(generator) = args.completions {
let mut app = Cli::command();
print_completions(generator, &mut app);
args.print_completions(generator);
return Ok(());
}

Expand Down

0 comments on commit 89f5e95

Please sign in to comment.