Command Line Function

You can easily create custom CLI commands using the create cli CLI command, such as:

apex create cli <PACKAGE> <ALIAS>

This will create a new file at /etc/<PACKAGE>/Opus/Cli/<ALIAS>.php which is a class that contains two methods that are explained below:

Method Description
process() Executed when the CLI command is run.
help() Used to output the help screen when the CLI command is run with help as the first argument.

You can now run the CLI command anytime from within the installation directory with the command:

apex <PACKAGE> <ALIAS>

And you may get the help screen by running the command:

apex help <PACKAGE> <ALIAS>

Both, the process() and help() methods are passed an argument of an instance of the Cli class. This object contains various methods to output messages to the screen, gather user input, display a table, gather a password securely, and more. For full information on all methods available within the object, please visit the Apex\App\Cli\CLi Reference section.

Arguments and Flags

All non-flag arguments will be passed to the process() method as the one-dimensional $args array. You may then use the Cli::getArgs() method. This methods accepts one array of any long flags (start with two hyphens --) that accept a value, and returns an associative array of all sort and long flags.

All short and long flags present that do not have a value attached to them will have a value of true. Otherwise, for flags that do have values assigned, the value within the array will be its value. For example, with the CLI command:

apex package alias arg1 arg2 -a -e --filename /home/username/test.txt --title "My Test TItle"

$opt = $cli->getArgs(['filename', 'title']);

$filename = $opt['filename'];  // /home/username/test.txt
$title = $opt['title'];    / "My Test Title"
$opt['a'];   /// true
$opt['e'];    // true

Example

The below three commands show examples of creating a CLI command, plus running it and displaying the help screen for it.

apex create cli my-shop recurring-payments

apex my-shop recurring-payments

apex help my-shop recurring-payments

Upon creating the CLI command, a new file was created at /src/MyShop/Opus/Cli/RecurringPayments.php, an example of which is below.

<?php
declare(strict_types = 1);

namespace App\MyShop\Opus\Cli;

use Apex\App\Cli\{Cli, CliHelpScreen};
use Apex\App\Interfaces\Opus\CliCommandInterface;

/**
 * CLI Command -- ./apex my-shop recurring_payments
 */
class RecurringPayments implements CliCommandInterface
{

    /**
     * Process
     */
    public function process(Cli $cli, array $args):void
    {

        // Get CLI arguments
        $opt = $cli->getArgs(['myflag1', 'myflag2']);

        // Go through recurring payments
        $recurring = [];
        foreach ($recurring as $vars) {
            $process->charge($vars);
            $cli->info("Successfully charged payment for id# $vars[0] total of $vars[amount]\n");
    }

    }

    /**
     * Help
     */
    public function help(Cli $cli):CliHelpScreen
    {

        $help = new CliHelpScreen(
            title: 'Recurring Payments',
            usage: 'my-shop recurring_payments',
            description: 'Description of the command here'
        );

        // Add parameters
        $help->addParam('param1', 'Description of parameter.');

        // Add optional flags
        $help->addFlag('--some-flag', 'Description of flag.');

        // Add example
        $help->addExample('./apex my-shop recurring_payments <param1> [--flat1=...]');

        // Return
        return $help;
    }

}