【问题标题】:Symfony: how to customize output of available commands?Symfony:如何自定义可用命令的输出?
【发布时间】:2015-06-30 20:25:35
【问题描述】:

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

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 为命令创建多个部分

或者,有没有办法改变命令标签的绿色颜色?

【问题讨论】:

    标签: symfony symfony-2.7 symfony-console


    【解决方案1】:

    您可以使用冒号创建新部分。

    $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'
            );
    

    但是,在这种情况下,您需要在运行命令时添加新的部分名称作为命名空间, &gt; php app.php newSection:greet Avindra.

    如果您使用空格命名您的部分,例如“New Section”,您需要调用您的命令,例如, &gt; php app.php "New Section:greet" Avindra.

    这就是您可以更改应用程序本身的info 注释颜色的方法。

    #!/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

    希望对你有帮助。

    【讨论】:

    • 谢谢,这绝对给了我一个开始的地方。这行代码看起来很有趣github.com/symfony/Console/blob/… 似乎没有一个真正干净的方法来挂钩它,但似乎我可以根据它的类型修改命令的颜色,至少。
    • 我认为您对 Textdescriptor 的看法是正确的。它是在这里github.com/symfony/Console/blob/… 发起的,它似乎是默认注册的。也许您可以尝试为应用程序 HelperSet 注册自己的 Descriptor 类。
    猜你喜欢
    • 2016-05-26
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多