| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 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 Symfony\Component\Console\Command; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Descriptor\ApplicationDescription; |
| 15 |
use Symfony\Component\Console\Helper\DescriptorHelper; |
| 16 |
use Symfony\Component\Console\Input\InputArgument; |
| 17 |
use Symfony\Component\Console\Input\InputInterface; |
| 18 |
use Symfony\Component\Console\Input\InputOption; |
| 19 |
use Symfony\Component\Console\Output\OutputInterface; |
| 20 |
|
| 21 |
/** |
| 22 |
* HelpCommand displays the help for a given command. |
| 23 |
* |
| 24 |
* @author Fabien Potencier <[email protected]> |
| 25 |
*/ |
| 26 |
class HelpCommand extends Command |
| 27 |
{ |
| 28 |
private Command $command; |
| 29 |
|
| 30 |
protected function configure(): void |
| 31 |
{ |
| 32 |
$this->ignoreValidationErrors(); |
| 33 |
|
| 34 |
$this |
| 35 |
->setName('help') |
| 36 |
->setDefinition([ |
| 37 |
new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help', fn () => array_keys((new ApplicationDescription($this->getApplication()))->getCommands())), |
| 38 |
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt', static fn () => (new DescriptorHelper())->getFormats()), |
| 39 |
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command help'), |
| 40 |
]) |
| 41 |
->setDescription('Display help for a command') |
| 42 |
->setHelp(<<<'EOF' |
| 43 |
The <info>%command.name%</info> command displays help for a given command: |
| 44 |
|
| 45 |
<info>%command.full_name% list</info> |
| 46 |
|
| 47 |
You can also output the help in other formats by using the <info>--format</info> option: |
| 48 |
|
| 49 |
<info>%command.full_name% --format=xml list</info> |
| 50 |
|
| 51 |
To display the list of available commands, please use the <info>list</info> command. |
| 52 |
EOF |
| 53 |
) |
| 54 |
; |
| 55 |
} |
| 56 |
|
| 57 |
public function setCommand(Command $command): void |
| 58 |
{ |
| 59 |
$this->command = $command; |
| 60 |
} |
| 61 |
|
| 62 |
protected function execute(InputInterface $input, OutputInterface $output): int |
| 63 |
{ |
| 64 |
$this->command ??= $this->getApplication()->find($input->getArgument('command_name')); |
| 65 |
|
| 66 |
$helper = new DescriptorHelper(); |
| 67 |
$helper->describe($output, $this->command, [ |
| 68 |
'format' => $input->getOption('format'), |
| 69 |
'raw_text' => $input->getOption('raw'), |
| 70 |
]); |
| 71 |
|
| 72 |
unset($this->command); |
| 73 |
|
| 74 |
return 0; |
| 75 |
} |
| 76 |
} |