=============
click-plugins
=============
.. image:: https://travis-ci.org/click-contrib/click-plugins.svg?branch=master
:target: https://travis-ci.org/click-contrib/click-plugins?branch=master
.. image:: https://coveralls.io/repos/click-contrib/click-plugins/badge.svg?branch=master&service=github
:target: https://coveralls.io/github/click-contrib/click-plugins?branch=master
An extension module for `click <https://github.com/mitsuhiko/click>`_ to register
external CLI commands via setuptools entry-points.
Why?
----
Lets say you develop a commandline interface and someone requests a new feature
that is absolutely related to your project but would have negative consequences
like additional dependencies, major refactoring, or maybe its just too domain
specific to be supported directly. Rather than developing a separate standalone
utility you could offer up a `setuptools entry point <https://pythonhosted.org/setuptools/setuptools.html#dynamic-discovery-of-services-and-plugins>`_
that allows others to use your commandline utility as a home for their related
sub-commands. You get to choose where these sub-commands or sub-groups CAN be
registered but the plugin developer gets to choose they ARE registered. You
could have all plugins register alongside the core commands, in a special
sub-group, across multiple sub-groups, or some combination.
Enabling Plugins
----------------
For a more detailed example see the `examples <https://github.com/click-contrib/click-plugins/tree/master/example>`_ section.
The only requirement is decorating ``click.group()`` with ``click_plugins.with_plugins()``
which handles attaching external commands and groups. In this case the core CLI developer
registers CLI plugins from ``core_package.cli_plugins``.
.. code-block:: python
from pkg_resources import iter_entry_points
import click
from click_plugins import with_plugins
@with_plugins(iter_entry_points('core_package.cli_plugins'))
@click.group()
def cli():
"""Commandline interface for yourpackage."""
@cli.command()
def subcommand():
"""Subcommand that does something."""
Developing Plugins
------------------
Plugin developers need to register their sub-commands or sub-groups to an
entry-point in their ``setup.py`` that is loaded by the core package.
.. code-block:: python
from setuptools import setup
setup(
name='yourscript',
version='0.1',
py_modules=['yourscript'],
install_requires=[
'click',
],
entry_points='''
[core_package.cli_plugins]
cool_subcommand=yourscript.cli:cool_subcommand
another_subcommand=yourscript.cli:another_subcommand
''',
)
Broken and Incompatible Plugins
-------------------------------
Any sub-command or sub-group that cannot be loaded is caught and converted to
a ``click_plugins.core.BrokenCommand()`` rather than just crashing the entire
CLI. The short-help is converted to a warning message like:
.. code-block:: console
Warning: could not load plugin. See ``<CLI> <command/group> --help``.
and if the sub-command or group is executed the entire traceback is printed.
Best Practices and Extra Credit
-------------------------------
Opening a CLI to plugins encourages other developers to independently extend
functionality independently but there is no guarantee these new features will
be "on brand". Plugin developers are almost certainly already using features
in the core package the CLI belongs to so defining commonly used arguments and
options in one place lets plugin developers reuse these flags to produce a more
cohesive CLI. If the CLI is simple maybe just define them at the top of
``yourpackage/cli.py`` or for more complex packages something like
``yourpackage/cli/options.py``. These common options need to be easy to find
and be well documented so that plugin developers know what variable to give to
their sub-command's function and what object they can expect to receive. Don't
forget to document non-obvious callbacks.
Keep in mind that plugin developers also have access to the parent group's
``ctx.obj``, which is very useful for passing things like verbosity levels or
config values around to sub-commands.
Here's some code that sub-commands could re-use:
.. code-block:: python
from multiprocessing import cpu_count
import click
jobs_opt = click.option(
'-j', '--jobs', metavar='CORES', type=click.IntRange(min=1, max=cpu_count()), default=1,
show_default=True, help="Process data across N cores."
)
Plugin developers can access this with:
.. code-block:: python
import click
import parent_cli_package.cli.options
@click.command()
@parent_cli_package.cli.options.jobs_opt
def subcommand(jobs):
"""I do something domain specific."""
Installation
------------
With ``pip``:
.. code-block:: console
$ pip install click-plugins
From source:
.. code-block:: console
$ git clone https://github.com/click-contrib/click-plugins.git
$ cd click-plugins
$ python setup.py install
Developing
----------
.. code-block:: console
$ git clone https://github.com/click-contrib/click-plugins.git
$ cd click-plugins
$ pip install -e .\[dev\]
$ pytest tests --cov click_plugins --cov-report term-missing
Changelog
---------
See ``CHANGES.txt``
Authors
-------
See ``AUTHORS.txt``
License
-------
See ``LICENSE.txt``
Raw data
{
"_id": null,
"home_page": "https://github.com/click-contrib/click-plugins",
"name": "click-plugins",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "click plugin setuptools entry-point",
"author": "Kevin Wurster, Sean Gillies",
"author_email": "wursterk@gmail.com, sean.gillies@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/5f/1d/45434f64ed749540af821fd7e42b8e4d23ac04b1eda7c26613288d6cd8a8/click-plugins-1.1.1.tar.gz",
"platform": "",
"description": "=============\nclick-plugins\n=============\n\n.. image:: https://travis-ci.org/click-contrib/click-plugins.svg?branch=master\n :target: https://travis-ci.org/click-contrib/click-plugins?branch=master\n\n.. image:: https://coveralls.io/repos/click-contrib/click-plugins/badge.svg?branch=master&service=github\n :target: https://coveralls.io/github/click-contrib/click-plugins?branch=master\n\nAn extension module for `click <https://github.com/mitsuhiko/click>`_ to register\nexternal CLI commands via setuptools entry-points.\n\n\nWhy?\n----\n\nLets say you develop a commandline interface and someone requests a new feature\nthat is absolutely related to your project but would have negative consequences\nlike additional dependencies, major refactoring, or maybe its just too domain\nspecific to be supported directly. Rather than developing a separate standalone\nutility you could offer up a `setuptools entry point <https://pythonhosted.org/setuptools/setuptools.html#dynamic-discovery-of-services-and-plugins>`_\nthat allows others to use your commandline utility as a home for their related\nsub-commands. You get to choose where these sub-commands or sub-groups CAN be\nregistered but the plugin developer gets to choose they ARE registered. You\ncould have all plugins register alongside the core commands, in a special\nsub-group, across multiple sub-groups, or some combination.\n\n\nEnabling Plugins\n----------------\n\nFor a more detailed example see the `examples <https://github.com/click-contrib/click-plugins/tree/master/example>`_ section.\n\nThe only requirement is decorating ``click.group()`` with ``click_plugins.with_plugins()``\nwhich handles attaching external commands and groups. In this case the core CLI developer\nregisters CLI plugins from ``core_package.cli_plugins``.\n\n.. code-block:: python\n\n from pkg_resources import iter_entry_points\n\n import click\n from click_plugins import with_plugins\n\n\n @with_plugins(iter_entry_points('core_package.cli_plugins'))\n @click.group()\n def cli():\n \"\"\"Commandline interface for yourpackage.\"\"\"\n\n @cli.command()\n def subcommand():\n \"\"\"Subcommand that does something.\"\"\"\n\n\nDeveloping Plugins\n------------------\n\nPlugin developers need to register their sub-commands or sub-groups to an\nentry-point in their ``setup.py`` that is loaded by the core package.\n\n.. code-block:: python\n\n from setuptools import setup\n\n setup(\n name='yourscript',\n version='0.1',\n py_modules=['yourscript'],\n install_requires=[\n 'click',\n ],\n entry_points='''\n [core_package.cli_plugins]\n cool_subcommand=yourscript.cli:cool_subcommand\n another_subcommand=yourscript.cli:another_subcommand\n ''',\n )\n\n\nBroken and Incompatible Plugins\n-------------------------------\n\nAny sub-command or sub-group that cannot be loaded is caught and converted to\na ``click_plugins.core.BrokenCommand()`` rather than just crashing the entire\nCLI. The short-help is converted to a warning message like:\n\n.. code-block:: console\n\n Warning: could not load plugin. See ``<CLI> <command/group> --help``.\n\nand if the sub-command or group is executed the entire traceback is printed.\n\n\nBest Practices and Extra Credit\n-------------------------------\n\nOpening a CLI to plugins encourages other developers to independently extend\nfunctionality independently but there is no guarantee these new features will\nbe \"on brand\". Plugin developers are almost certainly already using features\nin the core package the CLI belongs to so defining commonly used arguments and\noptions in one place lets plugin developers reuse these flags to produce a more\ncohesive CLI. If the CLI is simple maybe just define them at the top of\n``yourpackage/cli.py`` or for more complex packages something like\n``yourpackage/cli/options.py``. These common options need to be easy to find\nand be well documented so that plugin developers know what variable to give to\ntheir sub-command's function and what object they can expect to receive. Don't\nforget to document non-obvious callbacks.\n\nKeep in mind that plugin developers also have access to the parent group's\n``ctx.obj``, which is very useful for passing things like verbosity levels or\nconfig values around to sub-commands.\n\nHere's some code that sub-commands could re-use:\n\n.. code-block:: python\n\n from multiprocessing import cpu_count\n\n import click\n\n jobs_opt = click.option(\n '-j', '--jobs', metavar='CORES', type=click.IntRange(min=1, max=cpu_count()), default=1,\n show_default=True, help=\"Process data across N cores.\"\n )\n\nPlugin developers can access this with:\n\n.. code-block:: python\n\n import click\n import parent_cli_package.cli.options\n\n\n @click.command()\n @parent_cli_package.cli.options.jobs_opt\n def subcommand(jobs):\n \"\"\"I do something domain specific.\"\"\"\n\n\nInstallation\n------------\n\nWith ``pip``:\n\n.. code-block:: console\n\n $ pip install click-plugins\n\nFrom source:\n\n.. code-block:: console\n\n $ git clone https://github.com/click-contrib/click-plugins.git\n $ cd click-plugins\n $ python setup.py install\n\n\nDeveloping\n----------\n\n.. code-block:: console\n\n $ git clone https://github.com/click-contrib/click-plugins.git\n $ cd click-plugins\n $ pip install -e .\\[dev\\]\n $ pytest tests --cov click_plugins --cov-report term-missing\n\n\nChangelog\n---------\n\nSee ``CHANGES.txt``\n\n\nAuthors\n-------\n\nSee ``AUTHORS.txt``\n\n\nLicense\n-------\n\nSee ``LICENSE.txt``\n\n",
"bugtrack_url": null,
"license": "New BSD",
"summary": "An extension module for click to enable registering CLI commands via setuptools entry-points.",
"version": "1.1.1",
"split_keywords": [
"click",
"plugin",
"setuptools",
"entry-point"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "943968f6aa1a14862f164c2080cb2fda",
"sha256": "5d262006d3222f5057fd81e1623d4443e41dcda5dc815c06b442aa3c02889fc8"
},
"downloads": -1,
"filename": "click_plugins-1.1.1-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "943968f6aa1a14862f164c2080cb2fda",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 7497,
"upload_time": "2019-04-04T04:27:03",
"upload_time_iso_8601": "2019-04-04T04:27:03.360149Z",
"url": "https://files.pythonhosted.org/packages/e9/da/824b92d9942f4e472702488857914bdd50f73021efea15b4cad9aca8ecef/click_plugins-1.1.1-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "969268b5b005b2b56115c66c55013252",
"sha256": "46ab999744a9d831159c3411bb0c79346d94a444df9a3a3742e9ed63645f264b"
},
"downloads": -1,
"filename": "click-plugins-1.1.1.tar.gz",
"has_sig": false,
"md5_digest": "969268b5b005b2b56115c66c55013252",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 8164,
"upload_time": "2019-04-04T04:27:04",
"upload_time_iso_8601": "2019-04-04T04:27:04.820636Z",
"url": "https://files.pythonhosted.org/packages/5f/1d/45434f64ed749540af821fd7e42b8e4d23ac04b1eda7c26613288d6cd8a8/click-plugins-1.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2019-04-04 04:27:04",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "click-contrib",
"github_project": "click-plugins",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "click-plugins"
}