Rust库 - 命令行解析库:clap

1,719 阅读1分钟

clap是Rust的命令行解析库。号称“简单、高效、功能强大”,看了一下文档支持的功能还挺多的,这里就不列举了。感兴趣的可以看一下官方文档介绍,链接在最后。下面直接看一个例子:


// [dependencies]
// clap = "2.33.0"

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

fn main() {
    let matches = App::new("My Test Program")
        .version("0.1.0")
        .author("Hackerman Jones <hckrmnjones@hack.gov>")
        .about("Teaches argument parsing")
        .arg(
            Arg::with_name("file")
                .short("f")
                .long("file")
                .takes_value(true)
                .help("A cool file"))
        .arg(Arg::with_name("INPUT")
             .help("Sets the input file to use")
             .required(true)
             .index(1))
        .arg(Arg::with_name("OUTPUT")
             .help("Sets the output file to use")
             .required(true)
             .index(2))
        .arg(
            Arg::with_name("num")
                .short("n")
                .long("number")
                .takes_value(true)
                .help("Five less than your favorite number"))
        .arg(Arg::with_name("v")
             .short("v")
             .multiple(true)
             .help("Sets the level of verbosity"))
        .subcommand(SubCommand::with_name("test")
                    .about("controls testing features")
                    .version("1.3")
                    .author("Someone E. <someone_else@other.com>")
                    .arg(Arg::with_name("debug")
                         .short("d")
                         .help("print debug information verbosely")))
        .get_matches();

    let myfile = matches.value_of("file").unwrap_or("input.txt");
    println!("The file passed is: {}", myfile);

    println!("Using input file: {}", matches.value_of("INPUT").unwrap());
    println!("Using output file: {}", matches.value_of("OUTPUT").unwrap());

    if let Some(num_str) = matches.value_of("num") {
        if let Ok(n) = num_str.parse::<i32>() {
           println!("num: {}", n);
        }
    }

    match matches.occurrences_of("v") {
        0 => println!("No verbose info"),
        1 => println!("Some verbose info"),
        2 => println!("Tons of verbose info"),
        3 | _ => println!("Don't be crazy"),
    }

    if let Some(matches) = matches.subcommand_matches("test") {
        if matches.is_present("debug") {
            println!("Printing debug info...");
        } else {
            println!("Printing normally...");
        }
    }
}

输出:

 ./target/debug/clap-demo --file /tmp/a.rs -n 12 a.txt -vv b.txt test -d
The file passed is: /tmp/a.rs
Using input file: a.txt
Using output file: b.txt
num: 12
Tons of verbose info
Printing debug info...

从上面的例子中可以看出,使用还是很简洁的,基本上都是自解释的。


参考