| 1 |
use crate::{ |
| 2 |
Result, |
| 3 |
argparse::Error, |
| 4 |
}; |
| 5 |
|
| 6 |
pub struct Opt<T> { |
| 7 |
pub name: &'static str, |
| 8 |
pub factory: fn(&mut T, bool, Option<&str>) -> Result<()>, |
| 9 |
pub value: bool, |
| 10 |
pub help: &'static str, |
| 11 |
pub enabled: bool, |
| 12 |
} |
| 13 |
|
| 14 |
/// Option taking no value. |
| 15 |
#[allow(unused_macros)] |
| 16 |
macro_rules! opt { |
| 17 |
{ $name: expr, $factory: expr, $help: expr, } => { |
| 18 |
Opt { |
| 19 |
name: $name, |
| 20 |
factory: $factory, |
| 21 |
value: false, |
| 22 |
help: $help, |
| 23 |
enabled: true, |
| 24 |
} |
| 25 |
}; |
| 26 |
} |
| 27 |
|
| 28 |
/// Option taking an argument. |
| 29 |
#[allow(unused_macros)] |
| 30 |
macro_rules! opt_with_arg { |
| 31 |
{ $name: expr, $factory: expr, $help: expr, } => { |
| 32 |
Opt { |
| 33 |
name: $name, |
| 34 |
factory: $factory, |
| 35 |
value: true, |
| 36 |
help: $help, |
| 37 |
enabled: true, |
| 38 |
} |
| 39 |
}; |
| 40 |
} |
| 41 |
|
| 42 |
/// Option that does nothing. |
| 43 |
/// |
| 44 |
/// These will be hidden in help listings, and do nothing. |
| 45 |
#[allow(unused_macros)] |
| 46 |
macro_rules! opt_nop { |
| 47 |
( $name: expr ) => { |
| 48 |
opt_nop! { $name, |_, _, _| Ok(()), "", } |
| 49 |
}; |
| 50 |
|
| 51 |
{ $name: expr, $factory: expr, $help: expr, } => { |
| 52 |
Opt { |
| 53 |
name: $name, |
| 54 |
factory: |_, _, _| Ok(()), |
| 55 |
value: false, |
| 56 |
help: "", |
| 57 |
enabled: true, |
| 58 |
} |
| 59 |
}; |
| 60 |
} |
| 61 |
|
| 62 |
/// Option that is not yet implemented. |
| 63 |
/// |
| 64 |
/// These will be hidden in help listings, and will not be matched |
| 65 |
/// when parsing arguments. |
| 66 |
#[allow(unused_macros)] |
| 67 |
macro_rules! opt_todo { |
| 68 |
{ $name: expr, $factory: expr, $help: expr, } => { |
| 69 |
Opt { |
| 70 |
name: $name, |
| 71 |
factory: |_, _, _| Ok(()), |
| 72 |
value: false, |
| 73 |
help: "", |
| 74 |
enabled: false, |
| 75 |
} |
| 76 |
}; |
| 77 |
} |
| 78 |
|
| 79 |
/// Prints the list of import options if requested. |
| 80 |
/// |
| 81 |
/// If `s == "help"`, prints all supported options and returns `true`. |
| 82 |
/// The caller should then exit the process gracefully. |
| 83 |
pub fn maybe_print_help<T>(opts: &[Opt<T>], s: &str) -> Result<bool> { |
| 84 |
if s != "help" { |
| 85 |
return Ok(false); |
| 86 |
} |
| 87 |
|
| 88 |
let width = opts.iter() |
| 89 |
.filter(|o| o.enabled && ! o.help.is_empty()) |
| 90 |
.map(|o| o.name.len()).max().unwrap_or(0); |
| 91 |
for opt in opts.iter().filter(|o| ! o.help.is_empty()) { |
| 92 |
safe_eprintln!("{:<width$} {}", opt.name, opt.help); |
| 93 |
} |
| 94 |
|
| 95 |
Ok(true) |
| 96 |
} |
| 97 |
|
| 98 |
/// Parses the options `s` described by `opts`, mutating `o`. |
| 99 |
pub fn parse<T>(opts: &[Opt<T>], s: &str, o: &mut T) -> Result<()> { |
| 100 |
let mut rest = Some(s); |
| 101 |
'parsing: while let Some(v) = rest { |
| 102 |
let (token, r) = optsep(v); |
| 103 |
rest = r; |
| 104 |
|
| 105 |
let (original_key, value) = argsplit(token); |
| 106 |
|
| 107 |
let reversed = original_key.starts_with("no-"); |
| 108 |
let key = if reversed { |
| 109 |
&original_key[3..] |
| 110 |
} else { |
| 111 |
&original_key[..] |
| 112 |
}; |
| 113 |
|
| 114 |
for (i, opt) in opts.iter().filter(|o| o.enabled).enumerate() { |
| 115 |
if opt.name.starts_with(key) { |
| 116 |
if opt.name != key { |
| 117 |
if let Some(other) = opts[i + 1..].iter() |
| 118 |
.filter(|o| o.enabled) |
| 119 |
.filter(|o| o.name != opt.name) |
| 120 |
.find(|o| o.name.starts_with(key)) |
| 121 |
{ |
| 122 |
return Err(Error::Ambiguous( |
| 123 |
key.into(), other.name.into()).into()); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
(opt.factory)(o, ! reversed, value)?; |
| 128 |
continue 'parsing; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return Err(Error::Unknown(original_key.into()).into()); |
| 133 |
} |
| 134 |
|
| 135 |
Ok(()) |
| 136 |
} |
| 137 |
|
| 138 |
/// Break a string into successive option pieces. Accepts single word |
| 139 |
/// options and key=value argument options. |
| 140 |
fn optsep(s: &str) -> (&str, Option<&str>) { |
| 141 |
assert!(! s.is_empty()); |
| 142 |
|
| 143 |
if let Some(mut end) = s.find(&[' ', ',', '=']) { |
| 144 |
// what we need to do now is scan along starting with *end, If |
| 145 |
// the next character we see (ignoring spaces) is an = sign, |
| 146 |
// then there is an argument. |
| 147 |
let argument = |
| 148 |
if let Some(equals) = s[end..].find('=') { |
| 149 |
if s[end..end + equals].chars().all(|c| c == ' ') { |
| 150 |
Some(&s[end + equals + 1..]) |
| 151 |
} else { |
| 152 |
None |
| 153 |
} |
| 154 |
} else { |
| 155 |
None |
| 156 |
}; |
| 157 |
|
| 158 |
let mut rest = &s[end + 1..]; |
| 159 |
if let Some(arg) = argument { |
| 160 |
// There is an argument, so grab that too. At this point, |
| 161 |
// ptr points to the first character of the argument. |
| 162 |
if arg.starts_with('"') { |
| 163 |
if let Some(closing) = arg[1..].find('"') { |
| 164 |
rest = &rest[1 + closing + 1..]; |
| 165 |
end += 1 + closing + 1 + 1; |
| 166 |
} else { |
| 167 |
// GnuPG ignores missing closing quotes, so we do |
| 168 |
// too. |
| 169 |
rest = ""; |
| 170 |
end += arg.len() + 1; |
| 171 |
} |
| 172 |
} else { |
| 173 |
if let Some(eoa) = arg.find(&[' ', ',']) { |
| 174 |
rest = &rest[eoa + 1..]; |
| 175 |
end += eoa + 1; |
| 176 |
} else { |
| 177 |
rest = ""; |
| 178 |
end += arg.len() + 1; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
if rest.starts_with(",") { |
| 183 |
rest = &rest[1..]; |
| 184 |
} |
| 185 |
let rest = rest.trim_start(); |
| 186 |
(&s[..end], if rest.is_empty() { None } else { Some(rest) }) |
| 187 |
} else { |
| 188 |
(s, None) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/// Breaks an option into key and value. |
| 193 |
fn argsplit(s: &str) -> (&str, Option<&str>) { |
| 194 |
let mut split = s.splitn(2, "="); |
| 195 |
let mut key = split.next().unwrap(); |
| 196 |
let value = if let Some(mut value) = split.next() { |
| 197 |
if value.starts_with("\"") { |
| 198 |
value = &value[1..]; |
| 199 |
if value.ends_with("\"") { |
| 200 |
value = &value[..value.len() - 1]; |
| 201 |
} |
| 202 |
} else { |
| 203 |
value = value.trim_start(); |
| 204 |
} |
| 205 |
|
| 206 |
key = key.trim_end(); |
| 207 |
Some(value) |
| 208 |
} else { |
| 209 |
None |
| 210 |
}; |
| 211 |
|
| 212 |
(key, value) |
| 213 |
} |
| 214 |
|
| 215 |
#[cfg(test)] |
| 216 |
mod tests { |
| 217 |
use super::*; |
| 218 |
|
| 219 |
#[test] |
| 220 |
fn import_options() { |
| 221 |
#[derive(Default)] |
| 222 |
struct ImportOptions { |
| 223 |
import_show: bool, |
| 224 |
http_proxy: Option<String>, |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
let import_opts: [Opt<ImportOptions>; 2] = [ |
| 229 |
opt! { |
| 230 |
"import-show", |
| 231 |
|o, selected, _| { |
| 232 |
o.import_show = selected; |
| 233 |
Ok(()) |
| 234 |
}, |
| 235 |
"show key during import", |
| 236 |
}, |
| 237 |
opt_with_arg! { |
| 238 |
"http-proxy", |
| 239 |
|o, selected, v| { |
| 240 |
o.http_proxy = if selected { |
| 241 |
v.map(Into::into) |
| 242 |
} else { |
| 243 |
None |
| 244 |
}; |
| 245 |
Ok(()) |
| 246 |
}, |
| 247 |
"show key during import", |
| 248 |
}, |
| 249 |
]; |
| 250 |
|
| 251 |
let mut i = ImportOptions::default(); |
| 252 |
parse(&import_opts, "import-show", &mut i).unwrap(); |
| 253 |
assert_eq!(i.import_show, true); |
| 254 |
assert_eq!(i.http_proxy, None); |
| 255 |
|
| 256 |
let mut i = ImportOptions::default(); |
| 257 |
i.import_show = true; |
| 258 |
parse(&import_opts, "no-import-show", &mut i).unwrap(); |
| 259 |
assert_eq!(i.import_show, false); |
| 260 |
assert_eq!(i.http_proxy, None); |
| 261 |
|
| 262 |
let mut i = ImportOptions::default(); |
| 263 |
parse(&import_opts, "http-proxy=http://localhost:8080", &mut i).unwrap(); |
| 264 |
assert_eq!(i.import_show, false); |
| 265 |
assert_eq!(i.http_proxy, Some("http://localhost:8080".into())); |
| 266 |
|
| 267 |
let mut i = ImportOptions::default(); |
| 268 |
parse(&import_opts, "http-proxy=\"http://localhost:8080\"", &mut i).unwrap(); |
| 269 |
assert_eq!(i.import_show, false); |
| 270 |
assert_eq!(i.http_proxy, Some("http://localhost:8080".into())); |
| 271 |
|
| 272 |
let mut i = ImportOptions::default(); |
| 273 |
parse(&import_opts, "import-show,http-proxy=http://localhost:8080", &mut i).unwrap(); |
| 274 |
assert_eq!(i.import_show, true); |
| 275 |
assert_eq!(i.http_proxy, Some("http://localhost:8080".into())); |
| 276 |
|
| 277 |
let mut i = ImportOptions::default(); |
| 278 |
parse(&import_opts, "import-sho", &mut i).unwrap(); |
| 279 |
assert_eq!(i.import_show, true); |
| 280 |
assert_eq!(i.http_proxy, None); |
| 281 |
|
| 282 |
let mut i = ImportOptions::default(); |
| 283 |
assert!(parse(&import_opts, "import-short", &mut i).is_err()); |
| 284 |
} |
| 285 |
|
| 286 |
#[test] |
| 287 |
fn tokenizing() { |
| 288 |
fn t(mut s: &str, t: &[&str]) { |
| 289 |
let mut tokens = Vec::new(); |
| 290 |
|
| 291 |
loop { |
| 292 |
let (arg, rest) = optsep(s); |
| 293 |
tokens.push(arg); |
| 294 |
if let Some(r) = rest { |
| 295 |
s = r; |
| 296 |
} else { |
| 297 |
break; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
assert_eq!(&tokens, t); |
| 302 |
} |
| 303 |
|
| 304 |
t("foo", &["foo"]); |
| 305 |
t("foo=arg", &["foo=arg"]); |
| 306 |
t("foo=\"arg\"", &["foo=\"arg\""]); |
| 307 |
t("foo,bar", &["foo", "bar"]); |
| 308 |
t("foo, bar", &["foo", "bar"]); |
| 309 |
t("foo bar", &["foo", "bar"]); |
| 310 |
t("foo=arg,bar", &["foo=arg", "bar"]); |
| 311 |
t("foo=arg, bar", &["foo=arg", "bar"]); |
| 312 |
t("foo=arg bar", &["foo=arg", "bar"]); |
| 313 |
t("foo,bar=arg", &["foo", "bar=arg"]); |
| 314 |
t("foo, bar=arg", &["foo", "bar=arg"]); |
| 315 |
t("foo bar=arg", &["foo", "bar=arg"]); |
| 316 |
t("foo=\"arg\",bar", &["foo=\"arg\"", "bar"]); |
| 317 |
t("foo=\"arg\", bar", &["foo=\"arg\"", "bar"]); |
| 318 |
t("foo=\"arg\" bar", &["foo=\"arg\"", "bar"]); |
| 319 |
t("foo,bar=\"arg\"", &["foo", "bar=\"arg\""]); |
| 320 |
t("foo, bar=\"arg\"", &["foo", "bar=\"arg\""]); |
| 321 |
t("foo bar=\"arg\"", &["foo", "bar=\"arg\""]); |
| 322 |
t("foo,bar=\"arg", &["foo", "bar=\"arg"]); |
| 323 |
t("foo, bar=\"arg", &["foo", "bar=\"arg"]); |
| 324 |
t("foo bar=\"arg", &["foo", "bar=\"arg"]); |
| 325 |
} |
| 326 |
|
| 327 |
#[test] |
| 328 |
fn splitting() { |
| 329 |
assert_eq!(argsplit("foo"), ("foo", None)); |
| 330 |
assert_eq!(argsplit("foo=bar"), ("foo", Some("bar"))); |
| 331 |
assert_eq!(argsplit("foo=\"bar\""), ("foo", Some("bar"))); |
| 332 |
assert_eq!(argsplit("foo=\"bar"), ("foo", Some("bar"))); |
| 333 |
assert_eq!(argsplit("foo =bar"), ("foo", Some("bar"))); |
| 334 |
assert_eq!(argsplit("foo =\"bar\""), ("foo", Some("bar"))); |
| 335 |
assert_eq!(argsplit("foo =\"bar"), ("foo", Some("bar"))); |
| 336 |
assert_eq!(argsplit("foo= bar"), ("foo", Some("bar"))); |
| 337 |
assert_eq!(argsplit("foo=\" bar\""), ("foo", Some(" bar"))); |
| 338 |
assert_eq!(argsplit("foo=\" bar"), ("foo", Some(" bar"))); |
| 339 |
} |
| 340 |
} |