Skip to content

How to Add Custom CLI Flags/Options? #417

Answered by haoqixu
h2cone asked this question in Q&A
Discussion options

You must be logged in to vote

pingora is using clap-3.x which also supports flatten:

use clap::Parser;

#[derive(Parser)]
struct MyOpt {
    #[clap(short)]
    something_cool: String,

    #[clap(flatten)]
    base_opts: Opt
}

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

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

Run with -h flag and get:

load_balancer
Command-line options

USAGE:
    load_balancer [OPTIONS] -s <SOMETHING_COOL>

OPTIONS:
    -c, --conf <CONF>          The path to the configuration file.
    -d, --daemon               Whether this server should run in the background
    -h, --help                 Print help information
    -s <SOMETHING_COOL>
    -t, --test           …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@h2cone
Comment options

Answer selected by h2cone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants