首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony:如何定制可用命令的输出?

Symfony:如何定制可用命令的输出?
EN

Stack Overflow用户
提问于 2015-06-30 20:25:35
回答 1查看 837关注 0票数 1

我们正在使用symfony/console (顺便说一句伟大的库)构建一个控制台应用程序。可用的命令如下所示:

代码语言:javascript
复制
Available commands:
  check-deps     Get a report of resolved and missing (if any) dependencies.
  gen-docs       Rebuild the API / code documentation
  help           Displays help for a command
  list           Lists commands
  restart        Restart the Nginx and PHP-FPM processes.
  show-changes   Show all local changes to the source code since the last push.
  test           Run the unit tests
  test-coverage  Run the unit tests and include a coverage report.

命令的名称显示在绿色中,描述以白色显示。

目前,可用命令是唯一的部分。是否有一种使用OOP为命令创建多个部分的简单方法?

或者,是否有一种方法可以更改命令标签的绿色颜色?

EN

回答 1

Stack Overflow用户

发布于 2015-06-30 22:41:56

您可以使用冒号符号创建一个新节。

代码语言:javascript
复制
$this
        ->setName('newSection:greet') //<--- This line does the trick
        ->setDescription('Greet someone')
        ->addArgument(
            'name',
            InputArgument::OPTIONAL,
            'Who do you want to greet?'
        )
        ->addOption(
           'yell',
           null,
           InputOption::VALUE_NONE,
           'If set, the task will yell in uppercase letters'
        );

但是,在这种情况下,您需要使用添加为命名空间> php app.php newSection:greet Avindra的新节名来运行命令。

如果使用类似于“新建区段”的空格命名区段,则需要调用命令like,> php app.php "New Section:greet" Avindra

这就是如何更改应用程序本身的info注释的颜色。

代码语言:javascript
复制
#!/usr/bin/env php
<?php
require __DIR__.'/vendor/autoload.php';

use Command\GreetCommand;
use Command\HelloCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;

$application = new Application();
$application->add(new GreetCommand());
$application->add(new HelloCommand());

//Create a new OutputFormatter
$formatter = new OutputFormatter();
//Change info annotation color by blue
$formatter->setStyle('info', new OutputFormatterStyle('blue'));
//Construct output interface with new formatter
$output = new ConsoleOutput(OutputInterface::VERBOSITY_NORMAL, null, $formatter);

//Run your application with your new output interface
$application->run(null, $output);

您可以在这里查看相关的源代码以获得更多选项;https://github.com/symfony/Console/blob/5f241906889f0a3e7b1854b42e7c92a0ea8516ce/Formatter/OutputFormatter.php#L51

https://github.com/symfony/Console/blob/b6b351d326e2fb2fe673a808630f938c2881a473/Formatter/OutputFormatterStyle.php#L21

希望能帮上忙。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31147888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档