cloudflare / pingora

A library for building fast, reliable and evolvable network services.
Apache License 2.0
20.64k stars 1.12k forks source link

[ support ] How to add custom CLI flags / custom config #65

Closed yonas closed 5 months ago

yonas commented 5 months ago

How can I add new CLI flags and config file settings without hacking on pingora_core::server::configuration::Opt directly?

Should we be using a trait instead of directly referencing a fixed struct?

johnhurt commented 5 months ago

Looks like you can use structopt::flatten

I created a simple one to make sure it worked here 😅

use structopt::StructOpt;

#[derive(StructOpt)]
struct MyOpt {
    #[structopt(short)]
    something_cool: String,

    #[structopt(flatten)]
    base_opts: Opt
}

fn main() {
    let my_opts = MyOpt::from_args();

    let mut my_server = Server::new(Some(my_opts.base_opts)).unwrap();

Then if you run with -h you get

my-prox 0.1.0
Commandline options

Call `Opt::from_args()` to build this object from the process's command line arguments.

USAGE:
    my-prox [FLAGS] [OPTIONS] -s <something-cool>

FLAGS:
    -d, --daemon       Whether should run this server in the background
    -h, --help         Prints help information
        --nocapture    Not actually used. This flag is there so that the server is not upset seeing this flag passed
                       from `cargo test` sometimes
    -t, --test         Test the configuration and exit
    -u, --upgrade      Whether this server should try to upgrade from an running old server
    -V, --version      Prints version information

OPTIONS:
    -c, --conf <conf>          The path to the configuration file
    -s <something-cool>   
yonas commented 5 months ago

@johnhurt Thanks, it works!

MrLarssonJr commented 1 month ago

Very helpful!