Description
Knack ===== .. image:: https://img.shields.io/pypi/v/knack.svg :target: https://pypi.python.org/pypi/knack .. image:: https://img.shields.io/pypi/pyversions/knack.svg :target: https://pypi.python.org/pypi/knack .. image:: https://dev.azure.com/azure-sdk/public/_apis/build/status/cli/microsoft.knack?branchName=dev :target: https://dev.azure.com/azure-sdk/public/_build/latest?definitionId=1643&branchName=dev ------------ :: _ _ | | ___ __ __ _ ___| | __ | |/ / '_ \ / _` |/ __| |/ / | <| | | | (_| | (__| < |_|\_\_| |_|\__,_|\___|_|\_\ **A Command-Line Interface framework** Installation is easy via pip: .. code-block:: bash pip install knack Knack can be installed as a non-privileged user to your home directory by adding "--user" as below: .. code-block:: bash pip install knack --user ------------ .. note:: The project is in `initial development phase <https://semver.org/#how-should-i-deal-with-revisions-in-the-0yz-initial-development-phase>`__. We recommend pinning to at least a specific minor version when marking **knack** as a dependency in your project. ------------ Usage ===== .. code-block:: python import sys from collections import OrderedDict from knack import CLI, ArgumentsContext, CLICommandsLoader from knack.commands import CommandGroup def abc_str(length=3): import string return string.ascii_lowercase[:length] class MyCommandsLoader(CLICommandsLoader): def load_command_table(self, args): with CommandGroup(self, 'abc', '__main__#{}') as g: g.command('str', 'abc_str') return OrderedDict(self.command_table) def load_arguments(self, command): with ArgumentsContext(self, 'abc str') as ac: ac.argument('length', type=int) super(MyCommandsLoader, self).load_arguments(command) mycli = CLI(cli_name='mycli', commands_loader_cls=MyCommandsLoader) exit_code = mycli.invoke(sys.argv[1:]) sys.exit(exit_code) # $ python mycli.py abc str # "abc" # $ python mycli.py abc str --length 5 # "abcde" # $ python mycli.py abc str --length 100 # "abcdefghijklmnopqrstuvwxyz" More samples and snippets are available at `examples <https://github.com/Microsoft/knack/tree/dev/examples>`__. Documentation ============= Documentation is available at `docs <https://github.com/Microsoft/knack/tree/dev/docs>`__. Developer Setup =============== In a virtual environment, install the `requirements.txt` file. .. code-block:: bash pip install -r requirements.txt pip install -e . Run Automation ============== This project supports running automation using `tox <https://tox.readthedocs.io/en/latest/>`__. .. code-block:: bash pip install tox tox Real-world uses =============== - `Azure CLI <https://github.com/Azure/azure-cli/>`__: The Azure CLI 2.0 is Azure's new command line experience for managing Azure resources. - `VSTS CLI <https://github.com/Microsoft/vsts-cli>`__: A command-line interface for Visual Studio Team Services (VSTS) and Team Foundation Server (TFS). With the VSTS CLI, you can manage and work with resources including pull requests, work items, builds, and more. - `Service Fabric CLI <https://github.com/Azure/service-fabric-cli>`__: A command-line interface for interacting with Azure Service Fabric clusters and their related entities. Do you use knack in your CLI as well? Open a pull request to include it here. We would love to have it in our list. Release History =============== See `GitHub Releases <https://github.com/Microsoft/knack/releases>`__. Contribute Code =============== This project has adopted the `Microsoft Open Source Code of Conduct <https://opensource.microsoft.com/codeofconduct/>`__. For more information see the `Code of Conduct FAQ <https://opensource.microsoft.com/codeofconduct/faq/>`__ or contact `opencode@microsoft.com <mailto:opencode@microsoft.com>`__ with any additional questions or comments. If you would like to become an active contributor to this project, please follow the instructions provided in `Contribution License Agreement <https://cla.microsoft.com/>`__. License ======= Knack is licensed under `MIT <LICENSE>`__.
Release History
| Version | Changes | Urgency | Date |
|---|---|---|---|
| 0.13.0 | Imported from PyPI (0.13.0) | Low | 4/21/2026 |
| v0.13.0 | * Declare support for Python 3.13 (#290) * Drop Python 3.8 support (#289) | Low | 10/23/2025 |
| v0.12.0 | * Declare support for Python 3.12 (#279) | Low | 7/15/2024 |
| v0.11.0 | * Declare support for Python 3.11 and drop support for Python 3.7 (#275) * Stop converting argument's `bool` default value to `DefaultInt` (#273) | Low | 7/26/2023 |
| v0.10.1 | * Support bytearray serialization (#268) | Low | 12/1/2022 |
| v0.10.0 | * Enable Virtual Terminal mode on legacy Windows terminal to support ANSI escape sequences (#265) * Drop Python 3.6 support (#259) | Low | 8/19/2022 |
| v0.9.0 | * Support Python 3.10 (#250) * Only install colorama on Windows (#249) | Low | 11/5/2021 |
| v0.8.2 | * Always use UTF-8 for log file encoding (#247) | Low | 5/8/2021 |
| v0.8.1 | * Add error message for invalid argument value (#244) | Low | 4/9/2021 |
| v0.8.0 | * Make colors customizable (#242) * Init colorama only in Windows legacy terminal (#238) * Add `raw_result` to `CommandResultItem` (#235) * Refine code style to comply with Python 3 (#232, #233) * CI: Support Python 3.9 (#229) * Logging: `CLILogging.configure` returns as early as possible (#228) | Low | 3/26/2021 |
| v0.8.0rc2 | * Support multiple cli loggers by adding more logger names to `knack.log.cli_logger_names` list (#227) | Low | 12/29/2020 |
| v0.8.0rc1 | * Make config item names case-insensitive (#220) * `get_logger` uses `module_name` directly and no longer adds `cli` prefix (#221) * `CLILogging` accepts a custom `cli_logger_name` (#221) * Support ppc64le arch in Travis CI (#222) * Allow customizing tag message (#223) * Add `EVENT_CLI_SUCCESSFUL_EXECUTE` (#224) | Low | 12/28/2020 |
| v0.7.2 | * [Config] Support listing sections (#217) | Low | 12/23/2020 |
| v0.7.1 | * Rollback `get_config_parser` in `config.py` (#205) | Low | 5/9/2020 |
| v0.7.0 | * Add a `default_value_source` property in `HelpParameter` (#202) * Support removing option/section from config file (#201) * Support writing comment to config file (#201) * Import `configparser` directly instead of from `six` (#201) * Drop `get_config_parser` function from `config.py` (#201) | Low | 5/8/2020 |
| v0.7.0rc4 | * Change the timing to raise `EVENT_CLI_POST_EXECUTE` event (#199) * Make `CLI.invoke` catch `SystemExit` (#199) | Low | 4/22/2020 |
| v0.7.0rc3 | * Change experimental tag color to cyan | Low | 4/16/2020 |
| v0.7.0rc1 | * Allow disabling color (#171) * Support yaml and yamlc output (#173) * Drop support for python 2 and 3.5 (#174) * Support `--only-show-errors` to disable warnings (#179) * Add experimental tag (#180) | Low | 3/20/2020 |
| v0.6.3 | This release: * Fixes issue where argument marked `is_preview=True` would not always be handled correctly. * Fixes issue where ensuring a directory exists could cause a race condition. | Low | 7/9/2019 |
| v0.6.2 | * Adds ability to declare that command groups, commands, and arguments are in a preview status and therefore might change or be removed. This is done by passing the kwarg `is_preview=True`. * Adds a generic `StatusTag` class to `knack.util` that allows you to create your own colorized tags like `[Preview]` and `[Deprecated]`. * When an incorrect command name is entered, Knack will now attempt to suggest the closest alternative. | Low | 5/22/2019 |
| v0.6.1 | Always read from local for configured_default | Low | 4/26/2019 |
| v0.6.0 | Support local context chained config file | Low | 4/24/2019 |
| v0.5.4 | This release - Allows the loading of text files using `@filename` syntax. - Adds the argument kwarg `configured_default` to support setting argument defaults via the config file's [defaults] section or an environment variable. | Low | 3/29/2019 |
| v0.5.3 | This release: - Removes an incorrect check when adding arguments. | Low | 3/5/2019 |
| v0.5.2 | Updates usages of `yaml.load` to use `yaml.safe_load`. | Low | 2/11/2019 |
| v0.5.1 | Fix issue with some scenarios (no args and --version) | Low | 11/7/2018 |
| v0.5.0 | - Adds support for positional arguments with the .positional helper method on ArgumentsContext. - Removes the necessity for the type field in help.py. This information can be inferred from the class, so specifying it causes unnecessary crashes. - Adds support for examining the result of a command after a call to invoke. The raw object, error (if any) an exit code are accessible. - Adds support for accessing the command instance from inside custom commands by putting the special argument `cmd` | Low | 11/7/2018 |
| v0.4.5 | Preserves logging verbosity and output format on the namespace for use by validators. | Low | 10/25/2018 |
| 0.4.4 | Adds ability to set config file name. Fixes bug with argument deprecations. | Low | 10/12/2018 |
| 0.4.3 | Fixes issue where values were sometimes ignored when using deprecated options regardless of which option was given. | Low | 9/6/2018 |
| 0.4.2 | Bug fixes: [output]: disable number parse on table mode PR #88 | Low | 7/31/2018 |
| 0.4.1 | Version 0.4.0 introduced deprecation to Knack. This release fixes a bug related to that. - Ensures that the action kwarg is only set if the item is deprecated. Previously it would set it to "None" which would then override a pre-existing action like `store_true`. Version 0.4.0 also added the concept of the command group table to the CommandsLoader class. This release corrects an issue related to that: - The command group table would only be filled by calls to create CommandGroup classe | Low | 6/5/2018 |
| 0.4.0 | - Add mechanism to deprecate commands, command groups, arguments and argument options. - Improve help display support for Unicode. [View commits since last release](https://github.com/Microsoft/knack/compare/0.3.3...0.4.0) | Low | 5/31/2018 |
| 0.3.3 | - expose a callback to let client side perform extra logics (#80) - output: don't skip false value on auto-tabulating (#83) [View commits since last release](https://github.com/Microsoft/knack/compare/0.3.2...0.3.3) | Low | 5/2/2018 |
| 0.3.2 | - ArgumentsContext.ignore() should use hidden options_list (#76) - Consolidate exception handling (#66) [View commits since last release](https://github.com/Microsoft/knack/compare/0.3.1...0.3.2) | Low | 3/16/2018 |
| 0.3.1 | - Performance optimization - Delay import of platform and colorama (#47) - CLIError: Inherit from Exception directly (#65) - Explicitly state which packages to include (so exclude 'tests') (#68) [View commits since last release](https://github.com/Microsoft/knack/compare/0.3.0...0.3.1) | Low | 1/9/2018 |
| 0.3.0 | Release 0.3.0 | Low | 11/21/2017 |
| 0.2.0 | * Support command level and argument level validators. * knack.commands.CLICommandsLoader now accepts a `command_cls` argument so you can provide your own `CLICommand` class. * logging: make determine_verbose_level private method. * Allow overriding of NAMED_ARGUMENTS * Only pass valid argparse kwargs to argparse.ArgumentParser.add_argument and ignore the rest * logging: make determine_verbose_level private method * Remove `cli_command`, `register_cli_argument`, `register_extra_cli_argumen | Low | 9/27/2017 |
| 0.1.1 | * Add more types of command and argument loaders. * Add tests. [FULL CHANGELOG](https://github.com/Microsoft/knack/compare/0.1.0...0.1.1) | Low | 7/5/2017 |
| 0.1.0 | Initial release. | Low | 6/19/2017 |
