Skip to main content

A Command-Line Interface framework

Project description

Knack

https://img.shields.io/pypi/v/knack.svg https://img.shields.io/pypi/pyversions/knack.svg https://dev.azure.com/azure-sdk/public/_apis/build/status/cli/microsoft.knack?branchName=dev
 _                     _
| | ___ __   __ _  ___| | __
| |/ / '_ \ / _` |/ __| |/ /
|   <| | | | (_| | (__|   <
|_|\_\_| |_|\__,_|\___|_|\_\

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


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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

knack-0.8.0.tar.gz (70.3 kB view details)

Uploaded Source

Built Distribution

knack-0.8.0-py3-none-any.whl (59.7 kB view details)

Uploaded Python 3

File details

Details for the file knack-0.8.0.tar.gz.

File metadata

  • Download URL: knack-0.8.0.tar.gz
  • Upload date:
  • Size: 70.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.13

File hashes

Hashes for knack-0.8.0.tar.gz
Algorithm Hash digest
SHA256 16aa47240add6badd933a0b27576f3c090d7469177dc941e3ece05ca88123199
MD5 b77027cc8b2d97d821342d0a9f15110d
BLAKE2b-256 37302d36482738581c5fcf56b5fdda82bf82806bb4705813ff3466b70990b8e2

See more details on using hashes here.

File details

Details for the file knack-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: knack-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 59.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.13

File hashes

Hashes for knack-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 14cd1222e39ab0af667e071bd0ec8370b1260615693d1b842d6ba1f8ca6afbda
MD5 187950133904e40098fae4e425b4fdf9
BLAKE2b-256 c809653e83e14018810f7db9d82e1a92d4dbec74b2b8f8a8f4d7552c6da3fb30

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page