pub fn number_range<T>(s: &str, min: T, max: T) -> Result<T, String>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, value_parser=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 stringValues 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 typeValues exceeding the limits will show an error message similar to this:
error: Invalid value for '--cents <cents>': exceeds maximum of 99