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