Function clap_num::maybe_hex[][src]

pub fn maybe_hex<T: Num + Unsigned>(s: &str) -> Result<T, String> where
    <T as Num>::FromStrRadixErr: Display
Expand description

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

The number is assumed to be base-10 by default, it is parsed as hex if the number is prefixed with 0x, case insensitive.

Example

This allows base-10 addresses to be passed normally, or base-16 values to be passed when prefixed with 0x.

use clap::Parser;
use clap_num::maybe_hex;

#[derive(Parser)]
struct Args {
    #[clap(short, long, parse(try_from_str=maybe_hex))]
    address: u32,
}