PHP Luminova: CLI Controller's Commands Inputs 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
A routed command receives its parsed input through the controller's $input property.
php public/index.php blog list param=value --limit=5 --no-headerUse 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'); // 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\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=redYou 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->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 function __construct(array $command, array $knownOptions = [])Parameters:
| Parameter | Type | Description |
|---|---|---|
$command | array{group?: string, name?: string, command?: string, input?: string, classMethod?: string, arguments?: string[], options?: array<string, mixed>, argv?: string[], params?: string[]} | The parsed executed command. |
$knownOptions | string[] | Option names and aliases to recognize when checking for unknown options. |
replace
Replace the command input with new one.
public function replace(array $command): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$command | array<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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$option | string | The option name (e.g. 'verbose' or '--verbose'). |
$alias | ?string | Optional 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): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$index | int | Index 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): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$index | int | Index of the argument to retrieve (0-based). Negative indexes count from the end. |
$value | bool | Whether 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(): arrayReturn:
string[] - Return command arguments.
getCommand
Get executed command name.
Alias getName()
public function getCommand(): ?stringReturn:
string|null - Return the command name.
getGroup
Get executed command group namespace.
public function getGroup(): ?stringReturn:
string|null - Return the command group name.
getInput
Get raw executed command string.
public function getInput(): ?stringReturn:
string|null - Return the full passed command, options and arguments.
getOptions
Returns the array of executed command option key values.
public function getOptions(): arrayReturn:
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): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | Option key to retrieve. |
$default | mixed | Default 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): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | Option key to retrieve. |
$alias | string | Option key alias to retrieve. if main key is not found. |
$default | mixed | Default 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--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 function 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:
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 function getMethod(): ?stringReturn:
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): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$name | string | The 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(): arrayReturn:
array<string, mixed> - Return an associative array of the entire command information
isHelp
Determine if the command is a help command.
public function isHelp(): boolReturn:
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(): boolReturn:
bool - Returns true if the command starts with 'novakit', false otherwise.
isDryRun
Determine if the command is a dry-run.
public function isDryRun(): boolReturn:
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(): boolReturn:
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(): boolReturn:
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(): boolReturn:
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(): boolReturn:
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(): boolReturn:
bool - Returns true if the command includes '--system-info', false otherwise.
setKnownOptions
Set the known command options.
public function setKnownOptions(array $options): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$options | string[] | 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$unknown | ?string | The 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(): arrayReturn:
string[] - Returns the command-line arguments.
isAsyncCommand
Checks whether the input represents an asynchronous worker command.
public function isAsyncCommand(): boolReturn:
bool - Returns true if the input is an async command, otherwise false.
getName
Get executed command name.
public function getName(): ?stringReturn:
string|null - Return the command name.