cliff Command Line Interface Formulation Framework

4.13.3 · active · verified Sat Apr 11

cliff (Command Line Interface Formulation Framework) is a Python library for building structured command-line applications, commonly used for OpenStack clients and other complex CLIs. It provides base classes for application structure, command registration, argument parsing, and consistent output formatting. The current version is 4.13.3, and it maintains a regular release cadence with active development.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a minimal `cliff` application with one command. To make this a discoverable CLI tool, you would typically define entry points in your `pyproject.toml` (or `setup.cfg`). For example, under `[project.entry-points."mycli_entrypoints"]`, you would add `mycommand = my_module:MyCommand` for command discovery, and under `[project.scripts]`, you would add `mycli = my_module:main` to create the executable. The example manually registers the command for simplicity, allowing it to be run directly.

import sys
from cliff.app import App
from cliff.command import Command
from cliff.commandmanager import CommandManager

class MyCommand(Command):
    """A simple example command."""

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument(
            '--name',
            dest='name',
            default='World',
            help='Name to greet'
        )
        return parser

    def take_action(self, parsed_args):
        print(f"Hello, {parsed_args.name}!")

class MyCLIApp(App):

    def __init__(self):
        super().__init__(
            description='My simple cliff-based CLI',
            version='1.0',
            command_manager=CommandManager('mycli_entrypoints'),
            deferred_help=True,
        )

    def initialize_app(self, argv):
        self.LOG.debug('initialize_app')

    def prepare_to_run(self, argv):
        self.LOG.debug('prepare_to_run')

    def clean_up(self, cmd, result, err):
        self.LOG.debug('clean_up')
        if err:
            self.LOG.debug(f'An error occurred: {err}')


def main(argv=sys.argv[1:]):
    # For a real app, commands are typically registered via setup.cfg or pyproject.toml entry points.
    # Example: In pyproject.toml, under [project.entry-points."mycli_entrypoints"]
    #          'mycommand = my_module:MyCommand'
    #          And under [project.scripts]
    #          'mycli = my_module:main'
    # For this quickstart, we manually register the command:
    app = MyCLIApp()
    app.command_manager.add_command('mycommand', MyCommand)

    return app.run(argv)

if __name__ == '__main__':
    # Simulate running 'mycli mycommand --name Registry' from the command line
    sys.exit(main(['mycommand', '--name', 'Registry']))

view raw JSON →