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

A Command-Line Interface framework

pip install knack

Usage

from knack import CLI, CLICommandsLoader, CLICommand

def abc_list(myarg):
    import string
    return list(string.ascii_lowercase)

class MyCommandsLoader(CLICommandsLoader):
    def load_command_table(self, args):
        with CommandSuperGroup(__name__, self, '__main__#{}') as sg:
            with sg.group('abc') as g:
                g.command('list', 'abc_list')
        return OrderedDict(self.command_table)

    def load_arguments(self, command):
        with ArgumentsContext(self, 'abc list') as ac:
            ac.argument('myarg', type=int, default=100)
        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)

More samples and snippets 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 .

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.

Release History

0.2.0 (2017-09-27)

  • 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_argument as ways to register commands and arguments.

0.1.1 (2017-07-05)

  • Add more types of command and argument loaders.

  • Add tests.

0.1.0 (2017-06-16)

  • Initial release

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.2.0.tar.gz (40.9 kB view details)

Uploaded Source

Built Distribution

knack-0.2.0-py2.py3-none-any.whl (58.5 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: knack-0.2.0.tar.gz
  • Upload date:
  • Size: 40.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for knack-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9e66e77d0046fec1419d88a5a88e3b6c69948473bc080d9e0df5250b1012233c
MD5 7f26113b0cf2609d723205493ddca798
BLAKE2b-256 c11aaf6ee1af6e10e52f0c476cda0ef8ace279906a55672a595a606a6fa02447

See more details on using hashes here.

File details

Details for the file knack-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for knack-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a5bce5ad607a913c09c54e164cb9febbec481a1622d1fd2cb9a85acc6c26673f
MD5 36977f1da6496aa70a76f90e22b1e521
BLAKE2b-256 87bd05327fca348e0e4fd8696ad916d36d9961d725c70ba98dbd93e8ff9e9582

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