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 {
27 * Label for PsySH commands.
29 const PSYSH_CATEGORY = 'PsySH commands';
32 * The currently set subcommand.
34 * @var \Symfony\Component\Console\Command\Command
41 protected function configure() {
46 new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', NULL),
48 ->setDescription('Show a list of commands. Type `help [foo]` for information about [foo].');
52 * Helper for setting a subcommand to retrieve help for.
54 * @param \Symfony\Component\Console\Command\Command $command
56 public function setCommand(Command $command) {
57 $this->command = $command;
63 protected function execute(InputInterface $input, OutputInterface $output) {
64 if ($this->command !== NULL) {
65 // Help for an individual command.
66 $output->page($this->command->asText());
67 $this->command = NULL;
69 elseif ($name = $input->getArgument('command_name')) {
70 // Help for an individual command.
71 $output->page($this->getApplication()->get($name)->asText());
76 // List available commands.
77 $commands = $this->getApplication()->all();
79 // Find the alignment width.
81 foreach ($commands as $command) {
82 $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
86 foreach ($commands as $name => $command) {
87 if ($name !== $command->getName()) {
91 if ($command->getAliases()) {
92 $aliases = sprintf(' <comment>Aliases:</comment> %s', implode(', ', $command->getAliases()));
98 if ($command instanceof DrushCommand) {
99 $category = (string) $command->getCategory();
102 $category = static::PSYSH_CATEGORY;
105 if (!isset($categories[$category])) {
106 $categories[$category] = [];
109 $categories[$category][] = sprintf(" <info>%-${width}s</info> %s%s", $name, $command->getDescription(), $aliases);
114 foreach ($categories as $name => $category) {
116 $messages[] = sprintf('<comment>%s</comment>', OutputFormatter::escape($name));
117 foreach ($category as $message) {
118 $messages[] = $message;
122 $output->page($messages);