1

We are building a console application using symfony/console (great library by the way). The available commands show up as such:

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.

The name of the command shows up in green and the description shows up in white.

Currently, Available commands is the only section. Is there a simple way using OOP to create multiple sections for commands?

Alternatively, is there a way to change the green color for the command label?

1 Answer 1

2

You can create a new section by using colon notation.

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

However in that case you need to run your command with new section name added as namespace, > php app.php newSection:greet Avindra.

If you name your section with a whitespace like "New Section" you need to call your command like, > php app.php "New Section:greet" Avindra.

And this is how you can change the color of info annotation of the application itself.

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

You can check the related source code for more options here; https://github.com/symfony/Console/blob/5f241906889f0a3e7b1854b42e7c92a0ea8516ce/Formatter/OutputFormatter.php#L51

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

Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this definitely gives me a place to start. This line of code seems interesting github.com/symfony/Console/blob/… There doesn't seem to be a really clean way to hook into it, but it seems that I can modify the color of the command depending on it's type, at least.
I think you are right about Textdescriptor. It's initiated here github.com/symfony/Console/blob/… and it seems like it's registered by default. Maybe you can try to register your own Descriptor class for the applications HelperSet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.