adachsoft / console-internal
Ultra-lightweight internal CLI commands framework with \ prefix
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Forks: 0
pkg:composer/adachsoft/console-internal
Requires
- php: >=8.3
- adachsoft/collection: >=2.1.0
- adachsoft/console-io: *
Requires (Dev)
- adachsoft/php-code-style: ^0.2.0
- friendsofphp/php-cs-fixer: ^3.89
- phpstan/extension-installer: *
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.0
This package is not auto-updated.
Last update: 2025-12-02 09:34:56 UTC
README
Ultra-lightweight internal CLI commands framework with \ prefix.
Installation
composer require adachsoft/console-internal adachsoft/console-io
Quick Start
<?php
use AdachSoft\ConsoleInternal\InternalCli;
use AdachSoft\ConsoleInternal\Builtin\HelpCommand;
use AdachSoft\ConsoleInternal\Builtin\ExitCommand;
$cli = new InternalCli();
$cli->registerCommand('help', 'List available commands', new HelpCommand($cli));
$cli->registerCommand('exit', 'Exit interactive session', new ExitCommand());
// Example usage
$line = "\\help";
try {
$parsed = $cli->parse($line);
$result = $cli->execute($parsed);
echo $result->output;
if ($result->shouldExit) {
exit($result->exitCode);
}
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
Features
- Ultra-lightweight: Minimal dependencies and simple API
- Framework-agnostic: Works with any PHP application
- Extensible: Easy to add new commands
- Flag support: Short (-v) and long (--verbose) flags
- Positional arguments: Support for command arguments
- Double dash separator:
--treats everything after as positional arguments - Built-in commands:
\helpand\exit
Command Syntax
\command [flags] [arguments...]
Flags
- Short:
-a,-abc(multiple flags),-n value - Long:
--verbose,--name=value,--name value - Boolean:
--flag(true if present)
Examples
\help
\exit 42
\deploy --env=production --force
\test -v --filter pattern file1 file2
\command -- --this-is-positional
Creating Custom Commands
<?php
use AdachSoft\ConsoleInternal\CommandInterface;
use AdachSoft\ConsoleInternal\Dto\ParsedCommandDto;
use AdachSoft\ConsoleInternal\Dto\ExecuteResultDto;
class MyCommand implements CommandInterface
{
public function run(ParsedCommandDto $parsed): ExecuteResultDto
{
$name = $parsed->flags->get('name')?->value ?? 'World';
return new ExecuteResultDto("Hello, {$name}!\n");
}
}
$cli->registerCommand('hello', 'Say hello', new MyCommand());
API Reference
InternalCli
registerCommand(string $name, string $description, CommandInterface $command): voidparse(string $input): ParsedCommandDtoexecute(ParsedCommandDto $parsed): ExecuteResultDtogetRegisteredCommands(): array
CommandInterface
run(ParsedCommandDto $parsed): ExecuteResultDto
DTOs
ParsedCommandDto: command name, positional arguments, flagsExecuteResultDto: output, shouldExit flag, exit code
Requirements
- PHP >= 8.2
- adachsoft/collection >= 2.1.0
- adachsoft/console-io
Testing
composer test
License
MIT