1 个不稳定版本
0.1.1 | 2022年1月16日 |
---|---|
0.1.0 |
|
1876 在 解析实现
2,934 每月下载量
12KB
154 行
suggestions []](https://crates.org.cn/crates/suggestions)
提供 clap-style "你可能想要" 建议的最小 Rust 库
唯一的依赖项是 strsim.
实现直接从 clap (见此处) 复制。它已被提取为一个库。
示例
let possible_vals = vec!["test", "possible", "values"];
let input = "tst";
let suggestions = suggestions::provide_suggestions(input, &possible_vals);
assert_eq!(suggestions, vec!["test"]);
// We have a convenience function to only pick only a single suggestion, giving `Some` or `None`
let single_suggestion = suggestions::provide_a_suggestion(input, &possible_vals);
assert_eq!(single_suggestion.unwrap(), "test");
多个匹配项
有时可能会有多个(良好的)建议。
考虑以下示例
let possible_vals = vec!["testing", "tempo"];
let input = "teso"; // Sems ambiguous. Maybe multiple suggestions?
let suggestions = suggestions::provide_suggestions(input, &possible_vals);
// The implementation trys to order matches from "best" to "wort"
assert_eq!(suggestions, vec!["testing", "tempo"]);
在这里(provide_a_suggestion
)请求单个建议将尝试返回“最佳”的一个。正如你可以想象的那样,这可能与用户的期望不符。因此,最好坚持使用provide_suggesetions
。
没有匹配项
如果没有合理相似的内容,请求建议将返回 vec![]
或 None
。
let possible_vals = vec!["testing", "things", "here"];
let input = "--something-completely_different";
assert_eq!(suggestions::provide_a_suggestion(&input, &possible_vals), None)
二进制文件
作为一个如何使用库的示例,提供了一个二进制文件。
它没有额外的依赖项。所需的目标是作为参数提供,并从标准输入读取“可能的字符串”
示例
$ echo "baz\nbar\nfood\nfoz" | suggestions fod
foz food
# Supports multiple targets
$ echo "baz\nbar\nfood\nfoz" | suggestions fod ba
foz food
baz bar
# No matches -> corresponding empty line
$ echo "baz\nbar\nfood\nfoz" | suggestions fod ba
foz food
baz bar
```
```
# Supports outputing as josn (for whatever that's worth)
# echo "baz\nbar\nfood\nfoz" | suggestions --json fod ba nothing-similar
{
"fod":["foz","food"],
"ba":["baz","bar"],
"nothing-similar":[]
}
依赖关系
~43KB