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

Commit 73b4caf

Browse files
committed
remove potential panice from value_of and is_present
1 parent 6ded606 commit 73b4caf

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

token/cli/src/config.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use {
2626
},
2727
token::ComputeUnitLimit,
2828
},
29-
std::{process::exit, rc::Rc, sync::Arc},
29+
std::{process::exit, rc::Rc, str::FromStr, sync::Arc},
3030
};
3131

3232
type SignersOf = Vec<(Arc<dyn Signer>, Pubkey)>;
@@ -227,11 +227,17 @@ impl<'a> Config<'a> {
227227
OutputFormat::Display
228228
});
229229

230-
let nonce_account = pubkey_of_signer(matches, NONCE_ARG.name, wallet_manager)
231-
.unwrap_or_else(|e| {
232-
eprintln!("error: {}", e);
233-
exit(1);
234-
});
230+
let nonce_account = match pubkey_of_signer(matches, NONCE_ARG.name, wallet_manager) {
231+
Ok(account) => account,
232+
Err(e) => {
233+
if e.is::<clap::parser::MatchesError>() {
234+
None
235+
} else {
236+
eprintln!("error: {}", e);
237+
exit(1);
238+
}
239+
}
240+
};
235241
let nonce_authority = if nonce_account.is_some() {
236242
let (nonce_authority, _) = signer_from_path(
237243
matches,
@@ -256,17 +262,27 @@ impl<'a> Config<'a> {
256262
None
257263
};
258264

259-
let sign_only = matches.is_present(SIGN_ONLY_ARG.name);
260-
let dump_transaction_message = matches.is_present(DUMP_TRANSACTION_MESSAGE.name);
265+
let sign_only = matches.try_contains_id(SIGN_ONLY_ARG.name).unwrap_or(false);
266+
let dump_transaction_message = matches
267+
.try_contains_id(DUMP_TRANSACTION_MESSAGE.name)
268+
.unwrap_or(false);
269+
270+
let pubkey_from_matches = |name| {
271+
matches
272+
.try_get_one::<String>(name)
273+
.ok()
274+
.flatten()
275+
.and_then(|pubkey| Pubkey::from_str(pubkey).ok())
276+
};
261277

262278
let default_program_id = spl_token::id();
263279
let (program_id, restrict_to_program_id) =
264-
if let Some(program_id) = value_of(matches, "program_id") {
280+
if let Some(program_id) = pubkey_from_matches("program_id") {
265281
(program_id, true)
266282
} else if !sign_only {
267-
if let Some(address) = value_of(matches, "token")
268-
.or_else(|| value_of(matches, "account"))
269-
.or_else(|| value_of(matches, "address"))
283+
if let Some(address) = pubkey_from_matches("token")
284+
.or_else(|| pubkey_from_matches("account"))
285+
.or_else(|| pubkey_from_matches("address"))
270286
{
271287
(
272288
rpc_client
@@ -289,7 +305,7 @@ impl<'a> Config<'a> {
289305
&& !matches.is_present(COMPUTE_UNIT_LIMIT_ARG.name)
290306
{
291307
clap::Error::with_description(
292-
&format!(
308+
format!(
293309
"Need to set `{}` if `{}` and `--{}` are set",
294310
COMPUTE_UNIT_LIMIT_ARG.long, COMPUTE_UNIT_PRICE_ARG.long, BLOCKHASH_ARG.long,
295311
),
@@ -298,7 +314,12 @@ impl<'a> Config<'a> {
298314
.exit();
299315
}
300316

301-
let nonce_blockhash = value_of(matches, BLOCKHASH_ARG.name);
317+
let nonce_blockhash = matches
318+
.try_get_one::<Hash>(BLOCKHASH_ARG.name)
319+
.ok()
320+
.flatten()
321+
.copied();
322+
302323
let compute_unit_price = value_of(matches, COMPUTE_UNIT_PRICE_ARG.name);
303324
let compute_unit_limit = value_of(matches, COMPUTE_UNIT_LIMIT_ARG.name)
304325
.map(ComputeUnitLimit::Static)
@@ -309,6 +330,7 @@ impl<'a> Config<'a> {
309330
ComputeUnitLimit::Simulated
310331
}
311332
});
333+
312334
Self {
313335
default_signer,
314336
rpc_client,

0 commit comments

Comments
 (0)