Creating reusable shell tasks
Just as we have components to share functionality amongst controllers, we also have behaviors for models, and helpers for views. What about shells? CakePHP offers the concept of tasks, which are classes that also extend from the Shell
class, but can be reused from other shells.
In this recipe, we will learn how to build a task that handles argument and parameter processing for our shell, can auto-generate help messages, and check the definition of mandatory arguments and optional parameters. We will implement this task in the most generic fashion, so we can use it for any future shells we may decide to build.
Getting ready
To go through this recipe we need a shell that accepts parameters and has different commands available. Follow the entire recipe Parsing command line parameters.
How to do it...
1. Edit your
app/vendors/shells/user.php
file and add the following right below the declaration of theuses
property:public $tasks = array('Help'); public static $commands...