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
12namespace Symfony\Component\Console\Command;
13
14use Symfony\Component\Console\Completion\CompletionInput;
15use Symfony\Component\Console\Completion\CompletionSuggestions;
16use Symfony\Component\Console\Descriptor\ApplicationDescription;
17use Symfony\Component\Console\Helper\DescriptorHelper;
18use Symfony\Component\Console\Input\InputArgument;
19use Symfony\Component\Console\Input\InputInterface;
20use Symfony\Component\Console\Input\InputOption;
21use 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 */
28class 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'
45The <info>%command.name%</info> command lists all commands:
46
47  <info>%command.full_name%</info>
48
49You can also display the commands for a specific namespace:
50
51  <info>%command.full_name% test</info>
52
53You 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
57It's also possible to get raw list of commands (useful for embedding command runner):
58
59  <info>%command.full_name% --raw</info>
60EOF
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