thinkphp 自定义命令传参
2023-03-29 23:03:03
133
{{single.collect_count}}
  • 第一步:还是要把自定义的命令配置到command.php里面去。具体操作见tp文档(传送门)
  • 第二步: 直接上代码
<?phpnamespace app\admin\command;use think\console\Command;use think\console\Input;use think\console\Input\Argument;use think\console\Input\Option;use think\console\Output;class Test extends Command{protected function configure(){// 命令行参数$this->addArgument('a', Argument::REQUIRED); // 必须参数$this->addArgument('b', Argument::REQUIRED); $this->addArgument('c', Argument::OPTIONAL); // 可选参数// 添加选项$this->addOption('type', 't', Option::VALUE_OPTIONAL, 'test'); // 可选选项//$this->addOption('type', 'q', Option::VALUE_REQUIRED, 'test'); // 必须选项$this->setName('test');$this->setDescription('测试一个计算器');}protected function execute(Input $input, Output $output){$type = $input->getOption('type');$a= $input->getArgument('a');$b= $input->getArgument('b');$c= $input->getArgument('c');if (null == $type) {if (null !== $c) {$result= ($a + $b) * $c;$message = '(a + b) * c = ' . $result;} else {$result= $a + $b;$message = 'a + b = ' . $result;}} else {switch ($type) {case '+':$message = 'a + b = ' . ($a + $b);break;case '_':$message = 'a - b = ' . ($a - $b);break;case '*':$message = 'a * b = ' . ($a * $b);break;case '/':$message = 'a / b = ' . ($a / $b);break;default:$message = '操作符错误';break;}}$output->writeLn($message);}}

测试结果

测试的时候需要切换到项目根目录!!!

  • php think test 10 20
    测试结果

  • php think test 10 20 30
    在这里插入图片描述

  • php think test 10 20 -t +
    在这里插入图片描述

  • php think test 10 20 -t _ (中横杠是关键字)
    在这里插入图片描述

  • php think test 10 20 -t *
    在这里插入图片描述

  • php think test 10 20 -t
    在这里插入图片描述

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}