< 返回版块

坚果修补匠 发表于 2021-06-03 08:56

我想要实现一个命令行程序,模式如下:

kvs set <key> <value>
kvs get <key>
kvs rm <key>

。然而,我不明白如何用clap编写。clap中的Arg要求必须有连字符-连接作为参数。例如:

kvs -set=<key> -value=<value>

求一个例程


Ext Link: https://docs.rs/clap/2.33.3/clap/index.html

评论区

写评论
作者 坚果修补匠 2021-06-03 10:22

十分感谢

--
👇
johnmave126:

use clap::{App, Arg, SubCommand};

fn main() {
    let matches = App::new("Example Program")
        .version(clap::crate_version!())
        .author(clap::crate_authors!())
        .about(clap::crate_description!())
        .subcommands(vec![
            SubCommand::with_name("set")
                .about("set key-value")
                .arg(Arg::with_name("key").required(true).index(1))
                .arg(Arg::with_name("value").required(true).index(2)),
            SubCommand::with_name("get")
                .about("get value")
                .arg(Arg::with_name("key").required(true).index(1)),
            SubCommand::with_name("rm")
                .about("remove value")
                .arg(Arg::with_name("key").required(true).index(1)),
        ])
        .get_matches();

    if let Some(matches) = matches.subcommand_matches("set") {
        let key = matches.value_of("key").unwrap();
        let value = matches.value_of("value").unwrap();
        println!("Setting {} to {}", key, value);
    } else if let Some(matches) = matches.subcommand_matches("get") {
        let key = matches.value_of("key").unwrap();
        println!("Getting value of {}", key);
    } else if let Some(matches) = matches.subcommand_matches("rm") {
        let key = matches.value_of("key").unwrap();
        println!("Removing value of {}", key);
    }
}

johnmave126 2021-06-03 09:32
use clap::{App, Arg, SubCommand};

fn main() {
    let matches = App::new("Example Program")
        .version(clap::crate_version!())
        .author(clap::crate_authors!())
        .about(clap::crate_description!())
        .subcommands(vec![
            SubCommand::with_name("set")
                .about("set key-value")
                .arg(Arg::with_name("key").required(true).index(1))
                .arg(Arg::with_name("value").required(true).index(2)),
            SubCommand::with_name("get")
                .about("get value")
                .arg(Arg::with_name("key").required(true).index(1)),
            SubCommand::with_name("rm")
                .about("remove value")
                .arg(Arg::with_name("key").required(true).index(1)),
        ])
        .get_matches();

    if let Some(matches) = matches.subcommand_matches("set") {
        let key = matches.value_of("key").unwrap();
        let value = matches.value_of("value").unwrap();
        println!("Setting {} to {}", key, value);
    } else if let Some(matches) = matches.subcommand_matches("get") {
        let key = matches.value_of("key").unwrap();
        println!("Getting value of {}", key);
    } else if let Some(matches) = matches.subcommand_matches("rm") {
        let key = matches.value_of("key").unwrap();
        println!("Removing value of {}", key);
    }
}

rex-ma-2015 2021-06-03 09:03

https://github.com/ClementNerma/ReBackup

这个项目可以满足你的要求

1 共 3 条评论, 1 页