Luminova Framework

PHP Luminova: CLI Controller's Commands Inputs Object

Last updated: 2026-11-28 20:57:12

Access command arguments and options in Luminova with the `Command\Input` class. Works with Base Console and Base Command controllers for CLI tools.

The Luminova's Command Input Object class provides a consistent way to access command arguments and options in both Base Console and Base Command controllers. It allows you to:

  • Retrieve arguments by position
  • Access named options or aliases
  • Handle input for routed or direct CLI commands

This class simplifies building flexible CLI tools by giving a single interface to read all command input, no matter how the command is executed.

Note:

Both Base Console and Base Command controllers already provide a pre-initialized $input property.You can use it directly to access the executed command’s arguments and options—there’s no need to create a new Input instance.


Usages

Parsed Global Command

A routed command receives its parsed input through the controller's $input property.

php public/index.php blog list param=value --limit=5 --no-header

Use the manual initialization example below when you already have parsed command data.


Manual Initialization

Manually parse an array of command structure to input object:

use Luminova\Command\Input;

$input = new Input([
    // Original CLI input without the PHP binary
    'command'   => 'blog list param=value --limit=5 --no-header',

    // Resolved command name
    'name'      => 'list',

    // Command group namespace
    'group'     => 'blog',

    // Positional arguments
    'arguments' => ['param=value'],

    // Named options (option => value). Use null for flags with no value
    'options'   => [
        'limit' => 5,
        'no-header' => null,
    ],

    // Full executed command string including PHP binary
    'input'     => 'php /public/index.php blog list param=value --limit=5 --no-header',

    'argv'      => ['public/index.php', 'blog', 'list', 'param=value', '--limit=5', '--no-header'],

    // Parsed parameter values (e.g., values extracted from arguments)
    'params'    => ['value'],
]);

Access Input

Accessing values manually:

$commandName = $input->getCommand();       // 'list'
$group       = $input->getGroup();         // 'blog'
$arg         = $input->getArgument(0, false);     // 'param=value'
$argsVal     = $input->getParam(0);        // 'value'
$noHeader    = $input->getOption('no-header'); // true
$limit       = $input->getOption('limit');  // 5

Controller Examples

These examples show how the command input object can be used in both Base Console and Base Command controller classes.


Base Console Command

For a command executed like:

php novakit command --message=Hello -c=red

You can handle input in a console controller as follows:

// /app/Console/ConsoleCommand.php

use Luminova\Base\Cli\AbstractConsole as Console;

class ConsoleCommand extends Console
{
    public function run(?array $options = []): int
    {
        // Access the command name
        // This can be used as routing
        $command = $this->input->getCommand();

        // Access argument by position
        $arg1 = $this->input->getArgument(1);

        // Access named option
        $message = $this->input->getOption('message');

        // Access named option or alias
        $color = $this->input->getAnyOption('color', 'c');

        return STATUS_SUCCESS;
    }
}

Routable Base Command

For a command defined in a routable controller, like:

php public/index.php example hello --message=Hello -c=red

You can access the input in your Base Command controller:

// /app/Controllers/Cli/RoutableCommand.php

use Luminova\Base\Cli\AbstractController as Command;
use Luminova\Attributes\Command as CommandRoute;
use Luminova\Attributes\Group;

#[Group(name: 'example')]
class RoutableCommand extends Command
{
    #[CommandRoute('hello', group: 'example')]
    public function helloMessage(): int
    {
        // Access named option
        $message = $this->input->getOption('message');

        // Access named option or alias
        $color = $this->input->getAnyOption('color', 'c');

        return STATUS_SUCCESS;
    }
}

Tip:

Both Base Console and Base Command use the same $this->input object to handle command arguments and options, but Base Command integrates with routing, making it ideal for structured CLI controllers.


Class Definition

  • Class namespace: Luminova\Command\Input

Methods

constructor

Create a command input object.

public function __construct(array $command, array $knownOptions = [])

Parameters:

ParameterTypeDescription
$commandarray{group?: string, name?: string, command?: string, input?: string, classMethod?: string, arguments?: string[], options?: array<string, mixed>, argv?: string[], params?: string[]}The parsed executed command.
$knownOptionsstring[]Option names and aliases to recognize when checking for unknown options.

replace

Replace the command input with new one.

public function replace(array $command): self

Parameters:

ParameterTypeDescription
$commandarray<string, mixed>The parsed executed command.

Return:

self - Returns instance of command input.


hasOption

Determine if an option was provided in the command input.

Supports both long (--option) and short (-o) forms.

public function hasOption(string $option, ?string $alias = null): bool

Parameters:

ParameterTypeDescription
$optionstringThe option name (e.g. 'verbose' or '--verbose').
$alias?stringOptional short alias (e.g. 'v' or '-v').

Return:

bool - Returns true if the option or its alias exists, false otherwise.


getParam

Get command argument param value by index.

This method holds the extracted values from arguments, it shares same array index with getArgument ensuring correct value is returned based on argument index.

Supports negative indexes:

  • -1 => last param
  • -2 => second last, etc.
public function getParam(int $index = 0): mixed

Parameters:

ParameterTypeDescription
$indexintIndex of the param to retrieve (0-based). Negative indexes count from the end.

Return:

mixed - Returns the argument param value, or null if not found.


getArgument

Get command argument by index.

Supports negative indexes:

  • -1 => last argument
  • -2 => second last, etc.
public function getArgument(int $index = 0, bool $value = true): mixed

Parameters:

ParameterTypeDescription
$indexintIndex of the argument to retrieve (0-based). Negative indexes count from the end.
$valueboolWhether to return arguments value or full arguments raw string (default: true).

Return:

mixed - Returns the argument, or null if not found.

Note:When the input includes extracted parameters, both methods use the same index.


getArguments

Get all command arguments.

public function getArguments(): array

Return:

string[] - Return command arguments.


getCommand

Get executed command name.

Alias getName()

public function getCommand(): ?string

Return:

string|null - Return the command name.


getGroup

Get executed command group namespace.

public function getGroup(): ?string

Return:

string|null - Return the command group name.


getInput

Get raw executed command string.

public function getInput(): ?string

Return:

string|null - Return the full passed command, options and arguments.


getOptions

Returns the array of executed command option key values.

public function getOptions(): array

Return:

array<string, mixed> - Return array of executed command options.


getOption

Get options value from command arguments.

If option key is passed with an empty value true will be return otherwise the default value.

public function getOption(string $key, mixed $default = false): mixed

Parameters:

ParameterTypeDescription
$keystringOption key to retrieve.
$defaultmixedDefault value to return (default: false).

Return:

mixed - Return option value, true if empty value, otherwise default value.


getAnyOption

Get options value from command arguments using either the main option name or an alias.

This method allows you to retrieve a flag or arguments value, using The full name --option or short alias -o for lookup.

public function getAnyOption(string $key, string $alias, mixed $default = false): mixed

Parameters:

ParameterTypeDescription
$keystringOption key to retrieve.
$aliasstringOption key alias to retrieve. if main key is not found.
$defaultmixedDefault value to return (default: false).

Return:

mixed - Return option value, true if empty value, otherwise default value.

In the above example, getAnyOption('which', 'w') checks for both --which and -w, allowing you to use either option in your command.


getVerbose

Get the verbosity level from CLI options.

This method checks for both short (-v, -vv, -vvv)and long (--verbose or --verbose=<level>) flags.Returns an integer level between 0 (silent) and max $verbosity (most verbose).

Level meaning:

  • 0 = Silent or default mode
  • 1 = Verbose
  • 2 = More verbose
  • 3 = Debug-level verbosity
  • n = More-level verbosity
public function getVerbose(
    string $short = 'v',
    string $long = 'verbose',
    int $verbosity = 3,
    int $default = 0
): int

Parameters:

ParameterTypeDescription
$shortstringThe short flag key (default: v).
$longstringThe long flag alias (default: verbose).
$verbosityintThe maximum verbosity level (default: 3).
$defaultintDefault verbose level (default: 0).

Return:

int - Returns the verbosity level between 0 and $verbosity or default if not specified.

Example:

In Code:

$verbose = $input->getVerbose(verbosity: 5, default: 0);

In Command:

php novakit script -v           # returns 1
php novakit script -vv          # returns 2
php novakit script -vvv         # returns 3
php novakit script --verbose=2  # returns 2
php novakit script              # returns default level 0

getMethod

Returns the command controller class method name.

public function getMethod(): ?string

Return:

string|null - Return the command controller class method or null.


getEntry

Gets a single command-line input entry by name.

This method returns an executed command entry information by the array index key, if it doesn't exists return null.

public function getEntry(string $name): mixed

Parameters:

ParameterTypeDescription
$namestringThe command entry name (e.g, group, arguments, command, name, options, input).

Return:

mixed - Return command option query value.


getArray

Returns the entire command associative that was executed.

This is a complied array of executed command details which has been processed for final use.

public function getArray(): array

Return:

array<string, mixed> - Return an associative array of the entire command information


isHelp

Determine if the command is a help command.

public function isHelp(): bool

Return:

bool - Returns true if the command is 'help' or its alias 'h', false otherwise.


isNovakit

Determine if the command was executed via novakit.

public function isNovakit(): bool

Return:

bool - Returns true if the command starts with 'novakit', false otherwise.


isDryRun

Determine if the command is a dry-run.

public function isDryRun(): bool

Return:

bool - Returns true if the command includes the 'dry-run' option, false otherwise.


isNoHeader

Determine if the command was run with the 'no-header' option.

public function isNoHeader(): bool

Return:

bool - Returns true if '--no-header' is present, false otherwise.


isNoColor

Determine if the command was run with the 'no-color' option.

public function isNoColor(): bool

Return:

bool - Returns true if '--no-color' is present, false otherwise.


isNoAnsi

Determine if the command was run with the 'no-ansi' option.

public function isNoAnsi(): bool

Return:

bool - Returns true if '--no-ansi' is present, false otherwise.


isVersion

Determine if the command was run with the version option.

Checks for --version or its short alias --v.

public function isVersion(): bool

Return:

bool - Returns true if the command includes '--version' or '--v', false otherwise.


isSystemInfo

Determine if the command was run with the 'system-info' option.

Checks for --system-info only (no short alias defined).

public function isSystemInfo(): bool

Return:

bool - Returns true if the command includes '--system-info', false otherwise.


setKnownOptions

Set the known command options.

public function setKnownOptions(array $options): self

Parameters:

ParameterTypeDescription
$optionsstring[]Option names (e.g, verbose).

Return:

self - Returns instance of command input.


hasUnknownOption

Check if the command contains any unknown option or flag.

public function hasUnknownOption(?string &$unknown = null): bool

Parameters:

ParameterTypeDescription
$unknown?stringThe found unknown option parsed by reference.

Return:

bool - True if an unknown option is found, false otherwise.


getArgv

Gets the command-line arguments provided to the command input.

public function getArgv(): array

Return:

string[] - Returns the command-line arguments.


isAsyncCommand

Checks whether the input represents an asynchronous worker command.

public function isAsyncCommand(): bool

Return:

bool - Returns true if the input is an async command, otherwise false.


getName

Get executed command name.

public function getName(): ?string

Return:

string|null - Return the command name.