New in Symfony 2.2: Better interaction from the Console
December 14, 2012 • Published by Fabien Potencier
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Symfony is not just a web framework. Symfony is also a set of components that solve challenges faced by developers in their day-to-day activities. The Console component is one of those not-web-related Symfony component that help you create beautiful command line program with ease. In Symfony 2.2, the Console component was improved, mostly to enhance the interaction with the users.
Displaying a Progress bar for long running tasks
When executing longer-running commands from the CLI, giving some feedback to the user as your command runs is helpful. Symfony 2.2 provide a progress bar helper that does all the work for you:
1 2 3 4 5 6 7 8 9 10 11 12
$progress = $app->getHelperSet()->get('progress');
$progress->start($output, 50);
$i = 0;
while ($i++ < 50) {
// do some work
// advance the progress bar 1 unit
$progress->advance();
}
$progress->finish();
And here is how it is displayed to the user:
Of course, everything is customizable as explained in the docs.
Hiding passwords given from the CLI
If you want to ask for a password from the CLI, you'd better hide what the
user types. As of Symfony 2.2, there is a convenient way to hide what the
users enters on the command line, via the new askHiddenResponse
method:
1 2
$dialog = $this->getHelperSet()->get('dialog');
$password = $dialog->askHiddenResponse($output, 'What is the database password?');
Asking the user to choose from a list of choices
There are many ways to ask for some information on the CLI. As of Symfony 2.2,
you can even restrict what the user can enter via the new select()
helper.
The following code demonstrates some the possibilities you have now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
$app = new Application();
$app->register('ask-color')->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$dialog = $app->getHelperSet()->get('dialog');
$colors = array('red', 'blue', 'yellow');
// simply ask for a color (no validation)
$color = $dialog->ask($output, 'Enter your favorite color (default to red): ', 'red');
$output->writeln('You have just entered: '.$color);
// ask and validate the answer
$color = $dialog->askAndValidate($output, 'Enter your favorite color (default to red): ', function ($color) use ($colors) {
if (!in_array($color, array_values($colors))) {
throw new \InvalidArgumentException(sprintf('Color "%s" is invalid.', $color));
}
return $color;
}, false, 'red');
$output->writeln('You have just entered: '.$color);
// force the user to select from a pre-defined list of choices
$color = $dialog->select($output, 'Please select your favorite color (default to red)', $colors, 0);
$output->writeln('You have just selected: '.$colors[$color]);
});
$app->run();
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.