Skip to main content

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

Metadata

Release files for cli-toolkit 2.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cli-toolkit 2.5.1
File Size Uploaded Provenance
cli_toolkit-2.5.1.tar.gz 9.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for cli-toolkit 2.5.1
File Interpreter ABI Platform
cli_toolkit-2.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 22.4 kB

Release files / cli_toolkit-2.5.1.tar.gz

Download URL cli_toolkit-2.5.1.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4d7594079cc8c321e9b6ffecbc3ac98e4a3a1d63432235cff0d89260141165c9
BLAKE2b-256 checksum
How to use checksums
00f3d87f76091239572f0df03f3e276386ab1905780abe21bbf17f0aee75125f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.4 Darwin/22.5.0

Release files / cli_toolkit-2.5.1-py3-none-any.whl

Download URL cli_toolkit-2.5.1-py3-none-any.whl
Size 13.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3669b285054cd60b90191db2780e9f8467cc43a5ce48250295896b103740c595
BLAKE2b-256 checksum
How to use checksums
2dad92878fd978edd574bc6ffed9d73d9ac88136bf82950c7c21d6e9b4bcd242
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.4 Darwin/22.5.0

Security for cli-toolkit 2.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

2.5.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page