alesinicio / clicommand
该包最新版本(1.0.0)没有提供许可证信息。
1.0.0
2024-01-28 18:01 UTC
Requires
- php: ~8.1
- psr/container: ~1.0.0|~2.0.0
- psr/log: ~2.0.0|~3.0.0
This package is auto-updated.
Last update: 2024-09-28 19:36:33 UTC
README
此仓库允许您为您的应用程序创建CLI命令/处理器。
操作非常简单:创建实现Alesinicio\CLICommand\Commands\CLICommandInterface
接口的类,注册一个绑定到该类的命令,然后大功告成!
示例
//Hello.php
<?php
namespace Your\Namespace;
class Hello implements Alesinicio\CLICommand\Commands\CLICommandInterface {
public function run(mixed ...$args) : void {
echo sprintf("Hello %s!\n", $args[0] ?? 'noname');
}
}
//yourDependencyInjectionRegistry.php
<?php
$commandCollection = new Alesinicio\CLICommand\CommandCollection();
$commandCollection
->addCommand('hello', Your\Namespace\Hello::class)
;
//command.php
<?php
global $argc, $argv;
$di = loadYourDependencyInjectionContainer();
try {
$di->get(Alesinicio\CLICommand\CLICommandHandler::class))->handle($argc, $argv);
} catch (Exception $e) {}
现在您可以使用以下格式调用您的command.php
脚本
php command.php <command> <argument0> <argument1> ... <argumentN>`
$ php command.php hello master
Hello master!
当然,设置完全由您自己决定。您还可以通过shebang将PHP入口点设置为可执行!