Skip to content

Internals Cli CLIHandler

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

cli\CLIHandler

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/cli/CLIHandler.php · Snapshot 8411f4287706.

The CLI registers command objects and executes one command, then returns to a readline loop. Help and clear have special handling in the loop.

Imports and dependencies

use cli\interfaces\CLICommand;

CLIHandler

Declaration and architectural role

class CLIHandler

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$commands private array $commands = []; Stores commands in format: "command" => CLICommand @var CLICommand[]

Method index

Method Signature
parseInput public function parseInput(string $input): array
addCommand public function addCommand(CLICommand $command): void
getCommands public function getCommands(): array
execute public function execute(string $command, array $args = []): void
help public function help(?string $command = null): void
getGlobalHelp private function getGlobalHelp(): string

parseInput

Source line 12

public function parseInput(string $input): array

Parameters

Parameter Type, modifiers and default Purpose
$input string $input Operation input; the exact use, validation and propagation are shown in the implementation trace.

Parse interactive input without expanding variables or executing shell syntax.

Return expressions (including local callbacks): $parts.

Explicit exceptions: \InvalidArgumentException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
/** Parse interactive input without expanding variables or executing shell syntax. */
    public function parseInput(string $input): array
    {
        $parts = [];
        $value = '';
        $quote = null;
        $started = false;
        $length = strlen($input);

        for ($i = 0; $i < $length; ++$i) {
            $char = $input[$i];
            if ($char === '\\' && $quote !== "'") {
                if ($i + 1 === $length) {
                    throw new \InvalidArgumentException('Incomplete escape at end of command.');
                }
                $next = $input[$i + 1];
                if ($quote === '"' && $next !== '"' && $next !== '\\') {
                    $value .= $char;
                } else {
                    $value .= $input[++$i];
                }
                $started = true;
            } elseif ($quote !== null) {
                if ($char === $quote) {
                    $quote = null;
                } else {
                    $value .= $char;
                }
            } elseif ($char === '"' || $char === "'") {
                $quote = $char;
                $started = true;
            } elseif (ctype_space($char)) {
                if ($started) {
                    $parts[] = $value;
                    $value = '';
                    $started = false;
                }
            } else {
                $value .= $char;
                $started = true;
            }
        }

        if ($quote !== null) {
            throw new \InvalidArgumentException('Unclosed quote in command.');
        }
        if ($started) {
            $parts[] = $value;
        }
        return $parts;
    }

addCommand

Source line 71

public function addCommand(CLICommand $command): void

Parameters

Parameter Type, modifiers and default Purpose
$command CLICommand $command Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->commands[$command->getCommand()].

Implementation, validation guards and interaction order
public function addCommand(CLICommand $command): void
    {
        $this->commands[$command->getCommand()] = $command;
    }

getCommands

Source line 82

public function getCommands(): array

Returns list of registered command names

@return string[]

Return expressions (including local callbacks): array_keys($this->commands).

Implementation, validation guards and interaction order
/**
     * Returns list of registered command names
     *
     * @return string[]
     */
    public function getCommands(): array
    {
        return array_keys($this->commands);
    }

execute

Source line 95

public function execute(string $command, array $args = []): void

Parameters

Parameter Type, modifiers and default Purpose
$command string $command * @param array $args
$args array $args = [] Ordered arguments passed to the command or method.

Execute a registered command

@param string $command @param array $args @return void

Delegates to getGlobalHelp().

Implementation, validation guards and interaction order
/**
     * Execute a registered command
     *
     * @param string $command
     * @param array $args
     * @return void
     */
    public function execute(string $command, array $args = []): void
    {
        if (!isset($this->commands[$command])) {
            echo "Unknown command: {$command}\n";
            echo $this->getGlobalHelp();
            return;
        }


        $cmd = $this->commands[$command];
        $cmd->execute($command, $args);
        $output = $cmd->result();

        if ($output !== '') {
            echo $output . PHP_EOL;
        }
    }

help

Source line 120

public function help(?string $command = null): void

Parameters

Parameter Type, modifiers and default Purpose
$command ?string $command = null * @return void

Print help for a specific command or all commands

@param string|null $command @return void

Delegates to getGlobalHelp().

Implementation, validation guards and interaction order
/**
     * Print help for a specific command or all commands
     *
     * @param string|null $command
     * @return void
     */
    public function help(?string $command = null): void
    {
        if ($command === null) {
            echo $this->getGlobalHelp();
            return;
        }


        if (!isset($this->commands[$command])) {
            echo "No such command: {$command}\n";
            return;
        }


        $args = "";

        foreach ($this->commands[$command]->getArguments() as $arg => $value) {
            $args .= "\n{$arg}: {$value}";
        }


        echo $this->commands[$command]->getHelp() . PHP_EOL . "Arguments: {$args} ". PHP_EOL;
    }

getGlobalHelp

Source line 145

private function getGlobalHelp(): string

Return expressions (including local callbacks): implode(PHP_EOL, $lines) . PHP_EOL.

Implementation, validation guards and interaction order
private function getGlobalHelp(): string
    {
        $lines = ['Available commands:'];
        foreach ($this->commands as $name => $cmd) {
            $lines[] = sprintf(" %-15s %s", $name, strtok($cmd->getHelp(), "\n"));
        }


        $lines[] = "\nRun: php awt <command> [args...]";
        $lines[] = "Use 'help <command>' for full command help.";


        return implode(PHP_EOL, $lines) . PHP_EOL;
    }

Related types

Clone this wiki locally