diff --git a/pyproject.toml b/pyproject.toml index 15fb961..54df250 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,6 +40,7 @@ dynamic = [ "version", ] dependencies = [ + "argcomplete>=3.4", "httpx>=0.19", "platformdirs", "prettytable>=2.4", diff --git a/src/norwegianblue/__init__.py b/src/norwegianblue/__init__.py index 1e70c9f..b95f068 100644 --- a/src/norwegianblue/__init__.py +++ b/src/norwegianblue/__init__.py @@ -100,15 +100,18 @@ def norwegianblue( return output +def all_products() -> list[str]: + """Get all known products from the API or cache""" + return norwegianblue("all").splitlines() + + @lru_cache(maxsize=None) def suggest_product(product: str) -> str: + """Provide the best suggestion based on a typed product""" import difflib - # Get all known products from the API or cache - all_products = norwegianblue("all").splitlines() - # Find the closest match - result = difflib.get_close_matches(product, all_products, n=1) + result = difflib.get_close_matches(product, all_products(), n=1) logging.info("Suggestion:\t%s (score: %d)", *result) return result[0] if result else "" diff --git a/src/norwegianblue/cli.py b/src/norwegianblue/cli.py index 46cc43f..b6714bd 100644 --- a/src/norwegianblue/cli.py +++ b/src/norwegianblue/cli.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# PYTHON_ARGCOMPLETE_OK """ CLI to show end-of-life dates for a number of products, from https://endoflife.date @@ -19,6 +20,7 @@ import platform import sys +import argcomplete from termcolor import colored import norwegianblue @@ -27,6 +29,11 @@ atexit.register(_cache.clear) +def ProductCompleter(**kwargs): + """The list of all products to feed autocompletion""" + return norwegianblue.all_products() + + def main() -> None: parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter @@ -36,7 +43,7 @@ def main() -> None: nargs="*", default=["all"], help="product to check, or 'all' to list all available (default: 'all')", - ) + ).completer = ProductCompleter parser.add_argument( "-f", "--format", @@ -115,7 +122,7 @@ def main() -> None: help=f"output in {help_text}", ) parser.set_defaults(formatter="pretty") - + argcomplete.autocomplete(parser) args = parser.parse_args() if args.format: