{"id":1526,"library":"knack","title":"Knack CLI Framework","description":"Knack is a lightweight Python framework for building command-line interfaces (CLIs) with a focus on ease of use and extensibility. It provides common CLI features like command grouping, argument parsing, help generation, and output formatting. The current version is 0.13.0, with new releases typically tied to Python version support and bug fixes, occurring a few times a year.","status":"active","version":"0.13.0","language":"en","source_language":"en","source_url":"https://github.com/microsoft/knack","tags":["cli","framework","command-line","microsoft"],"install":[{"cmd":"pip install knack","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used for ANSI escape sequence support on Windows terminals. Conditionally installed only on Windows.","package":"colorama","optional":true}],"imports":[{"symbol":"CLI","correct":"from knack.cli import CLI"},{"note":"CLICommandsLoader is part of the 'commands' module, not 'cli'.","wrong":"from knack.cli import CLICommandsLoader","symbol":"CLICommandsLoader","correct":"from knack.commands import CLICommandsLoader"}],"quickstart":{"code":"import sys\nfrom knack.cli import CLI\nfrom knack.commands import CLICommandsLoader\n\n# Define a simple command handler\ndef hello_command_handler():\n    \"\"\"Says hello.\"\"\"\n    print(\"Hello from Knack CLI!\")\n    return 0\n\n# Define a custom commands loader\nclass MyCommandsLoader(CLICommandsLoader):\n    def load_command_table(self, args):\n        # Define a command group 'greet' and a command 'hello' within it\n        with self.command_group('greet', operations_tmpl='{}#{{}}'.format(__name__)) as g:\n            g.command('hello', 'hello_command_handler')\n        return self.command_table\n\n# Create the CLI instance\nmycli = CLI(cli_name='mycli', commands_loader_cls=MyCommandsLoader)\n\nif __name__ == '__main__':\n    # Example of invoking the CLI programmatically.\n    # For actual command-line use, you'd typically pass sys.argv[1:].\n    # To run this example: python your_script.py\n    print(\"\\n--- Invoking 'mycli greet hello' ---\")\n    exit_code = mycli.invoke(['greet', 'hello'])\n    print(f\"CLI exited with code: {exit_code}\")\n\n    print(\"\\n--- Invoking 'mycli --help' ---\")\n    exit_code = mycli.invoke(['--help'])\n    print(f\"CLI exited with code: {exit_code}\")","lang":"python","description":"This quickstart demonstrates how to set up a basic Knack CLI with a single command group and command. It defines a command loader, registers a simple 'hello' command, and then invokes the CLI programmatically. Save this as a Python file and run it directly to see the output."},"warnings":[{"fix":"Upgrade to a supported Python version. For example, v0.13.0 dropped Python 3.8 support; v0.11.0 dropped 3.7; v0.10.0 dropped 3.6. Always check the release notes for the target Knack version for exact Python compatibility.","message":"Knack frequently drops support for older Python versions. Ensure your environment uses a currently supported Python version.","severity":"breaking","affected_versions":"v0.10.0 and above"},{"fix":"Prior to v0.11.0, `bool` default values were implicitly converted to `DefaultInt`. This conversion was stopped. If your code relied on this previous, potentially incorrect, behavior for argument default values, verify your argument parsing logic, especially for boolean flags.","message":"The internal handling of `bool` default values for arguments changed, which might affect argument parsing logic.","severity":"breaking","affected_versions":"v0.11.0"},{"fix":"If your cross-platform project relies on `colorama` being available on non-Windows systems (e.g., for direct import and usage), you will need to explicitly add `colorama` to your project's dependencies for those platforms, as Knack no longer installs it universally.","message":"The `colorama` dependency, used for ANSI escape sequences, became conditionally installed only on Windows.","severity":"gotcha","affected_versions":"v0.9.0 and above"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}