Skip to main content

Classes to implement CLI commands in python

Project description

Unit Tests Style Checks

CLI toolkit for shell command utilities

This module contains modules to implement CLI scripts by wrapping python argparse.ArgumentParser to user friendly utility classes.

Create a script class

Main class to use is the Script class. It will get it's name from sys.argv[0].

Example:

from cli_toolkit.script import Script

if __name__ == '__main__':
    script = Script()
    script.add_argument('files', nargs='*', help='Files to process')
    args = script.parse_args()
    for filename in args.files:
        script.debug('PROCESSING', filename)
        script.message(filename)

Running the example:

> python test.py foo bar
foo
bar

This is pretty straightforward ArgumentParser, except it

  • sets SIGINT handler
  • adds --debug and --quiet flags
  • adds debug and message functions which honor the debug and quiet flags

Using script with subcommands

More useful is using a script with subcommands. The subcommands require at least name class variable and should have usage, description and epilog.

You also should implement run method and call script.run() to run correct subcommand. Arguments for subcommand parser are registered with method register_parser_arguments.

from cli_toolkit.script import Script
from cli_toolkit.command import Command


class ListCommand(Command):
    name = 'list'
    usage = 'List files'
    description = 'Lists files specified on command line'

    def register_parser_arguments(self, parser):
        parser.add_argument('files', nargs='*', help='Files to process')

    def run(self, args):
        for filename in args.files:
            self.debug('PROCESSING', filename)
            self.message(filename)


if __name__ == '__main__':
    script = Script()
    script.add_subcommand(ListCommand(script))
    script.run()

Running the example:

> python test.py list foo bar
foo
bar

Using nested subcommands

The subcommands can be nested. You need to pass the parser in paren't register_parser_subcommand to add_subcommand.

from cli_toolkit.script import Script
from cli_toolkit.command import Command


class FilesCommand(Command):
    name = 'demo'
    usage = 'Run nested demo subcommands'

    def register_parser_arguments(self, parser):
        """
        Register 'list' command under demo subcommand
        """
        self.add_subcommand(ListCommand(self), parser)
        return parser


class ListCommand(Command):
    name = 'list'
    usage = 'List files'
    description = 'Lists files specified on command line'

    def register_parser_arguments(self, parser):
        """
        Register 'list' command arguments
        """
        parser.add_argument('files', nargs='*', help='Files to process')
        return parser

    def run(self, args):
        if not args.files:
            self.exit(1, 'No files provided')
        for filename in args.files:
            self.debug('PROCESSING', filename)
            self.message(filename)


if __name__ == '__main__':
    script = Script()
    script.add_subcommand(FilesCommand(script))
    script.run()

Running the example:

> python test.py demo list foo bar
foo
bar

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

cli-toolkit-2.2.4.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cli_toolkit-2.2.4-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file cli-toolkit-2.2.4.tar.gz.

File metadata

  • Download URL: cli-toolkit-2.2.4.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.6 Darwin/21.6.0

File hashes

Hashes for cli-toolkit-2.2.4.tar.gz
Algorithm Hash digest
SHA256 bc1de4ad36b02da1734ca4c4de2efe9662d5e62e8e4fcaa742496f6f8daacb37
MD5 01e8c3f291d466258a143f20bd5bed87
BLAKE2b-256 49332a54092dd3f9ad161c1a4878fbc03f0af37d44182166a61dcaaf22baedf4

See more details on using hashes here.

File details

Details for the file cli_toolkit-2.2.4-py3-none-any.whl.

File metadata

  • Download URL: cli_toolkit-2.2.4-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.6 Darwin/21.6.0

File hashes

Hashes for cli_toolkit-2.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0c33692b2ec7cc74d4741636b0582aacebbef384e08b8595f7dd31839e1d5220
MD5 398b882664a28cb9e16ce3ee5441c3db
BLAKE2b-256 030a3377dc5ca4dbf0f5a6435a2c7f7dace4f047089417273808bb4547cfaed2

See more details on using hashes here.

Supported by

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