< 返回版块

Reiase 发表于 2023-11-06 01:17

复杂系统有大量参数需要配置,命令行或者配置文件往往需要付出额外的开发成本。于是写了个比较通用的参数配置模块,看大家是否感兴趣:

use clap::Parser;
use hyperparameter::*;

fn foo() {
    println!("in function foo");
    with_params! {
        get param1 = example.param1 or 1; // read param `example.param1` or use default value `1`
        get param2 = example.param2 or String::from("2"); // read param `example.param2` or use default value `2`
        get param3 = example.param3 or false; // read param `example.param3` or use default value `false`

        println!("param1={}", param1);
        println!("param2={}", param2);
        println!("param3={}", param3);
    }
}

fn bar() {
    println!("in function bar");
    foo()
}

#[derive(Parser, Debug)]
struct DeriveArgs {
    #[arg(short = 'D', long)]
    define: Vec<String>,
}

fn main() {
    let args = DeriveArgs::parse();
    with_params! {
        params ParamScope::from(&args.define);

        bar()
    }
}

项目地址在此:https://github.com/reiase/hyperparameter

评论区

写评论

还没有评论

1 共 0 条评论, 1 页