Skip to content

Commit

Permalink
fix(extension-run): allow params with multiple values (#3235)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Nowak-Liebiediew authored Jul 12, 2023
1 parent 12acf5d commit da9984e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 10 deletions.
53 changes: 53 additions & 0 deletions e2e/tests-dfx/extension.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF
{
"name": "test_extension",
"version": "0.1.0",
"homepage": "https://github.com/dfinity/dfx-extensions",
"authors": "DFINITY",
"summary": "Test extension for e2e purposes.",
"categories": [],
"keywords": [],
"subcommands": {
"abc": {
"about": "something something",
"args": {
"the_param": {
"about": "this is the param",
"long": "the-param",
"multiple": true
},
"another_param": {
"about": "this is the param",
"long": "the-another-param",
"multiple": true
}
}
}
}
}
EOF

assert_command dfx test_extension abc --the-param 123 456 789 --the-another-param 464646
assert_eq "abc --the-param 123 456 789 --the-another-param 464646 --dfx-cache-path $CACHE_DIR"
assert_command dfx test_extension abc --the-another-param 464646 --the-param 123 456 789
assert_eq "abc --the-another-param 464646 --the-param 123 456 789 --dfx-cache-path $CACHE_DIR"
assert_command dfx extension run test_extension abc --the-param 123 456 789 --the-another-param 464646
assert_eq "abc --the-param 123 456 789 --the-another-param 464646 --dfx-cache-path $CACHE_DIR"
assert_command dfx extension run test_extension abc --the-another-param 464646 --the-param 123 456 789
assert_eq "abc --the-another-param 464646 --the-param 123 456 789 --dfx-cache-path $CACHE_DIR"
}
64 changes: 54 additions & 10 deletions src/dfx/src/lib/extension/manifest/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub struct ExtensionSubcommandArgOpts {
pub about: Option<String>,
pub long: Option<String>,
pub short: Option<char>,
#[serde(default)]
pub multiple: bool,
}

impl ExtensionSubcommandArgOpts {
Expand All @@ -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))
}
Expand Down Expand Up @@ -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<ExtensionManifest, serde_json::Error> = 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::<String>("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::<String>("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::<String>("accounts")
.unwrap()
.into_iter()
.map(|x| x.as_str())
.collect::<Vec<&str>>()
);
}
_ => {}
}
}

Expand Down

0 comments on commit da9984e

Please sign in to comment.