Skip to content

Commit

Permalink
matcher for pubkey with option
Browse files Browse the repository at this point in the history
  • Loading branch information
ochaloup committed Jun 30, 2023
1 parent 83331fc commit 7789cab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
15 changes: 13 additions & 2 deletions libs/marinade-common-cli/src/config_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,19 @@ pub const PRINT_ONLY_ARG: ArgConstant<'static> = ArgConstant {
};
pub fn print_only_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name(PRINT_ONLY_ARG.name)
.takes_value(false)
.short("p")
.help(PRINT_ONLY_ARG.long)
.takes_value(false)
.help(PRINT_ONLY_ARG.help)
}

pub const PPP: ArgConstant<'static> = ArgConstant {
name: "print_only",
long: "print-only",
help: "Transactions are not executed against the cluster, only simulation is executed.",
};
pub fn ppp_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name(PPP.name)
.long(PPP.long)
.takes_value(false)
.help(PPP.help)
}
23 changes: 17 additions & 6 deletions libs/marinade-common-cli/src/matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,26 @@ pub fn signer_from_path_or_default(
}
}

/// Getting pubkey from the matched name or load it from the signer data
/// Getting pubkey from the matched name or load it from the signer data, when not provided, return an error
pub fn pubkey_or_of_signer(
matches: &ArgMatches<'_>,
name: &str,
wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
) -> anyhow::Result<Pubkey> {
if let Some(matched_value) = matches.value_of(name) {
pubkey_or_of_signer_optional(matches, name, wallet_manager)
.map(|pubkey| {
pubkey.ok_or_else(|| anyhow!("Value for argument '{}' was not provided", name))
})
.unwrap_or_else(Err)
}

/// Getting pubkey from the matched name or load it from the signer data, when not provided, option None is returned
pub fn pubkey_or_of_signer_optional(
matches: &ArgMatches<'_>,
name: &str,
wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
) -> anyhow::Result<Option<Pubkey>> {
matches.value_of(name).map_or(Ok(None), |matched_value| {
let pubkey = if let Ok(pubkey) = Pubkey::from_str(matched_value) {
pubkey
} else {
Expand All @@ -45,10 +58,8 @@ pub fn pubkey_or_of_signer(
)
})?
};
Ok(pubkey)
} else {
Err(anyhow!("Value for argument '{}' was not provided", name))
}
Ok(Some(pubkey))
})
}

/// Looking for a set of pubkeys in the matches, and return them as a vector
Expand Down

0 comments on commit 7789cab

Please sign in to comment.