2 个版本 (1 个稳定版)
| 1.0.0 | 2024年4月29日 |
|---|---|
| 1.0.0-rc.1 | 2024年4月27日 |
在 解析器实现 中排名第 1290
每月下载量 65 次
23KB
459 行
命令引擎
将字符串指令转换为代码执行。
引擎
此包提供了一个默认引擎,如果启用默认功能,则存在。它是一个容器,可以执行您的命令。
默认引擎有 2 个版本
sync- 默认。async- 启用async功能。
指令
给定输入被反序列化为在 Instruction 结构体中定义的特定格式。
它允许一个 caller 命令、多个位置 args,以及额外的 o_args,这些像标志一样可以包含 sub_args。
格式
<caller> <arg> <arg> --<o_arg> <sub_arg> <sub_arg> --<o_arg>
示例
example argument1 argument2 --flag2 child1 child2 --flag3 --flag1
反序列化为
Instruction {
caller: "example",
args: vec!["argument1", "argument2"],
o_args: {
let mut map = HashMap::new();
map.insert("--flag2", Some(vec!["child1", "child2"]));
map.insert("--flag3", None);
map.insert("--flag1", None);
map
},
input: "example argument1 argument2 --flag2 child1 child2 --flag3 --flag1",
};
要添加参数中的空格,请使用双引号 "
example "argument 1" "--flag 2" "child 1"
如果参数中有双引号,建议使用收集器 #
example #"argument "quotes" 1"#
没有转义字符以避免任何堆分配。每个参数都是从输入中取出的字符串切片。
示例
同步版本
use command_engine::*;
pub struct Example;
impl CommandInfo for Example {
fn caller(&self) -> &'static str {
"ex"
}
}
impl Command for Example {
type Output = String;
fn on_execute(&self, ins: Instruction) -> Self::Output {
format!("{:?}", ins)
}
}
fn main() {
let input = "ex arg --o_arg sub_arg";
let mut engine = Engine::new();
engine.insert(Example);
// Will return formatted string of the Instruction.
let output = engine.execute(input).unwrap();
println!("{}", output);
}
免责声明
待办事项 (未来)
- 自定义指令(就像输出一样)
- 集成帮助命令
版本
*.*.*- 已发布。*.*.*-rc.*- 发布候选。*.*.*-dev- 未在生产中发布。0.*.*- 已弃用。
依赖
~0–1.1MB
~19K SLoC