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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::text::T;
use std::collections::HashMap;
use std::str::FromStr;
/// Stores the parsed command line
pub struct CommandLine {
/// Commandline argument 0 the program name
program_name: String,
/// The options and values parsed from the command line
options: HashMap<String, String>,
/// The remaining non-option arguments
argument_map: HashMap<String, String>,
}
impl CommandLine {
/// Creates a new CommandLine from the args and the OptionDefs
///
/// # Arguments
///
/// * `program_name` - The program name used on the commandline
/// * `options` - A hashmap of options and their values specified on the commandline
/// * `argument_map` - A HashMap of arguments and their values specified on the commandline
///
/// # Examples
///
/// ```
/// use std::collections::VecDeque;
/// use std::env;
/// use cl_parse::{CommandLine, CommandLineDef};
/// // Simulate env::args()
/// let env_args=Vec::new();
/// let cl = CommandLineDef::new().parse(env_args.into_iter());
/// // Test Program Name
/// assert_eq!(true, cl.program_name().is_empty());
/// ```
pub(crate) fn new(
program_name: String,
options: HashMap<String, String>,
argument_map: HashMap<String, String>,
) -> Self {
CommandLine {
program_name,
options,
argument_map,
}
}
/// Returns the number of options parsed
///
/// # Examples
///
/// ```
/// use std::collections::VecDeque;
/// use std::env;
/// use cl_parse::{CommandLine, CommandLineDef};
/// // Simulate env::args()
/// let env_args=vec![String::from("program"), String::from("-f"), String::from("/file/path"), String::from("arg1")];
/// let cl = CommandLineDef::new()
/// .add_option(vec!["-f","--filename"], Some("filepath"), None, "The file to be parsed")
/// .add_flag(vec!["-b"], "A binary flag option")
/// .add_argument("arg-0")
/// .parse(env_args.into_iter());
///
/// // Test Program Name
/// assert_eq!(cl.options(), 5); // -f, --filename, -b, -h, --help
/// ```
pub fn options(self) -> usize {
self.options.len()
}
/// Returns the option for the option key specified
///
/// # Arguments
///
/// * `name` - A string slice that holds the name of the option
///
/// # Examples
///
/// ```
/// use std::collections::VecDeque;
/// use std::env;
/// use cl_parse::{CommandLine, CommandLineDef};
/// // Simulate env::args()
/// let env_args=vec![String::from("program"), String::from("-f"), String::from("/file/path")];
/// let cl = CommandLineDef::new().add_option(vec!["-f","--filename"], Some("filepath"),
/// None, "The file to be parsed").parse(env_args.into_iter());
/// let filename:String = cl.option("-f");
/// // Test Program Name
/// assert_eq!(filename, "/file/path".to_string());
/// ```
pub fn option<T>(&self, name: &str) -> T
where
T: FromStr,
{
let option = self
.options
.get(name)
.unwrap_or_else(|| panic!("{}", &T.option_not_found(name)));
match T::from_str(option) {
Ok(t) => t,
Err(_) => panic!("{}", T.option_cannot_convert(name, option, std::any::type_name::<T>())),
}
}
/// Returns the number of arguments parsed
///
/// # Examples
///
/// ```
/// use std::collections::VecDeque;
/// use std::env;
/// use cl_parse::{CommandLine, CommandLineDef};
/// // Simulate env::args()
/// let env_args=vec![String::from("program"), String::from("-f"), String::from("/file/path")];
/// let cl = CommandLineDef::new().add_option(vec!["-f","--filename"], Some("filepath"),
/// None, "The file to be parsed").parse(env_args.into_iter());
///
/// // Test Program Name
/// assert_eq!(cl.arguments(), 0);
/// ```
pub fn arguments(&self) -> usize {
self.argument_map.len()
}
/// Returns the argument based by index
///
/// # Examples
///
/// ```
/// use std::collections::VecDeque;
/// use std::env;
/// use cl_parse::{CommandLine, CommandLineDef};
/// // Simulate env::args()
/// let env_args=vec![String::from("program"), String::from("-f"), String::from("/file/path"), String::from("arg1_value")];
/// let cl = CommandLineDef::new()
/// .add_option(vec!["-f","--filename"], Some("filepath"), None, "The file to be parsed")
/// .add_argument("arg1_name")
/// .parse(env_args.into_iter());
///
/// let arg1:String = cl.argument("arg1_name");
/// assert_eq!(arg1, "arg1_value");
///
/// ```
pub fn argument<T>(&self, name : &str) -> T
where
T: FromStr,
{
let argument = self
.argument_map
.get(name)
.unwrap_or_else(|| panic!("{}", &T.argument_invalid_name(name)));
match T::from_str(argument) {
Ok(t) => t,
Err(_) => panic!("{}", T.argument_cannot_convert(name, argument, std::any::type_name::<T>())),
}
}
/// Returns the program name specified on the command line
///
/// # Examples
///
/// ```
/// use std::collections::VecDeque;
/// use std::env;
/// use cl_parse::{CommandLine, CommandLineDef};
/// // Simulate env::args()
/// let env_args=vec![String::from("program"), String::from("-f"), String::from("/file/path")];
/// let cl = CommandLineDef::new().add_option(vec!["-f","--filename"], Some("filepath"),
/// None, "The file to be parsed").parse(env_args.into_iter());
///
/// // Test Program Name
/// assert_eq!(false, cl.program_name().is_empty());
/// ```
pub fn program_name(&self) -> &str {
&self.program_name
}
}