Skip to content

Commit

Permalink
[clap-v3-utils] Add try_get_word_count (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto authored Mar 26, 2024
1 parent c867522 commit f6a3608
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion clap-v3-utils/src/keygen/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,32 @@ pub const NO_PASSPHRASE_ARG: ArgConstant<'static> = ArgConstant {
help: "Do not prompt for a BIP39 passphrase",
};

// The constant `POSSIBLE_WORD_COUNTS` and function `try_get_word_count` must always be updated in
// sync
const POSSIBLE_WORD_COUNTS: &[&str] = &["12", "15", "18", "21", "24"];
pub fn word_count_arg<'a>() -> Arg<'a> {
Arg::new(WORD_COUNT_ARG.name)
.long(WORD_COUNT_ARG.long)
.value_parser(PossibleValuesParser::new(["12", "15", "18", "21", "24"]))
.value_parser(PossibleValuesParser::new(POSSIBLE_WORD_COUNTS))
.default_value("12")
.value_name("NUMBER")
.takes_value(true)
.help(WORD_COUNT_ARG.help)
}

pub fn try_get_word_count(matches: &ArgMatches) -> Result<Option<usize>, Box<dyn error::Error>> {
Ok(matches
.try_get_one::<String>(WORD_COUNT_ARG.name)?
.map(|count| match count.as_str() {
"12" => 12,
"15" => 15,
"18" => 18,
"21" => 21,
"24" => 24,
_ => unreachable!(),
}))
}

pub fn language_arg<'a>() -> Arg<'a> {
Arg::new(LANGUAGE_ARG.name)
.long(LANGUAGE_ARG.long)
Expand Down

0 comments on commit f6a3608

Please sign in to comment.