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