Skip to main content

Plugin based CLI framework built on click

Project description

tests codecov

plugcli

Simple click-based tools for plugin-based CLIs.

plugcli is a small extension of click. It provides several useful pieces of functionality, including:

  • A plugin registration system, allowing user-created plugins to be registered either by installing in a given namespace or by adding plugin files to a specified directory.
  • A CLI class that uses that plugin registration system to register new subcommands, and which separates subcommands into sections for the --help documentation.
  • Tools to facilitate reuse of parameters between different subcommands, helping ensure a consistent user interface. While much of this is already enabled by click, the tools in plugcli are particularly useful in cases where click's callbacks may not be sufficient, such as when the order in which parameters are parsed is important.

Installing plugcli

plugcli can be installed through the standard mechanisms: pip install plugcli; or conda install -c conda-forge plugcli, or install from source using setuptools (python setup.py install, etc.) Its only direct dependency is click.

Plugin registration

The plugin registration system is designed to handle plugins that can essentially be treated as entries in a dispatch table. That is, these are plugins that are designed to give optionally-used additional functionality. Subcommands of the CLI are one example, and plugcli provides specific tools for this use case. Other examples might support for additional keywords when processing a user input file, or support for other options in menus for interactive user interfaces.

The basic approach to writing plugins is to wrap user-defined functionality in some subclass of the plugcli.Plugin class, which may also encapsulate additional metadata of use to the program. For example, the plugcli.CommandPlugin class also takes a section label, to tell which help section the plugin command should be listed in.

A program defines where a user can put plugins by creating one or more PluginLoaders. The two concrete classes of PluginLoader are FilePluginLoader, which takes a directory and searches for any plugins in Python files in that directory, and the NamespacePluginLoader, which takes a string representing a Python namespace and searches for any plugins in modules/subpackages found in that namespace.

CLI Class

The plugcli.CLI class subclasses click.CLI, adding support for loading commands from the plugcli plugin structure, and sorting commands in --help into sections defined by the developer.

Users must subclass plugcli.CLI. The subclass should set the class variable COMMAND_SECTIONS to a list of the section names used in sorting plugins for the help. The subclass should also implement the get_installed_plugins method, which uses a selection of PluginLoaders that determine the locations where plugins will be found and registered.

Reusable parameters

The goal of plugcli's reusable parameters is to ensure consistency throughout different subcommands by encapsulating names and behavior of parameters that can be shared. This is done with the plugcli.Option and plugcli.Argument classes, which take the same parameters as the click.option and click.parameter decorators, plus a getter keyword-only argument that takes an callable which converts the user input as processed by click and returns an object ready for use in the CLI's underlying library code. This takes a user-defined context dict, which provides more flexibility than the standard click callbacks.

You can create a standard click decorator for one of these parameters with the .parameter() method, allowing a single parameter instance to be reused in multiple subcommands. click arguments set in the initialization of the plugcli parameter can be overridden by keyword arguments passed to the .parameter method, allowing some customizability in special cases (e.g., to allow the parameter to be called a different number of times) while retaining most of the consistency.

History

plugcli was originally part of the OpenPathSampling CLI. It was refactored into a separate package as it become more evident that it filled a small, but useful, niche, and could be reused in other projects.

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

plugcli-0.2.0.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

plugcli-0.2.0-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: plugcli-0.2.0.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for plugcli-0.2.0.tar.gz
Algorithm Hash digest
SHA256 26f1e978eb9db6ec5f95c2b5561862127ecee5837ec5a5870d85d19b587ff686
MD5 cdb5ff9a3fb491a9be959050de2776c3
BLAKE2b-256 f4f118211cc0d2f28d576c73a13b852893857d02f4814d5917dc276048826509

See more details on using hashes here.

File details

Details for the file plugcli-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: plugcli-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for plugcli-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3aaf60b48f5d9c0f53e2968d620335dd90e74579b5ee27abf07f0c05b9aa1cba
MD5 452fb4522456f247a0dd11f615f64dae
BLAKE2b-256 2e67843ac177640ca8eb7cf8210066a3c732f64c084c575c35e71033c2906a39

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