thinkphp5 自定义命令行中input传值问题,官方文档只列出了如何生成命令行,但是针对input的传值没有找到任何说明文档,因此本文主要针对input的传值问题进行说明
<?php /**
*
*
* @author moshao <liangmenshang@gmail.com>
* @link http://gmail.com/
* @copyright 2018 moshao all rights reserved.
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
namespace appclicommand;
use thinkconsoleCommand;
use thinkconsoleInput;
use thinkconsoleInputArgument;
use thinkconsoleInputOption;
use thinkconsoleOutput;
use thinkDb;
use thinkConfig;
class Chapter extends Command {
protected $pernum = 100;
protected function configure() {
//设置参数
$this->addArgument('beginid', Argument::REQUIRED); //必传参数
$this->addArgument('endid', Argument::OPTIONAL);//可选参数
//选项定义
$this->addOption('message', 'm', Option::VALUE_REQUIRED, 'test'); //选项值必填
$this->addOption('status', 's', Option::VALUE_OPTIONAL, 'test'); //选项值选填
$this->setName('chapter')->setDescription('For ddshu spider');
}
protected function execute(Input $input, Output $output) {
//获取参数值
$args = $input->getArguments();
$output->writeln('The args value is:');
print_r($args);
//获取选项值
$options = $input->getOptions();
$output->writeln('The options value is:');
print_r($options);
$output->writeln("End Spider Command:" . date("Y-m-d H:i:s"));
}
然后传输方式如下
php think chapter 1 123 -m"2222" -s"4444"
注意针对参数的输入是按空格分开的,而针对选项值由于有带入-m或者-s的配置比较容易看出,以上的例子针对非必填项可以简化为
php think chapter 1 -m"2222"
如果你多填写一个就会提示错误,比如
php think chapter 1 123 111 -m"2222"
会提示
[RuntimeException]
Too many arguments.
© 版权声明
THE END
暂无评论内容