Skip to content

Commit 5230aa8

Browse files
committed
Named value arguments now work.
1 parent 873dcb5 commit 5230aa8

File tree

2 files changed

+85
-9
lines changed

2 files changed

+85
-9
lines changed

examples/simple.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ const FOO_ITEM: Item<Output> = Item {
1111
Parameter::Mandatory("a"),
1212
Parameter::Optional("b"),
1313
Parameter::Named("verbose"),
14-
// Parameter::NamedValue {
15-
// parameter_name: "level",
16-
// argument_name: "INT",
17-
// },
14+
Parameter::NamedValue {
15+
parameter_name: "level",
16+
argument_name: "INT",
17+
},
1818
],
1919
},
2020
command: "foo",
@@ -113,7 +113,7 @@ fn exit_root(_menu: &Menu<Output>, context: &mut Output) {
113113
writeln!(context, "In exit_root").unwrap();
114114
}
115115

116-
fn select_foo<'a>(menu: &Menu<Output>, item: &Item<Output>, args: &[&str], context: &mut Output) {
116+
fn select_foo<'a>(_menu: &Menu<Output>, item: &Item<Output>, args: &[&str], context: &mut Output) {
117117
writeln!(context, "In select_foo. Args = {:?}", args).unwrap();
118118
writeln!(
119119
context,

src/lib.rs

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ pub fn argument_finder<'a, T>(
106106
found_param = Some((param, 0));
107107
}
108108
}
109-
_ => {
110-
unimplemented!();
109+
Parameter::NamedValue { parameter_name, .. } => {
110+
if *parameter_name == name_to_find {
111+
found_param = Some((param, 0));
112+
}
111113
}
112114
}
113115
}
@@ -145,7 +147,7 @@ pub fn argument_finder<'a, T>(
145147
// Valid thing to ask for but we don't have it
146148
Ok(None)
147149
}
148-
// Step 2c - Named
150+
// Step 2c - Named (e.g. `--verbose`)
149151
Some((Parameter::Named(name), _)) => {
150152
for arg in argument_list {
151153
if arg.starts_with("--") && (&arg[2..] == *name) {
@@ -155,7 +157,23 @@ pub fn argument_finder<'a, T>(
155157
// Valid thing to ask for but we don't have it
156158
Ok(None)
157159
}
158-
// Step 2d - NamedValue
160+
// Step 2d - NamedValue (e.g. `--level=123`)
161+
Some((Parameter::NamedValue { parameter_name, .. }, _)) => {
162+
let name_start = 2;
163+
let equals_start = name_start + parameter_name.len();
164+
let value_start = equals_start + 1;
165+
for arg in argument_list {
166+
if arg.starts_with("--")
167+
&& (arg.len() >= value_start)
168+
&& (arg.get(equals_start..=equals_start) == Some("="))
169+
&& (arg.get(name_start..equals_start) == Some(*parameter_name))
170+
{
171+
return Ok(Some(&arg[value_start..]));
172+
}
173+
}
174+
// Valid thing to ask for but we don't have it
175+
Ok(None)
176+
}
159177
// Step 2e - not found
160178
_ => Err(()),
161179
}
@@ -562,4 +580,62 @@ mod tests {
562580
// Missing named
563581
assert_eq!(argument_finder(&item, &["a"], "baz"), Ok(None));
564582
}
583+
584+
#[test]
585+
fn find_arg_namedvalue() {
586+
let item = Item {
587+
command: "dummy",
588+
help: None,
589+
item_type: ItemType::Callback {
590+
function: dummy,
591+
parameters: &[
592+
Parameter::Mandatory("foo"),
593+
Parameter::Named("bar"),
594+
Parameter::NamedValue {
595+
parameter_name: "baz",
596+
argument_name: "BAZ",
597+
},
598+
],
599+
},
600+
};
601+
assert_eq!(
602+
argument_finder(&item, &["a", "--bar", "--baz"], "foo"),
603+
Ok(Some("a"))
604+
);
605+
assert_eq!(
606+
argument_finder(&item, &["a", "--bar", "--baz"], "bar"),
607+
Ok(Some(""))
608+
);
609+
// No argument so mark as not found
610+
assert_eq!(
611+
argument_finder(&item, &["a", "--bar", "--baz"], "baz"),
612+
Ok(None)
613+
);
614+
// Empty argument
615+
assert_eq!(
616+
argument_finder(&item, &["a", "--bar", "--baz="], "baz"),
617+
Ok(Some(""))
618+
);
619+
// Short argument
620+
assert_eq!(
621+
argument_finder(&item, &["a", "--bar", "--baz=1"], "baz"),
622+
Ok(Some("1"))
623+
);
624+
// Long argument
625+
assert_eq!(
626+
argument_finder(
627+
&item,
628+
&["a", "--bar", "--baz=abcdefghijklmnopqrstuvwxyz"],
629+
"baz"
630+
),
631+
Ok(Some("abcdefghijklmnopqrstuvwxyz"))
632+
);
633+
// Not an argument
634+
assert_eq!(
635+
argument_finder(&item, &["a", "--bar", "--baz"], "quux"),
636+
Err(())
637+
);
638+
// Missing named
639+
assert_eq!(argument_finder(&item, &["a"], "baz"), Ok(None));
640+
}
565641
}

0 commit comments

Comments
 (0)