Console class

Framework\CLI\Console

Class Console.

Defined in libraries/cli/src/Console.php

Properties #

$commands
List of commands.
$command
The current command name.
$options
Input options.
$arguments
Input arguments.
$language
The Language instance.

$commands

protected array $commands = array ( )

List of commands.

$command

protected string $command = ''

The current command name.

$options

protected array $options = array ( )

Input options.

$arguments

protected array $arguments = array ( )

Input arguments.

$language

protected \Framework\Language\Language $language

The Language instance.

Methods #

__construct()
Console constructor.
addCommand()
Add a command to the console.
addCommands()
Add many commands to the console.
commandToArgs()
-
exec()
-
getArgument()
Get a specific argument or null.
getArguments()
Get all arguments.
getCommand()
Get an active command.
getCommands()
Get a list of active commands.
getLanguage()
Get the Language instance.
getOption()
Get a specific option or null.
getOptions()
Get all CLI options.
hasCommand()
Tells if it has a command.
removeCommand()
Remove a command.
removeCommands()
Remove commands.
run()
Run the Console.
setLanguage()
Set the Language instance.
prepare()
Prepare information of the command line.
reset()
-
setDefaultCommands()
-

__construct()

public function __construct(?\Framework\Language\Language $language = NULL)

Console constructor.

Parameters
  • ?\Framework\Language\Language $language

addCommand()

public function addCommand(\Framework\CLI\Command|string $command): static

Add a command to the console.

Parameters
  • \Framework\CLI\Command|string $command - A Command instance or the class FQN
Returns static

addCommands()

public function addCommands(array $commands): static

Add many commands to the console.

Parameters
  • array $commands - A list of Command instances or the classes FQN
Returns static

commandToArgs()

public static function commandToArgs(string $command): array
Parameters
  • string $command
Returns array

exec()

public function exec(string $command): void
Parameters
  • string $command
Returns void

getArgument()

public function getArgument(int $position): ?string

Get a specific argument or null.

Parameters
  • int $position - Argument position, starting from zero
Returns ?string - The argument value or null if it was not set

getArguments()

public function getArguments(): array

Get all arguments.

Returns array

getCommand()

public function getCommand(string $name): ?\Framework\CLI\Command

Get an active command.

Parameters
  • string $name - Command name
Returns ?\Framework\CLI\Command - The Command on success or null if not found

getCommands()

public function getCommands(): array

Get a list of active commands.

Returns array

getLanguage()

public function getLanguage(): \Framework\Language\Language

Get the Language instance.

Returns \Framework\Language\Language

getOption()

public function getOption(string $option): string|bool|null

Get a specific option or null.

Parameters
  • string $option
Returns string|bool|null - The option value as string, TRUE if it was passed without a value or NULL if the option was not set

getOptions()

public function getOptions(): array

Get all CLI options.

Returns array

hasCommand()

public function hasCommand(string $name): bool

Tells if it has a command.

Parameters
  • string $name - Command name
Returns bool

removeCommand()

public function removeCommand(string $name): static

Remove a command.

Parameters
  • string $name - Command name
Returns static

removeCommands()

public function removeCommands(array $names): static

Remove commands.

Parameters
  • array $names - Command names
Returns static

run()

public function run(): void

Run the Console.

Returns void

setLanguage()

public function setLanguage(?\Framework\Language\Language $language = NULL): static

Set the Language instance.

Parameters
  • ?\Framework\Language\Language $language
Returns static

prepare()

protected function prepare(array $argumentValues): void

Prepare information of the command line.

[options] [arguments] [options]
[options] -- [arguments]
[command]
[command] [options] [arguments] [options]
[command] [options] -- [arguments]
Short option: -l, -la === l = true, a = true
Long option: --list, --all=vertical === list = true, all = vertical
Only Long Options receive values:
--foo=bar or --f=bar - "foo" and "f" are bar
-foo=bar or -f=bar - all characters are true (f, o, =, b, a, r)
After -- all values are arguments, also if is prefixed with -
Without --, arguments and options can be mixed: -ls foo -x abc --a=e.

Parameters
  • array $argumentValues
Returns void

reset()

protected function reset(): void
Returns void

setDefaultCommands()

protected function setDefaultCommands(): static
Returns static