diff options
author | Canop <cano.petrole@gmail.com> | 2020-03-01 10:46:03 +0100 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2020-03-01 10:46:03 +0100 |
commit | 698ee5178eaf59d7ec196e01426c1a908153f5a1 (patch) | |
tree | 8c6640f662dbc50485eef3923c04bb9b062a3fcb /src/verbs.rs | |
parent | b1d40f150d999f3cd698992d63e087750d19d4a5 (diff) |
support verb {args} made of an optional group (experimental)
Fix #210
Diffstat (limited to 'src/verbs.rs')
-rw-r--r-- | src/verbs.rs | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/src/verbs.rs b/src/verbs.rs index 7b59023..e8d6d36 100644 --- a/src/verbs.rs +++ b/src/verbs.rs @@ -64,6 +64,7 @@ pub trait VerbExecutor { fn make_invocation_args_regex(spec: &str) -> Result<Regex, ConfError> { let spec = GROUP.replace_all(spec, r"(?P<$1>.+)"); let spec = format!("^{}$", spec); + info!("spec = {:?}", &spec); Regex::new(&spec.to_string()) .or_else(|_| Err(ConfError::InvalidVerbInvocation { invocation: spec })) } @@ -184,18 +185,24 @@ impl Verb { let dir_str = if file.is_dir() { file_str } else { parent_str }; map.insert("directory".to_string(), dir_str.to_string()); // then the ones computed from the user input - if let Some(args) = args { - if let Some(r) = &self.args_parser { - if let Some(input_cap) = r.captures(&args) { - for name in r.capture_names().flatten() { - if let Some(c) = input_cap.name(name) { - map.insert(name.to_string(), c.as_str().to_string()); - } + debug!("building repmap, args_parser={:?}", &self.args_parser); + let default_args; + let args = match args { + Some(s) => s, + None => { + // empty args are useful when the args_parser contains + // an optional group + default_args = String::new(); + &default_args + } + }; + if let Some(r) = &self.args_parser { + if let Some(input_cap) = r.captures(&args) { + for name in r.capture_names().flatten() { + if let Some(c) = input_cap.name(name) { + map.insert(name.to_string(), c.as_str().to_string()); } } - } else { - warn!("invocation args given but none expected"); - // maybe tell the user? } } map |