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

Uploaded Source

Built Distribution

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

plugcli-0.2.1-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: plugcli-0.2.1.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for plugcli-0.2.1.tar.gz
Algorithm Hash digest
SHA256 91aa092cbc29e12a933b5b4b9cac354fc6b5c412fdd82747a820e346893ba2ad
MD5 5595eedc44fece8ad27c33b679abfa89
BLAKE2b-256 1b11258a201129ef79bf418656e48a400dc1c9d5a9f7cae4d3f38e439cf276cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for plugcli-0.2.1.tar.gz:

Publisher: autorelease-deploy.yml on dwhswenson/plugcli

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: plugcli-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for plugcli-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 79a74aa9564f4212c2727f912a96e6ee355eb3601284b52196357735f5b55c3b
MD5 fc77661df3dca2227326a0475b56f2b5
BLAKE2b-256 674a21e0d10bdd34137da517ea4c8c80cc76753b372f3a06f1e99406a4c25cc0

See more details on using hashes here.

Provenance

The following attestation bundles were made for plugcli-0.2.1-py3-none-any.whl:

Publisher: autorelease-deploy.yml on dwhswenson/plugcli

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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