PHP Luminova: CLI Commands Input Object
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
$inputproperty.You can use it directly to access the executed command’s arguments and options—there’s no need to create a newInputinstance.
Usages
Parsed Global Command
Simulate a command executed in CLI:
php /public/index.php blog list --limit=5 --no-headerAutomatically parse executed command into input object using parseCommands method in Terminal class:
use Luminova\Command\Input;
use Luminova\Command\Terminal;
$input = new Input(Terminal::parseCommands(
$_SERVER['argv'] ?? $argv
));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 --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 without 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',
// 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'); // 5Controller 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=redYou can handle input in a console controller as follows:
// /app/Console/ConsoleCommand.php
use Luminova\Base\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=redYou can access the input in your Base Command controller:
// /app/Controllers/Cli/RoutableCommand.php
use Luminova\Base\Command;
use Luminova\Attributes\Route;
use Luminova\Attributes\Group;
#[Group(name: 'example')]
class RoutableCommand extends Command
{
#[Route('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->inputobject 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 __construct(array $command)Parameters:
| Parameter | Type | Description |
|---|---|---|
$command | array<string, mixed> | The parsed executed command. |
Command Options:
[
'command' => string, // Original CLI input (without PHP binary)
'name' => string, // Resolved command name.
'group' => string, // Command group namespace.
'arguments' => string[], // Raw positional arguments (e.g. ['limit=2'])
'options' => array<string,mixed>, // Named options (e.g. ['no-header' => null])
'input' => string, // Full executable command string
'params' => string[], // positional arguments values
];replace
Replace the command input with new one.
public replace(array $command): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$command | array<string, mixed> | The parsed executed command. |
Return Value:
Luminova\Command\Input - 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 hasOption(string $option, ?string $alias = null): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$option | string | The option name (e.g. verbose or --verbose). |
$alias | string|null | Optional short alias (e.g. v or -v). |
Return Value:
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 vale is returned based on argument index.
Supports negative indexes:
1=> last param2=> second last, etc.
public getParam(int $index = 0): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$index | int | Index of the param to retrieve (0-based). Negative indexes count from the end. |
Return Value:
mixed - Returns the argument param value, or null if not found.
getArgument
Get command argument by index.
Supports negative indexes:
1=> last argument2=> second last, etc.
public getArgument(int $index = 0, bool $value = true): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$index | int | Index of the argument to retrieve (0-based). Negative indexes count from the end. |
$value | bool | Wether to return arguments value or raw string (default: true).When true, it allows you to extract arguments value directly instead of calling getParam method. |
Return Value:
mixed - Returns the argument, or null if not found.
Note:Both
getParamandgetArgumentshared same index for consistency.
getArguments
Get all command arguments.
public getArguments(): arrayReturn Value:
array - Return command arguments.
getCommand
Get executed command name.
Alias getName()
public getCommand(): ?stringReturn Value:
string|null - Return the command name.
getGroup
Get executed command group namespace.
public getGroup(): ?stringReturn Value:
string|null - Return the command group name.
getInput
Get raw executed command string.
public getInput(): ?stringReturn Value:
string|null - Return the full passed command, options and arguments.
getOptions
Returns the array of executed command option key values.
public getOptions(): arrayReturn Value:
array - 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 getOption(string $key, mixed $default = false): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | Option key to retrieve. |
$default | mixed | Default value to return (default: false). |
Return Value:
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 getAnyOption(string $key, string $alias, mixed $default = false): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | Option name to retrieve. |
$alias | string | Option short-name alias to retrieve. if main key is not found. |
$default | mixed | Default value to return (default: false). |
Return Value:
mixed - Return option value, true if empty value, otherwise default value.
Get Any Option Example
Assume you have a command, such as php index.php my-command --which=a.If you want to also accept an alias of --which as -w, then getAnyOption is what you need.
$which = $input->getAnyOption('which', 'w');
echo $which;In the above example,
getAnyOption('which', 'w')checks for both--whichand-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 getVerbose(
string $short = 'v',
string $long = 'verbose',
int $verbosity = 3,
int $default = 0
): intParameters:
| Parameter | Type | Description |
|---|---|---|
$short | string | The short flag key (default: v). |
$long | string | The long flag alias (default: verbose). |
$verbosity | int | The maximum verbosity level (default: 3). |
$default | int | Default verbose level (default: 0). |
Return Value:
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 0getMethod
Returns the command controller class method name.
public getMethod(): ?stringReturn Value:
string|null - Return the command controller class method name, otherwise 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 getEntry(string $name): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string | The option key name (e.g, group, arguments, command, name, options, input). |
Return Value:
mixed - Return command option entry data.
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 getArray(): arrayReturn Value:
array<string,mixed> - Return an associative array of the entire command information.
isHelp
Determine if the command is a help command.
public isHelp(): boolReturn Value:
bool - Returns true if the command is --help or its alias -h, false otherwise.
isNovakit
Determine if the command was executed via novakit.
public isNovakit(): boolReturn Value:
bool - Returns true if the command starts with novakit, false otherwise.
isDryRun
Determine if the command is a dry-run.
public isDryRun(): boolReturn Value:
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 isNoHeader(): boolReturn Value:
bool - Returns true if --no-header is present, false otherwise.
isNoColor
Determine if the command was run with the 'no-color' option.
public isNoColor(): boolReturn Value:
bool - Returns true if --no-color is present, false otherwise.
isNoAnsi
Determine if the command was run with the 'no-ansi' option.
public isNoAnsi(): boolReturn Value:
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 isVersion(): boolReturn Value:
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 isSystemInfo(): boolReturn Value:
bool - Returns true if the command includes --system-info, false otherwise.