- 
                Notifications
    You must be signed in to change notification settings 
- Fork 48
v3 add command
        Inhere edited this page May 28, 2022 
        ·
        1 revision
      
    public function command(string $name, $handler = null, $option = null)- 
$handler一个可调用的callable或者 继承类Inhere\Console\Command
- 
$optionstring|array命令的选项信息,传入字符串则作为描述
callable可以是 闭包函数,具名函数,可回调的对象
如下所示,使用闭包可以快速的添加一个简单的命令
$app->command('demo', function (Input $in, Output $out) {
    $cmd = $in->getCommand();
    $out->info('hello, this is a test command: ' . $cmd);
}, 'this is message for the command');
$app->command('demo2', function (Input $in, Output $out) {
    $cmd = $in->getCommand();
    $out->info('hello, this is a test command: ' . $cmd);
}, [ // 使用数组可以配置更多信息
	'aliases' => ['alias-name'], // 配置别名
	'description' => 'this is message for the command',
]);通过继承 Inhere\Console\Command 添加独立命令
独立命令 - 只有一个命令可执行,跟
symfony/console的 command 一样
use Inhere\Console\Command;
/**
 * Class TestCommand
 * @package app\console\commands
 */
class TestCommand extends Command
{
    // 命令名称
    protected static $name = 'test';
    // 命令描述
    protected static $description = 'this is a test independent command';
    /**
     * @usage usage message
     * @arguments 
     *  arg     some message ...
     *  
     * @options 
     *  -o, --opt     some message ...
     *  
     * @param  Inhere\Console\IO\Input $input
     * @param  Inhere\Console\IO\Output $output
     * @return int
     */
    public function execute($input, $output)
    {
        $output->write('hello, this in ' . __METHOD__);
    }
}注释中的 @usage @arguments @options @example 在使用 帮助命令时,会被解析并显示出来
$ php example/app test
hello, this in Inhere\Console\Examples\Command\TestCommand::execute$ php example/app test -h
我的其他PHP项目
- inhere/kite 方便本地开发和使用的个人CLI工具应用
- php-toolkit/pflag PHP编写的,通用的命令行标志(选项和参数)解析库
- phppkg/easytpl 使用简单且快速的 PHP 模板引擎
- inhere/php-validate 一个简洁小巧且功能完善的php验证库
- inhere/sroute 轻量且快速的HTTP请求路由库