Skip to content
Myr

Merge pull request #1821 from pallets/feature/plugin-cli

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Added plugin support to the cli

authored by Armin Ronacher · committed by Armin Ronacher · parents b13e83a, 9594876 · Browse files · 2 files+53 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+53 added−0 removed

  • other:+53 added−0 removed

2 files+53 added−0 removed

modified:docs/cli.rst+34 added
212 unmodified lines
213214 Whenever click now needs to operate on a Flask application it will215 call that function with the script info and ask for it to be created.2164. All is rounded up by invoking the script.217218CLI Plugins219-----------220221Flask extensions can always patch the `Flask.cli` instance with more222commands if they want. However there is a second way to add CLI plugins223to Flask which is through `setuptools`. If you make a Python package that224should export a Flask command line plugin you can ship a `setup.py` file225that declares an entrypoint that points to a click command:226227Example `setup.py`::228229 from setuptools import setup230231 setup(232 name='flask-my-extension',233 ...234 entry_points='''235 [flask.commands]236 my-command=mypackage.commands:cli237 ''',238 )239240Inside `mypackage/comamnds.py` you can then export a Click object::241242 import click243244 @click.command()245 def cli():246 """This is an example command."""247248Once that package is installed in the same virtualenv as Flask itself you249can run ``flask my-command`` to invoke your command. This is useful to250provide extra functionality that Flask itself cannot ship.
modified:flask/cli.py+19 added
280 unmodified lines
281 if add_default_commands:282 self.add_command(run_command)283 self.add_command(shell_command)284285 self._loaded_plugin_commands = False286287 def _load_plugin_commands(self):288 if self._loaded_plugin_commands:289 return290 try:291 import pkg_resources292 except ImportError:293 self._loaded_plugin_commands = True294 return295296 for ep in pkg_resources.iter_entry_points('flask.commands'):297 self.add_command(ep.load(), ep.name)298 self._loaded_plugin_commands = True299300 def get_command(self, ctx, name):301 self._load_plugin_commands()302303 # We load built-in commands first as these should always be the304 # same no matter what the app does. If the app does want to305 # override this it needs to make a custom instance of this group306 # and not attach the default commands.
12 unmodified lines
319 except NoAppException:320 pass321322 def list_commands(self, ctx):323 self._load_plugin_commands()324325 # The commands available is the list of both the application (if326 # available) plus the builtin commands.327 rv = set(click.Group.list_commands(self, ctx))328 info = ctx.ensure_object(ScriptInfo)
154 unmodified lines