Crates.io | wd_run |
lib.rs | wd_run |
version | 0.5.0 |
source | src |
created_at | 2021-05-31 15:25:57.56854 |
updated_at | 2024-04-08 07:31:01.212306 |
description | a project operation management tool |
homepage | |
repository | |
max_upload_size | |
id | 404270 |
size | 19,037 |
wd_run supports command line parsing and configuration file parsing. It is mainly achieved through the method of registering callbacks.
The following configuration file parsing formats are supported:
[dependencies]
wd_run = "0.2"
use std::future::Future;
use std::pin::Pin;
use wd_run::*;
#[tokio::main]
async fn main() {
ArgsManager::new()
.add_global_flag("name", "wd_run_test", "server name")
.register_init(init_one)
.register_init(init_two)
.register_cmd(
CmdInfo::new("run", "run appliation").add("cycle", 5, "cycle index"),
cmd_run,
)
.register_cmd(
CmdInfo::new("show", "show init value").add("desc", "show info", "none"),
cmd_show,
)
.register_exit(exit)
.run()
.await;
}
pub fn init_one(ctx: Context) -> Pin<Box<dyn Future<Output = Context> + Send>> {
Box::pin(async move {
//load config
// let result:Config = load_config("./config.yaml").unwrap();
ctx.set("init_one", "success".to_string()).await;
return ctx;
})
}
pub fn init_two(ctx: Context) -> Pin<Box<dyn Future<Output = Context> + Send>> {
Box::pin(async move {
ctx.set("init_two", "success".to_string()).await;
return ctx;
})
}
pub fn cmd_run(ctx: Context) -> Pin<Box<dyn Future<Output = Context> + Send>> {
Box::pin(async move {
let cycle = parse_args::<_, u32>(&ctx, "cycle").await.unwrap();
for i in 0..cycle {
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
println!("--->{}", i);
}
return ctx;
})
}
pub fn cmd_show(ctx: Context) -> Pin<Box<dyn Future<Output = Context> + Send>> {
Box::pin(async move {
let name = ctx.copy::<_, String>("name").await.unwrap();
let desc = ctx.copy::<_, String>("desc").await.unwrap();
println!("----------> {}:{} <---------------", name, desc);
let one = ctx.copy::<_, String>("init_one").await.unwrap();
let two = ctx.copy::<_, String>("init_two").await.unwrap();
println!("init one:{} two:{}", one, two);
return ctx;
})
}
pub fn exit(ctx: Context) -> Pin<Box<dyn Future<Output = Context> + Send>> {
Box::pin(async move {
println!("game over");
return ctx;
})
}
Run the following command:
cargo run -- run --cycle 3
cargo run -- show --name wd_run
Licensed under either of
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.