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.
pip install knackVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.
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.
Update your `CommandsLoader` implementation to remove usage of the `command_group` context manager. Instead, register commands directly by populating `self.command_table` or `self.command_groups` within `load_command_table`, or utilize the `command_group_from_module` pattern if using module-based command loading. Consult Knack's release notes or updated examples for the specific version you are targeting.
Install the library using pip: `pip install knack`
Try reinstalling the `knack` library or the dependent package (e.g., `azdev`) to ensure all submodules and dependencies are correctly installed and compatible. For `azdev`, specifically, a common fix is to manually uninstall older `knack` versions and reinstall the correct one (e.g., `pip install knack=={version}`).Ensure the arguments passed to `cli.invoke()` are enclosed in a list or tuple. For example, `cli.invoke(['my', 'command', '--option', 'value'])` instead of `cli.invoke('my command --option value')`.When initializing `ScenarioTest`, ensure you pass the `method_name` argument. This usually happens automatically when using standard Python `unittest` framework with `knack`'s testing utilities, so review your test class and inheritance. For example: `class MyTests(ScenarioTest): def test_my_command(self): ...` (where `method_name` would be 'test_my_command').