annotate vendor/psy/psysh/src/Psy/Command/HelpCommand.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2017 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Command;
Chris@0 13
Chris@0 14 use Symfony\Component\Console\Helper\TableHelper;
Chris@0 15 use Symfony\Component\Console\Input\InputArgument;
Chris@0 16 use Symfony\Component\Console\Input\InputInterface;
Chris@0 17 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Help command.
Chris@0 21 *
Chris@0 22 * Lists available commands, and gives command-specific help when asked nicely.
Chris@0 23 */
Chris@0 24 class HelpCommand extends Command
Chris@0 25 {
Chris@0 26 private $command;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * {@inheritdoc}
Chris@0 30 */
Chris@0 31 protected function configure()
Chris@0 32 {
Chris@0 33 $this
Chris@0 34 ->setName('help')
Chris@0 35 ->setAliases(array('?'))
Chris@0 36 ->setDefinition(array(
Chris@0 37 new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', null),
Chris@0 38 ))
Chris@0 39 ->setDescription('Show a list of commands. Type `help [foo]` for information about [foo].')
Chris@0 40 ->setHelp('My. How meta.');
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Helper for setting a subcommand to retrieve help for.
Chris@0 45 *
Chris@0 46 * @param Command $command
Chris@0 47 */
Chris@0 48 public function setCommand($command)
Chris@0 49 {
Chris@0 50 $this->command = $command;
Chris@0 51 }
Chris@0 52
Chris@0 53 /**
Chris@0 54 * {@inheritdoc}
Chris@0 55 */
Chris@0 56 protected function execute(InputInterface $input, OutputInterface $output)
Chris@0 57 {
Chris@0 58 if ($this->command !== null) {
Chris@0 59 // help for an individual command
Chris@0 60 $output->page($this->command->asText());
Chris@0 61 $this->command = null;
Chris@0 62 } elseif ($name = $input->getArgument('command_name')) {
Chris@0 63 // help for an individual command
Chris@0 64 $output->page($this->getApplication()->get($name)->asText());
Chris@0 65 } else {
Chris@0 66 // list available commands
Chris@0 67 $commands = $this->getApplication()->all();
Chris@0 68
Chris@0 69 $table = $this->getTable($output);
Chris@0 70
Chris@0 71 foreach ($commands as $name => $command) {
Chris@0 72 if ($name !== $command->getName()) {
Chris@0 73 continue;
Chris@0 74 }
Chris@0 75
Chris@0 76 if ($command->getAliases()) {
Chris@0 77 $aliases = sprintf('<comment>Aliases:</comment> %s', implode(', ', $command->getAliases()));
Chris@0 78 } else {
Chris@0 79 $aliases = '';
Chris@0 80 }
Chris@0 81
Chris@0 82 $table->addRow(array(
Chris@0 83 sprintf('<info>%s</info>', $name),
Chris@0 84 $command->getDescription(),
Chris@0 85 $aliases,
Chris@0 86 ));
Chris@0 87 }
Chris@0 88
Chris@0 89 $output->startPaging();
Chris@0 90 if ($table instanceof TableHelper) {
Chris@0 91 $table->render($output);
Chris@0 92 } else {
Chris@0 93 $table->render();
Chris@0 94 }
Chris@0 95 $output->stopPaging();
Chris@0 96 }
Chris@0 97 }
Chris@0 98 }