0252-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, ArgAction};fn main() {let matches = command!().arg(Arg::new("verbose").short('v').long("verbose").action(ArgAction::SetTrue).help("调试信息"),).get_matches();println!("输入的参数是: {}", matches.get_flag("verbose"));
}

查看帮助

root@jiangbo12490:~/git/game/target/release# ./game -h
Usage: game [OPTIONS]Options:-v, --verbose  调试信息-h, --help     Print help information-V, --version  Print version information

使用

root@jiangbo12490:~/git/game/target/release# ./game
输入的参数是: false
root@jiangbo12490:~/git/game/target/release# ./game -v
输入的参数是: true

总结

使用标记类型的参数。

附录