Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

v3 add group

Inhere edited this page May 28, 2022 · 1 revision

添加命令组

当一些命令相关性较大时,写在同一个文件里更方便阅读和管理。

通过继承 Inhere\Console\Controller 添加一组命令. 即是命令行的控制器,里面可以编写多个命令。

use Inhere\Console\Controller;
/**
 * default command controller. there are some command usage examples
 */
class HomeController extends Controller
{
 // 命令组名称
 protected static $name = 'home';
 // 命令组描述
 protected static $description = 'default command controller. there are some command usage examples';
 /**
 * this is a command's description message, <cyan>color text</cyan>
 * the second line text
 * @usage {command} [arg ...] [--opt ...]
 * @arguments
 * arg1 argument description 1
 * the second line
 * a2,arg2 argument description 2
 * the second line
 * @options
 * -s, --long option description 1
 * --opt option description 2
 * @example example text one
 * the second line example
 */
 public function testCommand()
 {
 $this->write('hello, welcome!! this is ' . __METHOD__);
 }
 
 /**
 * a example for use color text output on command
 */
 public function otherCommand()
 {
 $this->write('hello, welcome!! this is ' . __METHOD__);
 }
}

效果预览

查看命令组帮助: php examples/app home

group-command-list

查看子命令帮助

当使用 php examples/app home:test -h 时,可以查看到关于 HomeController::testCommand 更详细的信息

group-command-list

设置别名

命令组别名

 /**
 * @return array|string[]
 */
 public static function aliases(): array
 {
 return ['alias1', 'alias2'];
 }

看到一些命令最后的 [alias: ...] 了吗,那是此命令拥有的别名.

即用别名也可以访问它,当一个命令太长时可以加别名以方便使用。并且一个命令可以拥有多个别名,注意不能添加相同别名。

通过注释定义参数选项等信息

命令上的注释是可被解析的

  • 注释中的 @usage @arguments @options @example 在使用帮助命令时,会被解析并显示出来
  • 注释里面同样支持带颜色的文本输出 eg: this is a command's description <info>message</info>
  • 上述注释tag里,支持变量替换(例如: {command} 会自动替换为当前输入的命令)
  • 当你使用 php examples/app home -h 时,可以查看到 HomeController 的所有命令描述注释信息

通过代码定义参数选项等信息

每个子命令的选项、参数也可以通过代码里来定义,不使用注解。

说明:子命令 defArg 的命令方法为 defArgCommand 那它的配置方法就是 {COMMAND}+ConfiguredefArgConfigure

class HomeController extends Controller
{
 protected static $name = 'home';
 protected static $description = 'This is a demo command controller. there are some command usage examples(2)';
 /**
 * command `defArgCommand` config
 * @throws LogicException
 */
 protected function defArgConfigure(): void
 {
 $this->createDefinition()
 ->setDescription('the command arg/opt config use defined configure, it like symfony console: argument define by position')
 ->addArgument('name', Input::ARG_REQUIRED, "description for the argument 'name'")
 ->addOption('yes', 'y', Input::OPT_BOOLEAN, "description for the option 'yes'")
 ->addOption('opt1', null, Input::OPT_REQUIRED, "description for the option 'opt1'");
 }
 /**
 * the command arg/opt config use defined configure, it like symfony console: argument define by position
 */
 public function defArgCommand(): void
 {
 $this->output->dump($this->input->getArgs(), $this->input->getOpts(), $this->input->getBoolOpt('y'));
 }
}

如上,就可以给命令组里每个子命令添加配置信息(描述、参数、选项等)

一些说明

更多请查看 examples 目录中的示例代码和在目录下运行示例 php examples/app home 来查看效果

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /