From d1362e04437ea8ce87057d4e94b745ebe828cdab Mon Sep 17 00:00:00 2001 From: Thijs Snelleman <32924404+thijssnelleman@users.noreply.github.com> Date: Mon, 13 Jan 2025 12:27:25 +0100 Subject: [PATCH] CLI update (#138) --- CHANGELOG.md | 1 + sparkle/CLI/_cli_.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c8ab846..3083bb20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Notable changes to Sparkle will be documented in this file. ### Added - Flushing completed jobs from jobs command by hitting spacebar [SPRK-313] +- "Did you mean" functionality for typos in CLI commands [SPRK-345] ### Changed - CI pipeline for unittest now tests for Python 3.10, 3.11 and 3.12 diff --git a/sparkle/CLI/_cli_.py b/sparkle/CLI/_cli_.py index 9225974c..96db9c8c 100644 --- a/sparkle/CLI/_cli_.py +++ b/sparkle/CLI/_cli_.py @@ -52,7 +52,16 @@ def main() -> None: print(f"Sparkle autocomplete installed! To enable, run `source {bash_profile}` " "or restart your terminal.") else: - print(f"Sparkle does not understand command <{command}>") + print(f"Sparkle does not understand command <{command}>", end="") + from difflib import SequenceMatcher + similarities = [SequenceMatcher(None, command, alt).ratio() + for alt in possible_commands] + + if max(similarities) > 0.6: + alternative = possible_commands[similarities.index(max(similarities))] + print(f". Did you mean <{alternative}>?") + else: + print() if __name__ == "__main__":