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();
Nice additions, well done all!
i really like the progress bar thing
Very nice! Tiny point -- shouldn't 41/50 be 82% in the ProgressBar example?
Really cool features!!
the progress bar is very nice :)
Great job, thank you.
Love these new blog posts! It helps a lot to follow the enhancing of the whole framework.
Really great addition. thanx !
Great additional features ! :) thanks
This is really awesome, can't wait to try...
like that! looks cool.
Nice features, this will surely help command line tools to be easy to use. Does anybody knows if composer project plans to have this progress bar feature?
Thanks for theses additions. Good work.
Already tried and it's great! Congrats!
Great feature!
Works in terminal window, not so great on putty yet