8 个版本

0.1.11 2022年12月10日
0.1.10 2022年12月10日

#24#clap-parser

Download history 121/week @ 2024-03-14 102/week @ 2024-03-21 125/week @ 2024-03-28 79/week @ 2024-04-04 98/week @ 2024-04-11 96/week @ 2024-04-18 82/week @ 2024-04-25 219/week @ 2024-05-02 76/week @ 2024-05-09 200/week @ 2024-05-16 143/week @ 2024-05-23 280/week @ 2024-05-30 145/week @ 2024-06-06 225/week @ 2024-06-13 189/week @ 2024-06-20 98/week @ 2024-06-27

706 每月下载量
2 crate 中使用

自定义许可证

47KB
706

clap 参数值解析宏

为 clap 参数添加持续时间值解析宏,字段在编译时进行检查

1. duration_range_value_parse
   To be used as the value_parse value on a clap arg 
2. assign_duration_range_validator
   Create a constant to be used on multiple arguments of a clap arg
3. duration_range_validator 
   Create a DurationHumanValidator with compile-time checking

宏 duration_range_value_parse

use clap::Parser;
use clap_duration::duration_range_value_parse;
use duration_human::{DurationHuman, DurationHumanValidator};

#[derive(Parser)]
struct SampleOptions {
    #[arg(
        long, default_value="666000ms",
        value_parser = duration_range_value_parse!(min: 10min, max: 1h)
    )]
    interval: DurationHuman,
}

let opts = SampleOptions::parse();
assert_eq!(format!("{:#}",opts.interval), format!("11min 6s"));
assert_eq!(opts.interval.to_string(), "666s".to_string())

宏:assign_duration_range_validator

use clap::Parser;
use clap_duration::duration_range_value_parse;
use duration_human::{DurationHuman, DurationHumanValidator};

assign_duration_range_validator!( LIFETIME_RANGE = {default: 2h, min: 333s, max: 60day});

#[derive(Parser)]
struct ServerOptions {
    
    #[arg(
        long,
        help = format!("What lifetime will it have, between {}", LIFETIME_RANGE),
        default_value = LIFETIME_RANGE.default,
        value_parser = {|lifetime: &str|LIFETIME_RANGE.parse_and_validate(lifetime)}
    )]
    lifetime: DurationHuman,
}

 let opts = ServerOptions::parse();
 assert_eq!(format!("{:#}",opts.lifetime), format!("11min 6s"));
 assert_eq!(opts.lifetime.to_string(), "666s".to_string());

依赖关系

~4–5.5MB
~105K SLoC