Function clap_num::maybe_hex_range

source ·
pub fn maybe_hex_range<T>(s: &str, min: T, max: T) -> Result<T, String>
where <T as Num>::FromStrRadixErr: Display, <T as FromStr>::Err: Display, T: FromStr + Display + Ord + Num + Unsigned,
Expand description

Validates an unsigned integer value that can be base-10 or base-16 within a range.

This combines maybe_hex and number_range, see the documentation for those functions for details.

Example

This extends the example in maybe_hex, and only allows a range of addresses from 0x100 to 0x200.

use clap::Parser;
use clap_num::maybe_hex_range;

fn address_in_range(s: &str) -> Result<u32, String> {
    maybe_hex_range(s, 0x100, 0x200)
}

#[derive(Parser)]
struct Args {
    #[clap(short, long, value_parser=address_in_range)]
    address: u32,
}