如何用 clap 调用函数

问题描述 投票:0回答:1

我想使用

store_blocklist::insert_blocklist()
 来调用函数 
clap

pub fn args() -> Command {
    Command::new("admin-cli")
        .about("CLI to manage admin tasks")
        .subcommand_required(true)
        .arg_required_else_help(true)
        .allow_external_subcommands(false)
        .subcommand(
            Command::new("blocklist")
                .about("Storing the blocklists of SteveBlack in the database")
                //call store_blocklist::insert_blocklist() here
        )
}

我该怎么做?

rust clap
1个回答
0
投票

这不是 Clap 的工作。 Clap 是一个命令行参数解析器,仅此而已。

Clap 处理完用户的参数后,您可以对它们执行任何您想要的操作,包括调用您的函数:

use clap::{Arg, ArgAction, Command};

pub fn args() -> Command {
    Command::new("admin-cli")
        .about("CLI to manage admin tasks")
        .arg_required_else_help(true)
        .allow_external_subcommands(false)
        .arg(
            Arg::new("blocklist")
                .long("blocklist")
                .help("Storing the blocklists of SteveBlack in the database")
                .action(ArgAction::SetTrue),
        )
}

fn main() {
    let cmd = args();
    let args = cmd.get_matches();

    if args.get_flag("blocklist") {
        // Call your function here.
        println!("Hi from blocklist!");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.