Minimal Rust library to provide clap-style "Did you mean?" suggestions
The only dependency is strsim.
The implementation is copied directly from clap (see here). It has just been extracted into a library.
rust
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");
Sometimes, there may be multiple (good) suggestions.
Consider the following example:
rust
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"]);
Asking for a single suggestion here (provide_a_suggestion
) would attempt to return the "best" one.
As you can immagine, that may not be what the user expects.
Therefore, it is best to stick with provide_suggesetions
.
If nothing is reasonably similar, asking for suggestions
will return vec![]
or None
.
rust
let possible_vals = vec!["testing", "things", "here"];
let input = "--something-completely_different";
assert_eq!(suggestions::provide_a_suggestion(&input, &possible_vals), None)
A binary is available as an example of how to use the library.
It has no additional dependencies. Desired targets are provided as arguments, and "possible strings" are read from standard input
$ echo "baz\nbar\nfood\nfoz" | suggestions fod
foz food
````
$ echo "baz\nbar\nfood\nfoz" | suggestions fod ba foz food baz bar ````
````
$ echo "baz\nbar\nfood\nfoz" | suggestions fod ba foz food baz bar
```
```
{ "fod":["foz","food"], "ba":["baz","bar"], "nothing-similar":[] } ````