How to add sub command on php console app

  laravel, php

so i have a simple php app console which looks like:

Calculator 0.5

Usage:
  command [options] [arguments]

Options:
  ## option section

Available commands:
  add        Add all given Numbers
  divide     Divide all given Numbers
  help       Displays help for a command
  list       Lists commands
  multiply   Multiply all given Numbers
  pow        Exponent all given Number
  substract  Substract all given Numbers

i want to add sub command so the available commands section will be like this:

    Available commands:
      add        Add all given Numbers
      divide     Divide all given Numbers
      help       Displays help for a command
      list       Lists commands
      multiply   Multiply all given Numbers
      pow        Exponent all given Number
      substract  Substract all given Numbers
   history
      history:list
      history:clear

this is how i create the app :
app.php
#!/usr/bin/env php

<?php

use IlluminateConsoleApplication;
use IlluminateContainerContainer;
use IlluminateEventsDispatcher;
use SymfonyComponentConsoleInputArgvInput;
use SymfonyComponentConsoleOutputConsoleOutput;

try {
    require_once __DIR__.'/vendor/autoload.php';

    $container = new Container();
    $dispatcher = new Dispatcher();
    $app = new Application($container, $dispatcher, '0.5');
    $app->setName('Calculator');
    $appConfig = require_once __DIR__.'/config/app.php';
    $providers = $appConfig['providers'];

    foreach ($providers as $provider) {
        $container->make($provider)->register($container);
    }

    $commands = require_once __DIR__.'/commands.php';
    $commands = collect($commands)
        ->map(
            function ($command) use ($app) {
                return $app->getLaravel()->make($command);
            }
        )
        ->all()
    ;

    $app->addCommands($commands);

    $app->run(new ArgvInput(), new ConsoleOutput());
} catch (Throwable $e) {
    echo $e;
}

$command is basically list of command classes that extends IlluminateConsoleCommand;
i want to add sub command with colon, anyone can help me?

Source: Laravel

Leave a Reply