Command Line Interface allows to simply create scripts, which can be executed from system shell or via cron.
Task class must extend Vegas\Cli\Task class and implement method setOptions that defines all available task actions.
Name of task class must contain word Task, eg. "CustomTask".
We can create a Task in two locations:
touch app/tasks/CustomTask.php
class CustomTask extends \Vegas\Cli\Task
{
/**
* Available options
*
* @return mixed
*/
public function setOptions()
{
$action = new \Vegas\Cli\Task\Action('test', 'Test action');
//foo option
$foo = new \Vegas\Cli\Task\Option('foo', 'f', 'Foo option. Usage app:custom test -f numberOfSth');
$foo->setValidator(function($value) {
if (!is_numeric($value)) return false;
return true;
});
$foo->setRequired(true);
$action->addOption($foo);
$this->addTaskAction($action);
}
/**
* Test action that shows number passed to "f" option and in the first argument
*/
public function testAction()
{
$this->putText($this->getArg(0));
$this->putText($this->getOption('f'));
}
}
php cli/cli.php app:custom test 6789 -f 1234
6789
1234
app:custom | means that task is a global application task, and it's located in the "app/tasks/CustomTask.php" file. |
test | name of task action to execute. |
6789 | the first execution argument |
-f 1234 | the value of option "f" ("foo") |
touch app/modules/Example/tasks/CustomTask.php
namespace Example\Tasks;
class CustomTask extends \Vegas\Cli\Task
{
/**
* Available options
*
* @return mixed
*/
public function setOptions()
{
$action = new \Vegas\Cli\Task\Action('test', 'Test action');
//foo option
$foo = new \Vegas\Cli\Task\Option('foo', 'f', 'Foo option. Usage app:custom test -f numberOfSth');
$foo->setValidator(function($value) {
if (!is_numeric($value)) return false;
return true;
});
$foo->setRequired(true);
$action->addOption($foo);
$this->addTaskAction($action);
}
/**
* Test action that shows number passed to "f" option and in the first argument
*/
public function testAction()
{
$this->putText($this->getArg(0));
$this->putText($this->getOption('f'));
}
}
php cli/cli.php app:example:custom test 6789 -f 1234
6789
1234
app:example:custom | means that task is an application module task, and it's located in the "app/modules/Example/tasks/CustomTask.php" file. |
test | name of task action to execute. |
6789 | the first execution argument |
-f 1234 | the value of option "f" ("foo") |
To display all actions available in task use the following command:
php cli/cli.php app:example:custom -h
php cli/cli.php app:example:custom test -h