#did #mean #multiple #json #target #clap-style

bin+lib suggestions

Minimal library to provide clap-style "Did you mean?" suggestions

1 unstable release

0.1.1 Jan 16, 2022
0.1.0 Jan 16, 2022

#15 in #mean

Download history 330/week @ 2024-01-05 379/week @ 2024-01-12 195/week @ 2024-01-19 338/week @ 2024-01-26 547/week @ 2024-02-02 193/week @ 2024-02-09 1196/week @ 2024-02-16 593/week @ 2024-02-23 592/week @ 2024-03-01 650/week @ 2024-03-08 286/week @ 2024-03-15 425/week @ 2024-03-22 475/week @ 2024-03-29 686/week @ 2024-04-05 999/week @ 2024-04-12 883/week @ 2024-04-19

3,090 downloads per month

MIT license

12KB
154 lines

suggestions docs [crates.io]](https://lib.rs/crates/suggestions)

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.

Examples

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");

Multiple matches

Sometimes, there may be multiple (good) suggestions.

Consider the following example:

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.

No matches

If nothing is reasonably similar, asking for suggestions will return vec![] or None.

let possible_vals = vec!["testing", "things", "here"];
let input = "--something-completely_different";
assert_eq!(suggestions::provide_a_suggestion(&input, &possible_vals), None)

Binary

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

Examples

$ 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":[]
}

Dependencies

~43KB