Having a command line interface to the functionality provided by Drupal modules has been a highly valuable and widely-used feature that has been used for many years. Today, there are over 500 Drupal modules that provide Drush commands, and the number keeps growing. On top of this, some modules have started to use Drupal Console to implement their command line tools. Drupal Console provides an object-oriented interface and a host of utility functions provided by the Symfony Console libraries. The Drupal Console APIs are useful, but the existence of two standards has lead to some duplication of functionality, which increases maintenance costs. This is particularly acute for features such as site install and configuration import / export, which occasionally need to be adjusted to keep pace with changes made in Drupal core. This situation also creates some angst for module developers, who must decide which API will be best for their project.
Recently, the Drush and Drupal Console maintainers started a collaborative effort to make an even better way to use to rapidly create command line tools for Drupal modules. The new system uses annotations to describe the characteristics of your command. That is literally all that is required to make your command work with both Drush and Drupal Console. The command line tool framework just gets out of your way, and lets you call your module APIs to do what you need to do.
There is already a preliminary implementation of one of these “annotated command files” available for the Drupal 8 default_content module that provides two commands to export content. The basic structure is shown below:
The entire command is available in a patch in the default_content issue queue, Modern CLI command. It works with pre-release versions of both Drush and Drupal Console, as long as you have installed the appropriate PR: Use Symfony Console application and annotation commands in Drush, or Register annotation commands in Drupal Console, respectively.
Progress is being made rapidly on this and other new features that will make the task of providing a command line interface to your module quick and easy. Be sure to catch our session at DrupalCon New Orleans, Writing Command Line Tools for Drupal 8 Modules to learn all about it.Topics: Development, Drupal Planet, Drupal