Function clap_num::si_number

source ·
pub fn si_number<T>(s: &str) -> Result<T, String>
Expand description

Validate a signed or unsigned integer value with a metric prefix.

This can accept strings with the (case sensitive) SI symbols.

SymbolNameValue
Yyotta1_000_000_000_000_000_000_000_000
Zzetta1_000_000_000_000_000_000_000
Eexa1_000_000_000_000_000_000
Ppeta1_000_000_000_000_000
Ttera1_000_000_000_000
Ggiga1_000_000_000
Mmega1_000_000
kkilo1_000

The strings can be provided with a decimal, or using the SI symbol as the decimal separator.

StringValue
3k33300
3.3k3300
1M1_000_000

Example

This allows for resistance value to be passed using SI symbols.

use clap::Parser;
use clap_num::si_number;

#[derive(Parser)]
struct Args {
    #[clap(short, long, value_parser=si_number::<u128>)]
    resistance: u128,
}

To run this example use cargo run --example resistance, giving arguments to the program after --, for example:

$ cargo run --example resistance -- --resistance 1M1
Resistance: 1100000 ohms