DokuWiki

It's better when it's simple

User Tools

Site Tools


devel:cli_plugins

CLI Plugins

CLI (Command Line Interface) plugins give you an easy way to provide more DokuWiki features accessible from the command line. This is mostly useful as component in a larger plugin where certain functionality should be run from Cron jobs or by experienced Administrators only.

CLI plugins are based on the php-cli library, it's documentation might be an additional help.

Available CLI Plugins are listed. They can be installed via the Extension Manager.

Executing CLI Plugins

DokuWiki comes with bin/plugin.php. Call it without parameters to list all available CLI plugins. Call it with the name of the plugin as the first argument to call that plugin.

For the Example plugin below, call

./bin/plugin.php example -v

Synopsis

An CLI Plugin Example needs:

  • class name cli_plugin_example
  • which extends CLIPlugin1).
  • to be stored in a file lib/plugins/example/cli.php.

For full details of plugins and their files and how to create more CLI components refer to plugin file structure.

Required Implementation

Inheriting from the dokuwiki\Extension\CLIPlugin requires you to implement multiple methods in your class.

setup()

This method is passed an Options object on which you should call multiple methods to configure the behavior of your CLI tool. This configures which arguments and parameters are accepted and required for your tool.

Refer to the Options documentation on the available methods.

main()

This method is passed an Options object from which you can get all the parsed arguments and parameters.

This is where you should call your main code. You might want to use a Helper Plugin here.

Inherited functions

See common plugin functions for inherited functions available to all plugins. e.g. localisation, configuration and introspection.

Example

<?php
use dokuwiki\Extension\CLIPlugin;
use splitbrain\phpcli\Options;
 
class cli_plugin_example extends CLIPlugin {
 
    /**
     * register options and arguments
     * 
     * @param Options $options
     */
    protected function setup(Options $options) {
        $options->setHelp('A very minimal example that does nothing but print the plugin version info');
        $options->registerOption('version', 'print version', 'v');
    }
 
    /**
     * implement your code
     * 
     * @param Options $options
     */
    protected function main(Options $options)
    {
        if ($options->getOpt('version')) {
            $info = $this->getInfo(); // method available in all DokuWiki plugins
            $this->success($info['date']);
        } else {
            echo $options->help();
        }
    }
}

Further reading

1)
defined in lib/Extension/CLIPlugin.php, before called DokuWiki_CLI_Plugin which is still available as alias
devel/cli_plugins.txt · Last modified: 2023-09-02 00:16 by Klap-in

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki