10 unstable releases (4 breaking)
0.5.0 | Apr 8, 2024 |
---|---|
0.4.0 | Nov 14, 2023 |
0.3.1 | May 2, 2023 |
0.2.2 | Nov 1, 2021 |
0.0.1 | May 31, 2021 |
#1908 in Parser implementations
470 downloads per month
18KB
377 lines
wd_run: a project operation management tool
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:
- json
- yaml
- toml
- http (coding)
Getting Started
[dependencies]
wd_run = "0.2"
Example
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
License
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.
Contribution
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.
Dependencies
~6–15MB
~184K SLoC