Creating a new Drush command is easy. Follow the steps below. If you are porting a legacy commandfile, enter the path to that file after Step 1. Also see tips on porting command to Drush 9.
- Run
drush generate drush-command-file
. - Enter the machine name of the module that should "own" the file.
- Drush will then report that it created a commandfile and a drush.services.yml file. Edit those 2 files as needed.
- Use the classes for the core Drush commands at /src/Drupal/Commands as inspiration and documentation.
- Once your two files are ready, run
drush cr
to get your command recognized by the Drupal container.
Commandfiles that don't ship inside Drupal modules are called 'global' commandfiles. See the examples/Commands folder for examples. In general, its better to use modules to carry your Drush commands. If you still prefer using a global commandfiles, please note:
- The file's namespace should be \Drush\Commands[dir-name].
- The filename must end in Commands.php (e.g. FooCommands.php)
- The enclosing directory must be named Commands
- The directory above Commands must be one of:
- Folders listed in the 'include' option (see
drush topic docs-configuration
). - ../drush, /drush and /sites/all/drush relative to the current Drupal installation.
- Folders listed in the 'include' option (see
- The --ignored-modules global option stops loading of commandfiles from specified modules.