0253-CLAP-统计参数出现次数

环境

  • Time 2022-12-02
  • WSL-Ubuntu 22.04
  • CLAP 4.0.29

前言

说明

参考:https://docs.rs/clap/latest/clap/index.html

目标

统计参数出现的次数。

Cargo.toml

[package]
edition = "2021"
name = "game"
version = "1.0.0"

[dependencies]
clap = {version = "4", features = ["cargo"]}

main.rs

use clap::{command, Arg};

fn main() {
    let matches = command!()
        .arg(
            Arg::new("debug")
                .short('d')
                .long("debug")
                .help("调试信息")
                .action(clap::ArgAction::Count),
        )
        .get_matches();

    println!("输入的参数数量是: {}", matches.get_count("debug"));
}

查看帮助

root@jiangbo12490:~/git/game/target/release# ./game -h
Usage: game [OPTIONS]

Options:
  -d, --debug...  调试信息
  -h, --help      Print help information
  -V, --version   Print version information

使用

root@jiangbo12490:~/git/game/target/release# ./game -d --debug -d
输入的参数数量是: 3

总结

统计参数出现的次数。

附录

posted @ 2025-10-22 11:07  jiangbo4444  阅读(3)  评论(0)    收藏  举报