From 1a1e4f907c68a676badb6a7c53995dcbf300c037 Mon Sep 17 00:00:00 2001 From: sunface Date: Fri, 1 Apr 2022 20:51:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=AB=A0=E8=8A=82=20[Cookboo?= =?UTF-8?q?k=20-=20=E5=91=BD=E4=BB=A4=E8=A1=8C=E8=A7=A3=E6=9E=90]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SUMMARY.md | 4 ++- src/cookbook/cmd.md | 1 - src/cookbook/cmd/ansi.md | 1 + src/cookbook/cmd/parsing.md | 70 +++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) delete mode 100644 src/cookbook/cmd.md create mode 100644 src/cookbook/cmd/ansi.md create mode 100644 src/cookbook/cmd/parsing.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index ebb42d76..2424ac41 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -267,7 +267,9 @@ - [实用算法](cookbook/algos/intro.md) - [生成随机值](cookbook/algos/randomness.md) - [Vec 排序](cookbook/algos/sorting.md) - - [命令行解析 todo](cookbook/cmd.md) + - [命令行]() + - [参数解析](cookbook/cmd/parsing.md) + - [ANSI 终端](cookbook/cmd/ansi.md) - [配置文件解析 todo](cookbook/config.md) - [编解码 todo](cookbook/encoding/intro.md) - [JSON](cookbook/encoding/json.md) diff --git a/src/cookbook/cmd.md b/src/cookbook/cmd.md deleted file mode 100644 index ed9e9191..00000000 --- a/src/cookbook/cmd.md +++ /dev/null @@ -1 +0,0 @@ -# 命令行 diff --git a/src/cookbook/cmd/ansi.md b/src/cookbook/cmd/ansi.md new file mode 100644 index 00000000..a7f90759 --- /dev/null +++ b/src/cookbook/cmd/ansi.md @@ -0,0 +1 @@ +# ANSI 终端 diff --git a/src/cookbook/cmd/parsing.md b/src/cookbook/cmd/parsing.md new file mode 100644 index 00000000..b0356aef --- /dev/null +++ b/src/cookbook/cmd/parsing.md @@ -0,0 +1,70 @@ +# 参数解析 + +## Clap +下面的程序给出了使用 `clap` 来解析命令行参数的样式结构,如果大家想了解更多,在 `clap` [文档](https://docs.rs/clap/)中还给出了另外两种初始化一个应用的方式。 + +在下面的构建中,`value_of` 将获取通过 `with_name` 解析出的值。`short` 和 `long` 用于设置用户输入的长短命令格式,例如短命令 `-f` 和长命令 `--file`。 + +```rust,editable +use clap::{Arg, App}; + +fn main() { + let matches = App::new("My Test Program") + .version("0.1.0") + .author("Hackerman Jones ") + .about("Teaches argument parsing") + .arg(Arg::with_name("file") + .short("f") + .long("file") + .takes_value(true) + .help("A cool file")) + .arg(Arg::with_name("num") + .short("n") + .long("number") + .takes_value(true) + .help("Five less than your favorite number")) + .get_matches(); + + let myfile = matches.value_of("file").unwrap_or("input.txt"); + println!("The file passed is: {}", myfile); + + let num_str = matches.value_of("num"); + match num_str { + None => println!("No idea what your favorite number is."), + Some(s) => { + match s.parse::() { + Ok(n) => println!("Your favorite number must be {}.", n + 5), + Err(_) => println!("That's not a number! {}", s), + } + } + } +} +``` + +`clap` 针对上面提供的构建样式,会自动帮我们生成相应的使用方式说明。例如,上面代码生成的使用说明如下: +```shell +My Test Program 0.1.0 +Hackerman Jones +Teaches argument parsing + +USAGE: + testing [OPTIONS] + +FLAGS: + -h, --help Prints help information + -V, --version Prints version information + +OPTIONS: + -f, --file A cool file + -n, --number Five less than your favorite number +``` + +最后,再使用一些参数来运行下我们的代码: +```shell +$ cargo run -- -f myfile.txt -n 251 +The file passed is: myfile.txt +Your favorite number must be 256. +``` + +## Structopt +@todo \ No newline at end of file