4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command;
14 use Symfony\Component\Console\Helper\TableHelper;
15 use Symfony\Component\Console\Input\InputArgument;
16 use Symfony\Component\Console\Input\InputInterface;
17 use Symfony\Component\Console\Output\OutputInterface;
22 * Lists available commands, and gives command-specific help when asked nicely.
24 class HelpCommand extends Command
31 protected function configure()
35 ->setAliases(array('?'))
36 ->setDefinition(array(
37 new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', null),
39 ->setDescription('Show a list of commands. Type `help [foo]` for information about [foo].')
40 ->setHelp('My. How meta.');
44 * Helper for setting a subcommand to retrieve help for.
46 * @param Command $command
48 public function setCommand($command)
50 $this->command = $command;
56 protected function execute(InputInterface $input, OutputInterface $output)
58 if ($this->command !== null) {
59 // help for an individual command
60 $output->page($this->command->asText());
61 $this->command = null;
62 } elseif ($name = $input->getArgument('command_name')) {
63 // help for an individual command
64 $output->page($this->getApplication()->get($name)->asText());
66 // list available commands
67 $commands = $this->getApplication()->all();
69 $table = $this->getTable($output);
71 foreach ($commands as $name => $command) {
72 if ($name !== $command->getName()) {
76 if ($command->getAliases()) {
77 $aliases = sprintf('<comment>Aliases:</comment> %s', implode(', ', $command->getAliases()));
83 sprintf('<info>%s</info>', $name),
84 $command->getDescription(),
89 $output->startPaging();
90 if ($table instanceof TableHelper) {
91 $table->render($output);
95 $output->stopPaging();