irfantoor / command
Irfan的命令:使用php的Shell命令
0.6
2021-12-18 19:41 UTC
Requires
- php: >= 7.3
- irfantoor/debug: ~0.6
- irfantoor/terminal: ~0.1
Requires (Dev)
- irfantoor/mini-loader: ~0.1
README
制作控制台命令的最简单方式。
注意:由于Irfan的终端(irfantoor/terminal)的加入,您的命令也可以通过浏览器运行。
安装
$ composer require irfantoor/command
示例
一个简单的命令,在命令行上输出“Hello World!”。默认的详细程度、版本和帮助选项默认定义。因此您可以这样做
$ php hello1.php --help $ php hello1.php -V $ php hello1.php -v
注意:如果在初始化时未提供版本,则在包含/src目录的父目录中搜索名为'version'的文件,如果找到则使用其内容作为版本,否则使用默认值'0.1'。如果版本文件是版本系统的一部分并且更新到最后一个版本标签,则这很有用(参见:examples/hello6.php)
参考:examples/hello1.php
<?php require dirname(__DIR__) . "/vendor/autoload.php"; use IrfanTOOR\Command; $cmd = new Command( [ 'name' => 'hello1.php', 'description' => 'hello world! of command', 'handler' => function($cmd){ $name = $cmd->read('your name: ', 'green'); $cmd->writeln("Hello " . ucwords($name) . '!', "yellow"); }, 'version' => '1.1' ] ); $cmd->run();
以下是一个复合命令的更复杂示例
在这里,greeting选项是可选的,而name操作数是必需的。
例
$ php hello4.php hello world! $ php hello4.php hello -g=Hi irfan $ php hello4.php hello --greeting="Hi there," buddy
参考:examples/hello4.php
<?php require dirname(__DIR__) . "/vendor/autoload.php"; use IrfanTOOR\Command; use IrfanTOOR\Debug; Debug::enable(1); class HelloCommand extends Command { function __construct() { parent::__construct([ 'name' => 'hello', 'description' => 'hello world! of command', 'version' => '1.3', ]); } function init() { $this->addOption('g|greeting', 'Sets the greeting', 'Hello'); $this->addArgument('name', 'Name to be greeted', self::ARGUMENT_OPTIONAL, 'World!'); } function main() { $greeting = $this->getOption('greeting'); $name = ucfirst($this->getArgument('name')); $this->writeln($greeting . " " . $name, "yellow"); } } class CalCommand extends Command { function __construct() { parent::__construct([ 'name' => 'cal', 'description' => 'prints a calendar', 'version' => '0.1' ]); } function main() { ob_start(); system("cal"); $output = ob_get_clean(); $this->writeln($output, "yellow"); } } $cmd = new Command([ 'name' => 'hello4', 'description' => "Composit command", 'version' => '1.4', 'handler' => function($cmd) { } ]); $cmd->addCommand('hello', HelloCommand::class); $cmd->addCommand('cal', CalCommand::class); $cmd->run();
如果选项的默认值未提供,则假定其为整数,每个选项的出现都会将其当前值增加1,例如:command -v # 详细程度 = 1 command -vv # 详细程度 = 2
如果选项的默认值是字符串,则每次使用此类选项时都必须紧跟其值。例如,使用greeting "Hi"的hello4.php可以这样使用
php hello4.php hello -g Hi irfan # or php hello4.php hello --greeting "Hay you! cheers" man
参数可以解析为URL中的字符串,也可以通过POST发送
$ php hello4.php -h # http://localhost:8000/hello4.php?args=-h $ php hello4.php hello -g Hi irfan # args as string: # http://localhost:8000/hello4.php?args=hello%20-g%20Hi%20irfan # args as array # http://localhost:8000/hello4.php?args[]=hello&args[]=-g&args[]=Hi&args[]=irfan # args must be used as array, when args values has spaces e.g.: # http://localhost:8000/hello4.php?args[]=hello&args[]=-g&args[]=Hay%20You!&args[]=young%20man
例
$ php hello5.php | Error: Missing argument: required $ php hello5.php -h hello 2.0 test the required argument usage: hello [options] [--] <required> options: -h, --help Displays help -V, --version Displays version --ansi force ANSI outupt --no-ansi disable ANSI output -v, --verbose Adds verbosity arguments: required its a required argument [required] $ php hello5.php Hello OK the required argument is: Hello