diff --git a/e2e/tests-dfx/extension.bash b/e2e/tests-dfx/extension.bash index 26fd675e9f..0d81e3e91b 100644 --- a/e2e/tests-dfx/extension.bash +++ b/e2e/tests-dfx/extension.bash @@ -155,4 +155,57 @@ EOF assert_command dfx test_extension abc --the-param 123 assert_eq "pamparam the param is 123" + assert_command dfx extension run test_extension abc --the-param 123 + assert_eq "pamparam the param is 123" +} + +@test "run with multiple values for the same parameter" { + CACHE_DIR=$(dfx cache show) + mkdir -p "$CACHE_DIR"/extensions/test_extension + + cat > "$CACHE_DIR"/extensions/test_extension/test_extension << "EOF" +#!/usr/bin/env bash + +echo $@ +EOF + + chmod +x "$CACHE_DIR"/extensions/test_extension/test_extension + + cat > "$CACHE_DIR"/extensions/test_extension/extension.json <, pub long: Option, pub short: Option, + #[serde(default)] + pub multiple: bool, } impl ExtensionSubcommandArgOpts { @@ -78,9 +80,12 @@ impl ExtensionSubcommandArgOpts { if let Some(s) = self.short { arg = arg.short(s); } + if self.multiple { + arg = arg.num_args(0..); + } Ok(arg // let's not enforce any restrictions - .allow_hyphen_values(false) + .allow_hyphen_values(true) .required(false) .action(ArgAction::Append)) } @@ -164,26 +169,65 @@ fn parse_test_file() { "long": "wasms-dir" } } + }, + "install": { + "about": "Subcommand for installing something.", + "args": { + "accounts": { + "about": "some arg that accepts multiple values separated by spaces", + "long": "accounts", + "multiple": true + } + } } } } "#; let m: Result = serde_json::from_str(f); - // dbg!(&m); + dbg!(&m); assert!(m.is_ok()); let subcmds = m.unwrap().into_clap_commands().unwrap(); dbg!(&subcmds); for s in &subcmds { - if s.get_name() == "download" { - let matches = s - .clone() - .get_matches_from(vec!["download", "--ic-commit", "value"]); - assert_eq!( - Some(&"value".to_string()), - matches.get_one::("ic_commit") - ); + match s.get_name() { + "download" => { + let matches = s + .clone() + .get_matches_from(vec!["download", "--ic-commit", "value"]); + assert_eq!( + Some(&"value".to_string()), + matches.get_one::("ic_commit") + ); + let matches = s.clone().try_get_matches_from(vec![ + "download", + "--ic-commit", + "value", + "value2", + ]); + assert!(matches.is_err()); + } + "install" => { + let matches = s.clone().get_matches_from(vec![ + "install", + "--accounts", + "value1", + "value2", + "value3", + "value4", + ]); + assert_eq!( + vec!["value1", "value2", "value3", "value4"], + matches + .get_many::("accounts") + .unwrap() + .into_iter() + .map(|x| x.as_str()) + .collect::>() + ); + } + _ => {} } }