Skip to content
Knack - A Python command line interface framework
Python
Branch: master
Clone or download
adewaleo and tjprescott Fix preview mechanism bug (#161)
* Fix bug where PreviewArgumentAction, does not inherit from registered argument action.

* Update test to catch issue. Update fix logic to pass tests.

* python2: in test, use print function from __future__
Latest commit 44ee179 Jul 9, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs
examples Ensure usages of ~ also use os.path.expanduser. (#153) May 22, 2019
knack
scripts
tests
.flake8
.gitattributes
.gitignore
.pylintrc Remove blocking check. (#140) Mar 5, 2019
.travis.yml Add Tox support Nov 27, 2017
CONTRIBUTING.rst Update tests, upgrade Python syntax, fix some flake8 (#102) Oct 12, 2018
HISTORY.rst Fix preview mechanism bug (#161) Jul 9, 2019
LICENSE
MANIFEST.in
README.rst
VS2015.sln
VS2017.sln Add deprecation mechanism (#84) May 31, 2018
knack.pyproj Add deprecation mechanism (#84) May 31, 2018
requirements.txt
setup.cfg
setup.py Fix preview mechanism bug (#161) Jul 9, 2019
tox.ini

README.rst

Knack

https://travis-ci.org/Microsoft/knack.svg?branch=master
 _                     _
| | ___ __   __ _  ___| | __
| |/ / '_ \ / _` |/ __| |/ /
|   <| | | | (_| | (__|   <
|_|\_\_| |_|\__,_|\___|_|\_\

A Command-Line Interface framework

Installation is easy via pip:

pip install knack

Knack can be installed as a non-privileged user to your home directory by adding "--user" as below:

pip install knack --user

Note

The project is in initial development phase. We recommend pinning to at least a specific minor version when marking knack as a dependency in your project.


Usage

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.

Documentation

Documentation is available at docs.

Developer Setup

In a virtual environment, install the requirements.txt file.

pip install -r requirements.txt
pip install -e .

Run Automation

This project supports running automation using tox.

pip install tox
tox

Real-world uses

  • Azure CLI: The Azure CLI 2.0 is Azure's new command line experience for managing Azure resources.
  • 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: 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.

Contribute Code

This project has adopted the Microsoft Open Source Code of Conduct.

For more information see the Code of Conduct FAQ or contact 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.

License

Knack is licensed under MIT.

You can’t perform that action at this time.