| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 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\Completion\CompletionInput; |
| 15 |
use Symfony\Component\Console\Completion\CompletionSuggestions; |
| 16 |
use Symfony\Component\Console\Descriptor\ApplicationDescription; |
| 17 |
use Symfony\Component\Console\Helper\DescriptorHelper; |
| 18 |
use Symfony\Component\Console\Input\InputArgument; |
| 19 |
use Symfony\Component\Console\Input\InputInterface; |
| 20 |
use Symfony\Component\Console\Input\InputOption; |
| 21 |
use Symfony\Component\Console\Output\OutputInterface; |
| 22 |
|
| 23 |
/** |
| 24 |
* ListCommand displays the list of all available commands for the application. |
| 25 |
* |
| 26 |
* @author Fabien Potencier <fabien@symfony.com> |
| 27 |
*/ |
| 28 |
class ListCommand extends Command |
| 29 |
{ |
| 30 |
/** |
| 31 |
* {@inheritdoc} |
| 32 |
*/ |
| 33 |
protected function configure() |
| 34 |
{ |
| 35 |
$this |
| 36 |
->setName('list') |
| 37 |
->setDefinition([ |
| 38 |
new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'), |
| 39 |
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'), |
| 40 |
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'), |
| 41 |
new InputOption('short', null, InputOption::VALUE_NONE, 'To skip describing commands\' arguments'), |
| 42 |
]) |
| 43 |
->setDescription('List commands') |
| 44 |
->setHelp(<<<'EOF' |
| 45 |
The <info>%command.name%</info> command lists all commands: |
| 46 |
|
| 47 |
<info>%command.full_name%</info> |
| 48 |
|
| 49 |
You can also display the commands for a specific namespace: |
| 50 |
|
| 51 |
<info>%command.full_name% test</info> |
| 52 |
|
| 53 |
You can also output the information in other formats by using the <comment>--format</comment> option: |
| 54 |
|
| 55 |
<info>%command.full_name% --format=xml</info> |
| 56 |
|
| 57 |
It's also possible to get raw list of commands (useful for embedding command runner): |
| 58 |
|
| 59 |
<info>%command.full_name% --raw</info> |
| 60 |
EOF |
| 61 |
) |
| 62 |
; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* {@inheritdoc} |
| 67 |
*/ |
| 68 |
protected function execute(InputInterface $input, OutputInterface $output) |
| 69 |
{ |
| 70 |
$helper = new DescriptorHelper(); |
| 71 |
$helper->describe($output, $this->getApplication(), [ |
| 72 |
'format' => $input->getOption('format'), |
| 73 |
'raw_text' => $input->getOption('raw'), |
| 74 |
'namespace' => $input->getArgument('namespace'), |
| 75 |
'short' => $input->getOption('short'), |
| 76 |
]); |
| 77 |
|
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
| 82 |
{ |
| 83 |
if ($input->mustSuggestArgumentValuesFor('namespace')) { |
| 84 |
$descriptor = new ApplicationDescription($this->getApplication()); |
| 85 |
$suggestions->suggestValues(array_keys($descriptor->getNamespaces())); |
| 86 |
|
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
if ($input->mustSuggestOptionValuesFor('format')) { |
| 91 |
$helper = new DescriptorHelper(); |
| 92 |
$suggestions->suggestValues($helper->getFormats()); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|