fuzzy search cannot handle same-distance case
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
cliff |
Fix Released
|
Undecided
|
Brano Zarnovican |
Bug Description
When exact command match is not found, fuzzy search will trigger. It will calculate the Levenshtein distance from entered string to each command. Commands with minimum distance are then offered as suggestion.
Problem:
When all commands have the same distance then the code will bomb-out with IndexError.
To reproduce:
1. create an App with two implicit commands "complete", "help"
2. add one custom command "user"
3. execute cliff app with "node" as command
Expected output:
cliff should suggest all three commands
Actual Result:
$ python myapp.py node
Traceback (most recent call last):
File "myapp.py", line 31, in <module>
sys.
File "myapp.py", line 27, in main
return app.run(argv)
File "/home/
result = self.run_
File "/home/
fuzzy_matches = self.get_
File "/home/
while (dist[i][1] == best_similarity):
IndexError: list index out of range
In the attached example code, there are three commands: "complete", "help", "user". First two are implicit. String "node" entered on command-line has the same distance from all three. Iterating loop looking for the command with the next higher distance will fall over the edge.
There are two similar problems around the same code. It assumes that list of commands is non-empty. It also has problem in case when entered command is a prefix of all command. Since "complete" and "help" are always present and they don't have common prefix, this is not an issue (now).
This problem was originally spotted and analyzed by Radek Smidl.
Changed in python-cliff: | |
status: | Fix Committed → Fix Released |
Fix proposed to branch: master /review. openstack. org/228466
Review: https:/