Very thin wrapper over the humantime
library that provides convenience methods to parse time either as a humantime
compatible string or as a number, falling back to a default time unit. Comes in handy for passing a value_parser
function when using Clap
derive macros.
Supports the following convenience wrappers:
parse_duration_fallback_us
: falls back toMicroseconds
parse_duration_fallback_ns
: falls back toNanoseconds
parse_duration_fallback_ms
: falls back toMilliseconds
parse_duration_fallback_sec
: falls back toSeconds
parse_duration_fallback_min
: falls back toMinutes
parse_duration_fallback_hour
: falls back toHours
parse_duration_fallback_day
: falls back toDays
- using a duration string:
let duration_string = "42h"; match parse_duration_fallback_sec(duration_string) { Ok(duration) => { println!("{:?}", duration); } Err(e) => { println!("{:?}", e) } }
- using a number:
let seconds = "1_000_000"; match parse_duration_fallback_sec(seconds) { Ok(duration) => { println!("{:?}", duration); } Err(e) => { println!("{:?}", e) } }
- in a
Clap
macro:and you can now use#[clap(long = "duration-argument", default_value = "100ms", value_parser = parse_duration_fallback_ms)] duration_argument: Duration
./cli --duration-argument 10ms
or./cli --duration-argument 10
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.