maybe_bin

Function maybe_bin 

Source
pub fn maybe_bin<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-2.

The number is assumed to be base-10 by default, it is parsed as binary if the number is prefixed with 0b (case sensitive!).

ยงExample

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

use clap::Parser;
use clap_num::maybe_bin;

#[derive(Parser)]
struct Args {
    #[clap(short, long, value_parser=maybe_bin::<u8>)]
    address: u8,
}