[自定义应用命令行]

第一步,我们以demo应用为例创建demo:hello命令行,在app/demo/command目录里创建Hello.php文件

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Released under the MIT License.
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\demo\command;
  12. use think\console\Command;
  13. use think\console\Input;
  14. use think\console\input\Argument;
  15. use think\console\input\Option;
  16. use think\console\Output;
  17. class Hello extends Command
  18. {
  19. protected function configure()
  20. {
  21. $this->setName('demo:hello')
  22. ->addArgument('name', Argument::OPTIONAL, "your name")
  23. ->addOption('city', '-c', Option::VALUE_REQUIRED, 'city name')
  24. ->setDescription('Say App Hello');
  25. }
  26. protected function execute(Input $input, Output $output)
  27. {
  28. $name = $input->getArgument('name');
  29. $name = $name ? $name : 'ThinkCMF';
  30. $city = $input->getOption('city');
  31. $city = $city ? $city : 'China';
  32. $output->writeln("Hello, My name is " . $name . '! I\'m from ' . $city);
  33. }
  34. }

这个文件定义了一个叫demo:hello的命令,并设置了一个name参数和一个city选项。

第二步,我们创建命令行配置文件app/demo/command.php,并添加如下内容

  1. <?php
  2. return [
  3. // 指令名 =》完整的类名
  4. 'demo:hello' => 'app\demo\command\Hello'
  5. ];

第三步,测试-命令帮助-命令行下运行

  1. php think

输出

  1. ThinkPHP v6.0.12LTS & ThinkCMF v6.0.5
  2. Usage:
  3. command [options] [arguments]
  4. Options:
  5. -h, --help Display this help message
  6. -V, --version Display this console version
  7. -q, --quiet Do not output any message
  8. --ansi Force ANSI output
  9. --no-ansi Disable ANSI output
  10. -n, --no-interaction Do not ask any interactive question
  11. -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  12. Available commands:
  13. clear Clear runtime file
  14. cli List lightweight CLI commands
  15. help Displays help for a command
  16. list Lists commands
  17. run PHP Built-in Server for ThinkPHP
  18. version Show ThinkPHP & ThinkCMF version
  19. demo
  20. demo:hello Say App Hello
  21. migrate
  22. migrate:create Create a new migration
  23. migrate:run Execute database migration
  24. plugin
  25. plugin:hello Say Plugin Hello
  26. publish
  27. publish:app Publish a ThinkCMF app
  28. publish:plugin Publish a ThinkCMF plugin
  29. publish:theme Publish a ThinkCMF theme
  30. service
  31. service:discover Discover Services for ThinkPHP
  32. vendor
  33. vendor:publish Publish any publishable assets from vendor packages

第四步,运行demo:hello命令

  1. php think demo:hello

输出

  1. Hello, My name is ThinkCMF! I'm from China

添加命令参数

  1. php think demo:hello catman

输出

  1. Hello, My name is catman! I'm from China

添加city选项

  1. php think demo:hello --city Shanghai

输出

  1. Hello, My name is ThinkCMF! I'm from Shanghai