Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(storagext-cli): parse numbers with underscores #577

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions cli/polka-storage/storagext-cli/src/cmd/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,28 @@ use url::Url;

use crate::{missing_keypair_error, operation_takes_a_while, OutputFormat};

/// Removes the `_` from a the input before calling [`parse`](std::std::FromStr::parse).
fn parse_without_underscore<T>(s: &str) -> Result<T, T::Err>
aidan46 marked this conversation as resolved.
Show resolved Hide resolved
where
T: std::str::FromStr,
{
s.replace('_', "").parse()
}

#[derive(Debug, Subcommand)]
#[command(name = "market", about = "CLI Client to the Market Pallet", version)]
pub(crate) enum MarketCommand {
/// Add balance to an account.
AddBalance {
/// Amount to add to the account.
#[arg(value_parser=parse_without_underscore::<storagext::Currency>)]
amount: storagext::Currency,
},

/// Withdraw balance from an account.
WithdrawBalance {
/// Amount to withdraw from the account.
#[arg(value_parser=parse_without_underscore::<storagext::Currency>)]
amount: storagext::Currency,
},

Expand Down Expand Up @@ -57,12 +73,6 @@ pub(crate) enum MarketCommand {
deal_ids: Vec<DealId>,
},

/// Withdraw balance from an account.
WithdrawBalance {
/// Amount to withdraw from the account.
amount: storagext::Currency,
},

/// Retrieve the balance for a given account.
RetrieveBalance {
/// The target account's ID.
Expand Down Expand Up @@ -296,3 +306,14 @@ impl MarketCommand {
Ok(submission_result)
}
}

#[cfg(test)]
mod test {
use super::parse_without_underscore;

#[test]
fn test_parse() {
let parsed = parse_without_underscore::<u128>("1_000_0").unwrap();
assert_eq!(parsed, 1_000_0);
}
}