5 * Contains \Drush\Psysh\DrushCommand.
10 use Psy\Command\Command as BaseCommand;
11 use Symfony\Component\Console\Command\Command;
12 use Symfony\Component\Console\Formatter\OutputFormatter;
13 use Symfony\Component\Console\Input\InputArgument;
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Output\OutputInterface;
20 * Lists available commands, and gives command-specific help when asked nicely.
22 * This replaces the PsySH help command to list commands by category.
24 class DrushHelpCommand extends BaseCommand
28 * Label for PsySH commands.
30 const PSYSH_CATEGORY = 'PsySH';
33 * The currently set subcommand.
35 * @var \Symfony\Component\Console\Command\Command
42 protected function configure()
48 new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', null),
50 ->setDescription('Show a list of commands. Type `help [foo]` for information about [foo].');
54 * Helper for setting a subcommand to retrieve help for.
56 * @param \Symfony\Component\Console\Command\Command $command
58 public function setCommand(Command $command)
60 $this->command = $command;
66 protected function execute(InputInterface $input, OutputInterface $output)
68 if ($this->command !== null) {
69 // Help for an individual command.
70 $output->page($this->command->asText());
71 $this->command = null;
72 } elseif ($name = $input->getArgument('command_name')) {
73 // Help for an individual command.
74 $output->page($this->getApplication()->get($name)->asText());
78 // List available commands.
79 $commands = $this->getApplication()->all();
81 // Find the alignment width.
83 foreach ($commands as $command) {
84 $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
88 foreach ($commands as $name => $command) {
89 if ($name !== $command->getName()) {
93 if ($command->getAliases()) {
94 $aliases = sprintf(' <comment>Aliases:</comment> %s', implode(', ', $command->getAliases()));
100 if ($command instanceof DrushCommand) {
101 $namespace = $command->getNamespace();
104 if (empty($namespace)) {
105 $namespace = static::PSYSH_CATEGORY;
108 if (!isset($namespaces[$namespace])) {
109 $namespaces[$namespace] = [];
112 $namespaces[$namespace][] = sprintf(" <info>%-${width}s</info> %s%s", $name, $command->getDescription(), $aliases);
117 foreach ($namespaces as $namespace => $command_messages) {
119 $messages[] = sprintf('<comment>%s</comment>', OutputFormatter::escape($namespace));
120 foreach ($command_messages as $command_message) {
121 $messages[] = $command_message;
125 $output->page($messages);