1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::collections::HashMap;
// 系统参数
pub struct Args {
// 命令行
pub command: String,
// 二级命令
pub sub_command: String,
// 参数选项
pub option: Vec<String>,
// 请求参数
pub data: HashMap<String, Vec<String>>,
pub raw: Vec<String>,
}
impl Args {
// 获取命令行参数
pub fn get_data(&self, key: &str) -> Option<&Vec<String>> {
if !self.data.is_empty() {
if self.data.contains_key(key) {
let value = self.data.get(key);
if value != None {
let vs = value.unwrap();
return Some(vs);
}
}
}
None
}
// 实例化函数
pub fn new(args: &Vec<String>) -> Args {
// 字段信息
let mut command = String::new(); // 命令
let mut sub_command = String::new(); // 子命令
let mut option = Vec::new();
let mut data: HashMap<String, Vec<String>> = HashMap::new();
// 辅助变量
let mut count = 0;
let mut last_opt = String::new(); // 最新的opt缓存
let mut last_value: Vec<String> = Vec::new(); // 最新的value缓存
// 使用闭包函数
// let update_data_value = |v_opt: &String| {
// if !v_opt.is_empty(){
// if !data.contains_key(v_opt.as_str()){
// data.insert(v_opt, last_value);
// }
// last_value = Vec::new();
// }
// };
for arg in args {
let long_opt = arg.find("--");
let shot_opt = arg.find('-');
if count == 0 {
// 命令
if shot_opt.is_none() {
command = String::from(arg);
}
} else if sub_command.len() == 0 && shot_opt == None {
// 子命令
sub_command = String::from(arg);
} else {
if long_opt != None {
let (_, opt) = arg.split_at(2);
if !last_opt.is_empty() {
if !data.contains_key(last_opt.as_str()) {
data.insert(last_opt, last_value);
}
last_value = Vec::new();
}
last_opt = String::from(opt);
option.push(String::from(&last_opt));
} else if shot_opt != None {
let (_, opt) = arg.split_at(1);
for by in opt.chars() {
last_opt = String::from(by);
option.push(String::from(&last_opt));
}
} else {
last_value.push(String::from(arg));
}
}
count += 1;
}
Args {
command,
sub_command,
option,
data,
raw: args.to_vec(),
}
}
}