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

Commit 414650d

Browse files
committed
remove potential panice from value_of and is_present
1 parent 8dab624 commit 414650d

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

token/cli/src/config.rs

Lines changed: 34 additions & 12 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)>;
@@ -224,11 +224,17 @@ impl<'a> Config<'a> {
224224
OutputFormat::Display
225225
});
226226

227-
let nonce_account = pubkey_of_signer(matches, NONCE_ARG.name, wallet_manager)
228-
.unwrap_or_else(|e| {
229-
eprintln!("error: {}", e);
230-
exit(1);
231-
});
227+
let nonce_account = match pubkey_of_signer(matches, NONCE_ARG.name, wallet_manager) {
228+
Ok(account) => account,
229+
Err(e) => {
230+
if e.is::<clap::parser::MatchesError>() {
231+
None
232+
} else {
233+
eprintln!("error: {}", e);
234+
exit(1);
235+
}
236+
}
237+
};
232238
let nonce_authority = if nonce_account.is_some() {
233239
let (nonce_authority, _) = signer_from_path(
234240
matches,
@@ -253,17 +259,27 @@ impl<'a> Config<'a> {
253259
None
254260
};
255261

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

259275
let default_program_id = spl_token::id();
260276
let (program_id, restrict_to_program_id) =
261-
if let Some(program_id) = value_of(matches, "program_id") {
277+
if let Some(program_id) = pubkey_from_matches("program_id") {
262278
(program_id, true)
263279
} else if !sign_only {
264-
if let Some(address) = value_of(matches, "token")
265-
.or_else(|| value_of(matches, "account"))
266-
.or_else(|| value_of(matches, "address"))
280+
if let Some(address) = pubkey_from_matches("token")
281+
.or_else(|| pubkey_from_matches("account"))
282+
.or_else(|| pubkey_from_matches("address"))
267283
{
268284
(
269285
rpc_client
@@ -283,6 +299,12 @@ impl<'a> Config<'a> {
283299
let nonce_blockhash = value_of(matches, BLOCKHASH_ARG.name);
284300
let compute_unit_price = value_of(matches, COMPUTE_UNIT_PRICE_ARG.name);
285301
let compute_unit_limit = value_of(matches, COMPUTE_UNIT_LIMIT_ARG.name);
302+
let nonce_blockhash = matches
303+
.try_get_one::<Hash>(BLOCKHASH_ARG.name)
304+
.ok()
305+
.flatten()
306+
.copied();
307+
286308
Self {
287309
default_signer,
288310
rpc_client,

0 commit comments

Comments
 (0)