Function clap_num::number_range [−][src]
pub fn number_range<T: Ord + PartialOrd + Display>(
s: &str,
min: T,
max: T
) -> Result<T, String> where
T: FromStr,
<T as FromStr>::Err: Display,
Expand description
Validate a signed or unsigned integer value.
Arguments
s
- String to parse.min
- Minimum value, inclusive.max
- Maximum value, inclusive.
Example
This allows for a number of cents to be passed in the range of 0-99 (inclusive).
use clap::Parser;
use clap_num::number_range;
fn less_than_100(s: &str) -> Result<u8, String> {
number_range(s, 0, 99)
}
#[derive(Parser)]
struct Change {
#[clap(long, parse(try_from_str=less_than_100))]
cents: u8,
}
To run this example run cargo run --example change
, giving arguments to
the program after --
, for example:
$ cargo run --example change -- --cents 99
Change: 99 cents
Error Messages
Values that are not numbers will show an error message similar to this:
error: Invalid value for '--cents <cents>': invalid digit found in string
Values resulting in integer overflow will show an error message similar to this:
error: Invalid value for '--cents <cents>': number too large to fit in target type
Values exceeding the limits will show an error message similar to this:
error: Invalid value for '--cents <cents>': exceeds maximum of 99