adachsoft/console-internal
最新稳定版本:v0.1.0
Composer 安装命令:
composer require adachsoft/console-internal
包简介
Ultra-lightweight internal CLI commands framework with \ prefix
README 文档
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
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-17