[clap-v3-utils] Add `try_get_word_count` (#411)

This commit is contained in:
samkim-crypto 2024-03-26 13:25:57 +09:00 committed by GitHub
parent c867522de8
commit f6a3608981
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 1 deletions

View File

@ -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)