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;
20 use Symfony\Component\Console\Output\OutputInterface;
23 * Markdown descriptor.
25 * @author Jean-François Simon <contact@jfsimon.fr>
29 class MarkdownDescriptor extends Descriptor
34 public function describe(OutputInterface $output, $object, array $options = array())
36 $decorated = $output->isDecorated();
37 $output->setDecorated(false);
39 parent::describe($output, $object, $options);
41 $output->setDecorated($decorated);
47 protected function write($content, $decorated = true)
49 parent::write($content, $decorated);
55 protected function describeInputArgument(InputArgument $argument, array $options = array())
58 '#### `'.($argument->getName() ?: '<none>')."`\n\n"
59 .($argument->getDescription() ? preg_replace('/\s*[\r\n]\s*/', "\n", $argument->getDescription())."\n\n" : '')
60 .'* Is required: '.($argument->isRequired() ? 'yes' : 'no')."\n"
61 .'* Is array: '.($argument->isArray() ? 'yes' : 'no')."\n"
62 .'* Default: `'.str_replace("\n", '', var_export($argument->getDefault(), true)).'`'
69 protected function describeInputOption(InputOption $option, array $options = array())
71 $name = '--'.$option->getName();
72 if ($option->getShortcut()) {
73 $name .= '|-'.str_replace('|', '|-', $option->getShortcut()).'';
77 '#### `'.$name.'`'."\n\n"
78 .($option->getDescription() ? preg_replace('/\s*[\r\n]\s*/', "\n", $option->getDescription())."\n\n" : '')
79 .'* Accept value: '.($option->acceptValue() ? 'yes' : 'no')."\n"
80 .'* Is value required: '.($option->isValueRequired() ? 'yes' : 'no')."\n"
81 .'* Is multiple: '.($option->isArray() ? 'yes' : 'no')."\n"
82 .'* Default: `'.str_replace("\n", '', var_export($option->getDefault(), true)).'`'
89 protected function describeInputDefinition(InputDefinition $definition, array $options = array())
91 if ($showArguments = count($definition->getArguments()) > 0) {
92 $this->write('### Arguments');
93 foreach ($definition->getArguments() as $argument) {
95 $this->write($this->describeInputArgument($argument));
99 if (count($definition->getOptions()) > 0) {
100 if ($showArguments) {
101 $this->write("\n\n");
104 $this->write('### Options');
105 foreach ($definition->getOptions() as $option) {
106 $this->write("\n\n");
107 $this->write($this->describeInputOption($option));
115 protected function describeCommand(Command $command, array $options = array())
117 $command->getSynopsis();
118 $command->mergeApplicationDefinition(false);
121 '`'.$command->getName()."`\n"
122 .str_repeat('-', Helper::strlen($command->getName()) + 2)."\n\n"
123 .($command->getDescription() ? $command->getDescription()."\n\n" : '')
125 .array_reduce(array_merge(array($command->getSynopsis()), $command->getAliases(), $command->getUsages()), function ($carry, $usage) {
126 return $carry.'* `'.$usage.'`'."\n";
130 if ($help = $command->getProcessedHelp()) {
135 if ($command->getNativeDefinition()) {
136 $this->write("\n\n");
137 $this->describeInputDefinition($command->getNativeDefinition());
144 protected function describeApplication(Application $application, array $options = array())
146 $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
147 $description = new ApplicationDescription($application, $describedNamespace);
148 $title = $this->getApplicationTitle($application);
150 $this->write($title."\n".str_repeat('=', Helper::strlen($title)));
152 foreach ($description->getNamespaces() as $namespace) {
153 if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
154 $this->write("\n\n");
155 $this->write('**'.$namespace['id'].':**');
158 $this->write("\n\n");
159 $this->write(implode("\n", array_map(function ($commandName) use ($description) {
160 return sprintf('* [`%s`](#%s)', $commandName, str_replace(':', '', $description->getCommand($commandName)->getName()));
161 }, $namespace['commands'])));
164 foreach ($description->getCommands() as $command) {
165 $this->write("\n\n");
166 $this->write($this->describeCommand($command));
170 private function getApplicationTitle(Application $application)
172 if ('UNKNOWN' !== $application->getName()) {
173 if ('UNKNOWN' !== $application->getVersion()) {
174 return sprintf('%s %s', $application->getName(), $application->getVersion());
177 return $application->getName();
180 return 'Console Tool';