-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
matches.rs
178 lines (167 loc) · 4.04 KB
/
matches.rs
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
178
use crate::file::File;
use clap::{ArgMatches as Args, Values};
use ini::Ini as Config;
use std::env::{self, VarError};
use std::fmt;
use std::path::PathBuf;
/* clap::ArgMatches wrapper with config file */
#[derive(Clone)]
pub struct ArgMatches<'a> {
pub args: &'a Args<'a>,
pub config: Option<Config>,
pub section: &'a str,
}
/* Debug implementation for programmer-facing output */
impl fmt::Debug for ArgMatches<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ArgMatches")
.field("args", &self.args)
.field("config", &self.config.is_some())
.field("section", &self.section)
.finish()
}
}
impl<'a> ArgMatches<'a> {
/**
* Create a new ArgMatches object.
*
* @param args
* @return ArgMatches
*/
pub fn new(args: &'a Args<'a>) -> Self {
let config = if let Some(config_file) = args
.value_of("config")
.map_or(File::get_config_file().take(), |v| Some(PathBuf::from(v)))
{
Config::load_from_file(config_file).ok()
} else {
None
};
Self {
args,
config,
section: "general",
}
}
/**
* Get argument value from an environment variable.
*
* @param name
* @return String (Result)
*/
fn get_env(&self, name: &'a str) -> Result<String, VarError> {
env::var(
format!(
"{}_{}_{}",
env!("CARGO_PKG_NAME"),
self.section,
name.replace('-', "_")
)
.to_uppercase(),
)
}
/**
* Get the value of a specific option or positional argument.
*
* @param name
* @return str (Option)
*/
pub fn value_of(&self, name: &'a str) -> Option<&str> {
self.get_env(name).map_or(
match self.config {
Some(ref config) => config
.get_from(Some(self.section), name)
.or_else(|| self.args.value_of(name)),
None => self.args.value_of(name),
},
|v| Some(Box::leak(v.into_boxed_str())),
)
}
/**
* Check the argument is present in runtime.
*
* @param name
* @return bool
*/
pub fn is_present(&self, name: &'a str) -> bool {
self.args.is_present(name)
|| if let Some(config) = &self.config {
config
.get_from(Some(self.section), name)
.map_or(false, |s| s.to_lowercase() == "true")
} else {
false
} || self
.get_env(name)
.map_or(false, |s| s.to_lowercase() == "true")
}
/**
* Get the number of times an argument was used at runtime.
*
* @param name
* @return u64
*/
pub fn occurrences_of(&self, name: &'a str) -> u64 {
self.get_env(name).map_or(
match self.config {
Some(ref config) => config
.get_from(Some(self.section), name)
.map_or(0, |s| s.parse().unwrap_or(1)),
None => self.args.occurrences_of(name),
},
|v| v.parse().unwrap_or(1),
)
}
/**
* Get the subcommand matches with names.
*
* @return tuple (str, Args)
*/
pub fn subcommand(&self) -> (&'a str, Option<&'a Args<'a>>) {
self.args.subcommand()
}
/**
* Get the subcommand matches.
*
* @param name
* @return Args (Option)
*/
pub fn subcommand_matches(&'a self, name: &'a str) -> Option<&'a Args<'a>> {
self.args.subcommand_matches(name)
}
/**
* Get the values of an argument.
*
* @param name
* @return Values (Option)
*/
pub fn values_of(&self, name: &'a str) -> Option<Values<'a>> {
self.args.values_of(name)
}
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn test_matches() {
let args = Args::default();
let matches = ArgMatches::new(&args);
let key = format!("{}_general_test", env!("CARGO_PKG_NAME")).to_uppercase();
env::set_var(&key, "...");
assert_eq!("...", matches.get_env("test").unwrap_or_default());
assert_eq!(Some("..."), matches.value_of("test"));
assert_eq!(None, matches.value_of("test_"));
env::set_var(&key, "true");
assert_eq!(true, matches.is_present("test"));
assert_eq!(false, matches.is_present("test_"));
env::set_var(&key, "5");
assert_eq!(5, matches.occurrences_of("test"));
assert_eq!(0, matches.occurrences_of("test_"));
assert_eq!(
format!("{matches:?}"),
"ArgMatches { args: ArgMatches { args: {}, \
subcommand: None, usage: None }, config: false, section: \"general\" }"
);
}
}