Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit b613066

Browse files
committed
remove potential panice from value_of and is_present
1 parent 0afb444 commit b613066

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

token/cli/src/config.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use {
2323
spl_token_client::client::{
2424
ProgramClient, ProgramOfflineClient, ProgramRpcClient, ProgramRpcClientSendTransaction,
2525
},
26-
std::{process::exit, rc::Rc, sync::Arc},
26+
std::{process::exit, rc::Rc, str::FromStr, sync::Arc},
2727
};
2828

2929
type SignersOf = Vec<(Arc<dyn Signer>, Pubkey)>;
@@ -222,11 +222,17 @@ impl<'a> Config<'a> {
222222
OutputFormat::Display
223223
});
224224

225-
let nonce_account = pubkey_of_signer(matches, NONCE_ARG.name, wallet_manager)
226-
.unwrap_or_else(|e| {
227-
eprintln!("error: {}", e);
228-
exit(1);
229-
});
225+
let nonce_account = match pubkey_of_signer(matches, NONCE_ARG.name, wallet_manager) {
226+
Ok(account) => account,
227+
Err(e) => {
228+
if e.is::<clap::parser::MatchesError>() {
229+
None
230+
} else {
231+
eprintln!("error: {}", e);
232+
exit(1);
233+
}
234+
}
235+
};
230236
let nonce_authority = if nonce_account.is_some() {
231237
let (nonce_authority, _) = signer_from_path(
232238
matches,
@@ -251,17 +257,27 @@ impl<'a> Config<'a> {
251257
None
252258
};
253259

254-
let sign_only = matches.is_present(SIGN_ONLY_ARG.name);
255-
let dump_transaction_message = matches.is_present(DUMP_TRANSACTION_MESSAGE.name);
260+
let sign_only = matches.try_contains_id(SIGN_ONLY_ARG.name).unwrap_or(false);
261+
let dump_transaction_message = matches
262+
.try_contains_id(DUMP_TRANSACTION_MESSAGE.name)
263+
.unwrap_or(false);
264+
265+
let pubkey_from_matches = |name| {
266+
matches
267+
.try_get_one::<String>(name)
268+
.ok()
269+
.flatten()
270+
.and_then(|pubkey| Pubkey::from_str(pubkey).ok())
271+
};
256272

257273
let default_program_id = spl_token::id();
258274
let (program_id, restrict_to_program_id) =
259-
if let Some(program_id) = value_of(matches, "program_id") {
275+
if let Some(program_id) = pubkey_from_matches("program_id") {
260276
(program_id, true)
261277
} else if !sign_only {
262-
if let Some(address) = value_of(matches, "token")
263-
.or_else(|| value_of(matches, "account"))
264-
.or_else(|| value_of(matches, "address"))
278+
if let Some(address) = pubkey_from_matches("token")
279+
.or_else(|| pubkey_from_matches("account"))
280+
.or_else(|| pubkey_from_matches("address"))
265281
{
266282
(
267283
rpc_client
@@ -278,7 +294,12 @@ impl<'a> Config<'a> {
278294
(default_program_id, false)
279295
};
280296

281-
let nonce_blockhash = value_of(matches, BLOCKHASH_ARG.name);
297+
let nonce_blockhash = matches
298+
.try_get_one::<Hash>(BLOCKHASH_ARG.name)
299+
.ok()
300+
.flatten()
301+
.copied();
302+
282303
Self {
283304
default_signer,
284305
rpc_client,

0 commit comments

Comments
 (0)