4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Descriptor;
14 use Symfony\Component\Console\Application;
15 use Symfony\Component\Console\Command\Command;
16 use Symfony\Component\Console\Helper\Helper;
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputDefinition;
19 use Symfony\Component\Console\Input\InputOption;
22 * Markdown descriptor.
24 * @author Jean-François Simon <contact@jfsimon.fr>
28 class MarkdownDescriptor extends Descriptor
33 protected function describeInputArgument(InputArgument $argument, array $options = array())
36 '**'.$argument->getName().':**'."\n\n"
37 .'* Name: '.($argument->getName() ?: '<none>')."\n"
38 .'* Is required: '.($argument->isRequired() ? 'yes' : 'no')."\n"
39 .'* Is array: '.($argument->isArray() ? 'yes' : 'no')."\n"
40 .'* Description: '.preg_replace('/\s*[\r\n]\s*/', "\n ", $argument->getDescription() ?: '<none>')."\n"
41 .'* Default: `'.str_replace("\n", '', var_export($argument->getDefault(), true)).'`'
48 protected function describeInputOption(InputOption $option, array $options = array())
51 '**'.$option->getName().':**'."\n\n"
52 .'* Name: `--'.$option->getName().'`'."\n"
53 .'* Shortcut: '.($option->getShortcut() ? '`-'.implode('|-', explode('|', $option->getShortcut())).'`' : '<none>')."\n"
54 .'* Accept value: '.($option->acceptValue() ? 'yes' : 'no')."\n"
55 .'* Is value required: '.($option->isValueRequired() ? 'yes' : 'no')."\n"
56 .'* Is multiple: '.($option->isArray() ? 'yes' : 'no')."\n"
57 .'* Description: '.preg_replace('/\s*[\r\n]\s*/', "\n ", $option->getDescription() ?: '<none>')."\n"
58 .'* Default: `'.str_replace("\n", '', var_export($option->getDefault(), true)).'`'
65 protected function describeInputDefinition(InputDefinition $definition, array $options = array())
67 if ($showArguments = count($definition->getArguments()) > 0) {
68 $this->write('### Arguments:');
69 foreach ($definition->getArguments() as $argument) {
71 $this->write($this->describeInputArgument($argument));
75 if (count($definition->getOptions()) > 0) {
80 $this->write('### Options:');
81 foreach ($definition->getOptions() as $option) {
83 $this->write($this->describeInputOption($option));
91 protected function describeCommand(Command $command, array $options = array())
93 $command->getSynopsis();
94 $command->mergeApplicationDefinition(false);
97 $command->getName()."\n"
98 .str_repeat('-', Helper::strlen($command->getName()))."\n\n"
99 .'* Description: '.($command->getDescription() ?: '<none>')."\n"
101 .array_reduce(array_merge(array($command->getSynopsis()), $command->getAliases(), $command->getUsages()), function ($carry, $usage) {
102 return $carry.' * `'.$usage.'`'."\n";
106 if ($help = $command->getProcessedHelp()) {
111 if ($command->getNativeDefinition()) {
112 $this->write("\n\n");
113 $this->describeInputDefinition($command->getNativeDefinition());
120 protected function describeApplication(Application $application, array $options = array())
122 $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
123 $description = new ApplicationDescription($application, $describedNamespace);
125 $this->write($application->getName()."\n".str_repeat('=', Helper::strlen($application->getName())));
127 foreach ($description->getNamespaces() as $namespace) {
128 if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
129 $this->write("\n\n");
130 $this->write('**'.$namespace['id'].':**');
133 $this->write("\n\n");
134 $this->write(implode("\n", array_map(function ($commandName) {
135 return '* '.$commandName;
136 }, $namespace['commands'])));
139 foreach ($description->getCommands() as $command) {
140 $this->write("\n\n");
141 $this->write($this->describeCommand($command));