2 个版本
0.1.3 | 2022 年 5 月 2 日 |
---|---|
0.1.2 | 2022 年 5 月 1 日 |
0.1.1 |
|
0.1.0 |
|
#698 in 命令行界面
64KB
404 行
CLIA
发音为 KLEE-uh
Rust 命令行参数解析器,无额外依赖。
这是一个用于解析命令行参数的工具 crate。
功能
- 根据可用选项生成帮助消息,
- 支持所有后续指定的参数类型
用法
支持的参数类型
就这个 crate 而言,有 4 种类型的参数,分为 2 个主要组
选项
- 标志(例如
-r
) - 带有列表的标志(例如
-f [逗号分隔列表]
) - 带有数据的标志(例如
--format <NUMERIC>
)
和参数
- (例如文件路径、字符串等。)
此 crate 对您的命令行程序有以下假设
- 所有选项/标志都以
-
开头 - 命令行中输入的列表是逗号分隔的
- 选项及其关联数据在任何参数之前类型化(程序的用法遵循以下模式:
foo.exe [OPTIONS]... [PARAMETERS]
) - 所有“参数”都是必需的,并且必须包含在程序的参数中才能正常工作(可选参数应与标志绑定)
安装
将 clia=0.1.3
添加到项目的 cargo.toml 的 [dependencies] 中,如下所示
cargo.toml
[dependencies]
clia="0.1.3"
示例
以下是一个展示 CLIO 所有基本功能的示例。
use std::env;
use clia::{option_args::{ClOption,ClOptionInfo},parameter_args::ClParameter,Parser};
/// this is just an example of using this crate
fn main() {
/* step 1: define options and parameters */
let mut valid_options: Vec<ClOption> = Vec::new();
let mut expected_parameters: Vec<ClParameter> = Vec::new();
// define options
// this is an example of making an option with a list
valid_options.push( ClOption::new_flag_list(
&ClOptionInfo::new(
"-f",
"--filter",
"Comma separated list of extensions, will only count lines of files with these extensions"
).unwrap(),
"EXTENSIONS"
));
// this is an example of making an option with some data
valid_options.push( ClOption::new_flag_data(
&ClOptionInfo::new(
"-F",
"--format",
"Format the output in a list, valid formats are: DEFAULT, BULLET, MARKDOWN, and NUMERIC"
).unwrap(),
"FORMAT"
));
// this is an example of making a simple option
valid_options.push( ClOption::new_flag(
&ClOptionInfo::new(
"-r",
"--recursive",
"Search through subdirectories"
).unwrap()
));
// this is an example of making a simple option
valid_options.push( ClOption::new_flag(
&ClOptionInfo::new(
"-h",
"--help",
"Prints help information"
).unwrap()
));
// define parameters
expected_parameters.push( ClParameter::new(
"PATH",
"Path to file/folder to search"
));
expected_parameters.push( ClParameter::new(
"QUERY",
"String to search for, all the stuff after the path wrap in \"'s if it contains spaces"
));
/* step 2: collect CLI Arguments and call the parser */
let args: Vec<String> = env::args().collect(); //read the argument values from env, and collect them into a string vector
//call parser
let arg_parser;
match Parser::new(&args, &valid_options, &expected_parameters) {
Ok(arg_par) => arg_parser = arg_par,
Err(e) => {println!("{}", Parser::help("foo.exe", "by Anthony Rubick", "Just here as an example of things you can do", &valid_options, &expected_parameters)); panic!("{}", e);}, //print any errors that occur
}
/* step 3: access the "found" fields of the parser */
//store data from the parser
let found_options = arg_parser.get_option_arguments_found();
let found_parameters = arg_parser.get_parameter_arguments_found();
/* process the arguments */
//user passed the -h flag
if found_options.iter().any(|opt| opt.get_info().get_short_flag().eq("-h")) {
println!("{}", Parser::help("foo.exe", "by Anthony Rubick", "Just here as an example of things you can do", &valid_options, &expected_parameters));
}
// ...
}
-h 的输出
foo.exe
by Anthony Rubick
Just here as an example of things you can do
USAGE: foo.exe [OPTIONS]... [PATH] [QUERY]
OPTIONS:
-f, --filter <EXTENSIONS>... Comma separated list of extensions, will only count lines of files with these extensions
-F, --format <FORMAT> Format the output in a list, valid formats are: DEFAULT, BULLET, MARKDOWN, and NUMERIC
-r, --recursive Search through subdirectories
-h, --help Prints help information
PARAMETER ARGUMENTS:
PATH:
Path to file/folder to search
QUERY:
String to search for, all the stuff after the path wrap in "'s if it contains spaces