senhung / command-line-interface
此包已被弃用且不再维护。没有建议的替代包。
一个PHP脚本命令设置器
v0.4.1
2018-04-29 04:27 UTC
Requires
- php: >=7.1.0
This package is not auto-updated.
Last update: 2020-01-24 17:42:28 UTC
README
描述
一个模仿 Artisan Console 的包,以便更容易编写命令。
设置
- 添加依赖项
$ composer require senhung/command-line-interface
- 为命令添加条目
在根目录中创建一个文件,并将其命名为您想调用的命令
例如,创建一个名为 example 的文件;调用命令行将是
$ php example <command>
- 编辑命令条目
打开刚刚创建的命令条目文件并添加以下内容
<?php require_once 'vendor/autoload.php'; use Senhung\CLI\CommandEntry; /* Read through all commands in <change-folder-to-your-command-folder> */ CommandEntry::load('<change-folder-to-your-command-folder>'); /* When calling the script, execute the target command */ CommandEntry::entry($argv);
用法
创建命令
打开命令文件夹并创建一个PHP脚本
<?php namespace Some\Name\Space; use Senhung\CLI\Command; class YourClassName extends Command { /** * @var string $signature * * Set the signature of your command * * <command> how you call the command * {:arguments} arguments will be filled in in order when call * {--options} options are like flags or parameters */ public $signature = '<command> {:arguments} {--options}'; /** * @var string $description * * Description of your command * * Will be used in help command */ public $description = 'Description for <command>'; /** * The executing function when calling the command */ public function handle() { /** * Write your handling function here * * You can get argument by: $this->getArgument('<argument-key>') * You can get option by: $this->getOption('<option-key>') */ } }
默认值
您可以通过设置签名来为参数和选项设置默认值
public $signature = '<command> {:arguments=some-default-value} {--options=some-default-value}';
调用命令
您可以通过以下方式调用您的命令
$ php <entry-file-name> <command> <argument> <--opiton-as-a-flag> <--option-as-a-param=some-value>
您可以混合使用参数和选项,包将识别参数/选项
注意:参数的顺序很重要,但选项的顺序不重要
帮助
调用以下命令以获取所有命令
$ php <entry-file-name> help
您还可以添加一个额外的参数以获取特定功能的描述
$ php <entry-file-name> help <command>
示例
示例条目
<?php /* file: example */ require_once 'vendor/autoload.php'; use Senhung\CLI\CommandEntry; CommandEntry::load('Commands'); CommandEntry::entry($argv);
示例命令
<?php /* file: Commands/Greet.php */ namespace Example; use Senhung\CLI\Command; class Greet extends Command { protected $signature = 'greet {:name} {--with-exclamation} {--number-of-times=1}'; protected $description = 'Greet people'; public function handle() { /* Repeat Greeting Times */ $numberOfTimes = $this->getOption('number-of-times'); /* Get Greeting Person's Name */ $name = $this->getArgument('name'); /* Check If Using Exclamation Point */ $withExclamation = $this->getOption('with-exclamation'); /* Repeat $numberOfTimes Times */ for ($index = 0; $index < $numberOfTimes; $index++) { $greet = "Hello " . $name; if ($withExclamation) { $greet .= "!"; } $greet .= "\n"; echo $greet; } } }
使用命令
带参数
$ php example greet Senhung
输出
Hello Senhung
注意:如果没有传递参数,{:name} 将为空
带选项(标志)
$ php example greet Senhung --with-exclamation
输出
Hello Senhung!
带选项(参数)
$ php example greet Senhung --with-exclamation --number-of-times=3
输出
Hello Senhung!
Hello Senhung!
Hello Senhung!