-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Made output docopt compatible #13220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4daf2c0
Added addUsage() to specify usage patterns
wouterj 43c1c85
Made output docopt compatible
wouterj d462aba
Customize usage section
wouterj d9a42c9
Fixed some more tests
wouterj 749d9cf
Added test for multiline descriptions
wouterj 7489720
Made fabbot happy
wouterj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,16 +32,19 @@ class TextDescriptor extends Descriptor | |
protected function describeInputArgument(InputArgument $argument, array $options = array()) | ||
{ | ||
if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) { | ||
$default = sprintf('<comment> (default: %s)</comment>', $this->formatDefaultValue($argument->getDefault())); | ||
$default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($argument->getDefault())); | ||
} else { | ||
$default = ''; | ||
} | ||
|
||
$nameWidth = isset($options['name_width']) ? $options['name_width'] : strlen($argument->getName()); | ||
$totalWidth = isset($options['total_width']) ? $options['total_width'] : strlen($argument->getName()); | ||
$spacingWidth = $totalWidth - strlen($argument->getName()) + 2; | ||
|
||
$this->writeText(sprintf(" <info>%-${nameWidth}s</info> %s%s", | ||
$this->writeText(sprintf(" <info>%s</info>%s%s%s", | ||
$argument->getName(), | ||
str_replace("\n", "\n".str_repeat(' ', $nameWidth + 2), $argument->getDescription()), | ||
str_repeat(' ', $spacingWidth), | ||
// + 17 = 2 spaces + <info> + </info> + 2 spaces | ||
preg_replace('/\s*\R\s*/', PHP_EOL.str_repeat(' ', $totalWidth + 17), $argument->getDescription()), | ||
$default | ||
), $options); | ||
} | ||
|
@@ -52,18 +55,33 @@ protected function describeInputArgument(InputArgument $argument, array $options | |
protected function describeInputOption(InputOption $option, array $options = array()) | ||
{ | ||
if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) { | ||
$default = sprintf('<comment> (default: %s)</comment>', $this->formatDefaultValue($option->getDefault())); | ||
$default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($option->getDefault())); | ||
} else { | ||
$default = ''; | ||
} | ||
|
||
$nameWidth = isset($options['name_width']) ? $options['name_width'] : strlen($option->getName()); | ||
$nameWithShortcutWidth = $nameWidth - strlen($option->getName()) - 2; | ||
$value = ''; | ||
if ($option->acceptValue()) { | ||
$value = '='.strtoupper($option->getName()); | ||
|
||
$this->writeText(sprintf(" <info>%s</info> %-${nameWithShortcutWidth}s%s%s%s", | ||
'--'.$option->getName(), | ||
$option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '', | ||
str_replace("\n", "\n".str_repeat(' ', $nameWidth + 2), $option->getDescription()), | ||
if ($option->isValueOptional()) { | ||
$value = '['.$value.']'; | ||
} | ||
} | ||
|
||
$totalWidth = isset($options['total_width']) ? $options['total_width'] : $this->calculateTotalWidthForOptions(array($option)); | ||
$synopsis = sprintf('%s%s', | ||
$option->getShortcut() ? sprintf('-%s, ', $option->getShortcut()) : ' ', | ||
sprintf('--%s%s', $option->getName(), $value) | ||
); | ||
|
||
$spacingWidth = $totalWidth - strlen($synopsis) + 2; | ||
|
||
$this->writeText(sprintf(" <info>%s</info>%s%s%s%s", | ||
$synopsis, | ||
str_repeat(' ', $spacingWidth), | ||
// + 17 = 2 spaces + <info> + </info> + 2 spaces | ||
preg_replace('/\s*\R\s*/', "\n".str_repeat(' ', $totalWidth + 17), $option->getDescription()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The additional lines only need to have +4 spaces as the |
||
$default, | ||
$option->isArray() ? '<comment> (multiple values allowed)</comment>' : '' | ||
), $options); | ||
|
@@ -74,24 +92,16 @@ protected function describeInputOption(InputOption $option, array $options = arr | |
*/ | ||
protected function describeInputDefinition(InputDefinition $definition, array $options = array()) | ||
{ | ||
$nameWidth = 0; | ||
foreach ($definition->getOptions() as $option) { | ||
$nameLength = strlen($option->getName()) + 2; | ||
if ($option->getShortcut()) { | ||
$nameLength += strlen($option->getShortcut()) + 3; | ||
} | ||
$nameWidth = max($nameWidth, $nameLength); | ||
} | ||
$totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions()); | ||
foreach ($definition->getArguments() as $argument) { | ||
$nameWidth = max($nameWidth, strlen($argument->getName())); | ||
$totalWidth = max($totalWidth, strlen($argument->getName())); | ||
} | ||
++$nameWidth; | ||
|
||
if ($definition->getArguments()) { | ||
$this->writeText('<comment>Arguments:</comment>', $options); | ||
$this->writeText("\n"); | ||
foreach ($definition->getArguments() as $argument) { | ||
$this->describeInputArgument($argument, array_merge($options, array('name_width' => $nameWidth))); | ||
$this->describeInputArgument($argument, array_merge($options, array('total_width' => $totalWidth))); | ||
$this->writeText("\n"); | ||
} | ||
} | ||
|
@@ -101,11 +111,20 @@ protected function describeInputDefinition(InputDefinition $definition, array $o | |
} | ||
|
||
if ($definition->getOptions()) { | ||
$laterOptions = array(); | ||
|
||
$this->writeText('<comment>Options:</comment>', $options); | ||
$this->writeText("\n"); | ||
foreach ($definition->getOptions() as $option) { | ||
$this->describeInputOption($option, array_merge($options, array('name_width' => $nameWidth))); | ||
if (strlen($option->getShortcut()) > 1) { | ||
$laterOptions[] = $option; | ||
continue; | ||
} | ||
$this->writeText("\n"); | ||
$this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth))); | ||
} | ||
foreach ($laterOptions as $option) { | ||
$this->writeText("\n"); | ||
$this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth))); | ||
} | ||
} | ||
} | ||
|
@@ -115,27 +134,26 @@ protected function describeInputDefinition(InputDefinition $definition, array $o | |
*/ | ||
protected function describeCommand(Command $command, array $options = array()) | ||
{ | ||
$command->getSynopsis(); | ||
$command->getSynopsis(true); | ||
$command->getSynopsis(false); | ||
$command->mergeApplicationDefinition(false); | ||
|
||
$this->writeText('<comment>Usage:</comment>', $options); | ||
$this->writeText("\n"); | ||
$this->writeText(' '.$command->getSynopsis(), $options); | ||
$this->writeText("\n"); | ||
|
||
if (count($command->getAliases()) > 0) { | ||
foreach (array_merge(array($command->getSynopsis(true)), $command->getAliases(), $command->getUsages()) as $usage) { | ||
$this->writeText("\n"); | ||
$this->writeText('<comment>Aliases:</comment> <info>'.implode(', ', $command->getAliases()).'</info>', $options); | ||
$this->writeText(' '.$usage, $options); | ||
} | ||
$this->writeText("\n"); | ||
|
||
if ($definition = $command->getNativeDefinition()) { | ||
$definition = $command->getNativeDefinition(); | ||
if ($definition->getOptions() || $definition->getArguments()) { | ||
$this->writeText("\n"); | ||
$this->describeInputDefinition($definition, $options); | ||
$this->writeText("\n"); | ||
} | ||
|
||
$this->writeText("\n"); | ||
|
||
if ($help = $command->getProcessedHelp()) { | ||
$this->writeText("\n"); | ||
$this->writeText('<comment>Help:</comment>', $options); | ||
$this->writeText("\n"); | ||
$this->writeText(' '.str_replace("\n", "\n ", $help), $options); | ||
|
@@ -164,27 +182,12 @@ protected function describeApplication(Application $application, array $options | |
} | ||
|
||
$this->writeText("<comment>Usage:</comment>\n", $options); | ||
$this->writeText(" command [options] [arguments]\n\n", $options); | ||
$this->writeText('<comment>Options:</comment>', $options); | ||
|
||
$inputOptions = $application->getDefinition()->getOptions(); | ||
|
||
$width = 0; | ||
foreach ($inputOptions as $option) { | ||
$nameLength = strlen($option->getName()) + 2; | ||
if ($option->getShortcut()) { | ||
$nameLength += strlen($option->getShortcut()) + 3; | ||
} | ||
$width = max($width, $nameLength); | ||
} | ||
++$width; | ||
$this->writeText(" command [options] [arguments]\n\n", $options); | ||
|
||
foreach ($inputOptions as $option) { | ||
$this->writeText("\n", $options); | ||
$this->describeInputOption($option, array_merge($options, array('name_width' => $width))); | ||
} | ||
$this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions()), $options); | ||
|
||
$this->writeText("\n\n", $options); | ||
$this->writeText("\n"); | ||
$this->writeText("\n"); | ||
|
||
$width = $this->getColumnWidth($description->getCommands()); | ||
|
||
|
@@ -198,12 +201,13 @@ protected function describeApplication(Application $application, array $options | |
foreach ($description->getNamespaces() as $namespace) { | ||
if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) { | ||
$this->writeText("\n"); | ||
$this->writeText('<comment>'.$namespace['id'].'</comment>', $options); | ||
$this->writeText(' <comment>'.$namespace['id'].'</comment>', $options); | ||
} | ||
|
||
foreach ($namespace['commands'] as $name) { | ||
$this->writeText("\n"); | ||
$this->writeText(sprintf(" <info>%-${width}s</info> %s", $name, $description->getCommand($name)->getDescription()), $options); | ||
$spacingWidth = $width - strlen($name); | ||
$this->writeText(sprintf(" <info>%s</info>%s%s", $name, str_repeat(' ', $spacingWidth), $description->getCommand($name)->getDescription()), $options); | ||
} | ||
} | ||
|
||
|
@@ -252,4 +256,27 @@ private function getColumnWidth(array $commands) | |
|
||
return $width + 2; | ||
} | ||
|
||
/** | ||
* @param InputOption[] $options | ||
* | ||
* @return int | ||
*/ | ||
private function calculateTotalWidthForOptions($options) | ||
{ | ||
$totalWidth = 0; | ||
foreach ($options as $option) { | ||
$nameLength = 4 + strlen($option->getName()) + 2; // - + shortcut + , + whitespace + name + -- | ||
|
||
if ($option->acceptValue()) { | ||
$valueLength = 1 + strlen($option->getName()); // = + value | ||
$valueLength += $option->isValueOptional() ? 2 : 0; // [ + ] | ||
|
||
$nameLength += $valueLength; | ||
} | ||
$totalWidth = max($totalWidth, $nameLength); | ||
} | ||
|
||
return $totalWidth; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The additional lines only need to have +4 spaces as the
<info></info>
elements only apply to the first line and that's catered for in the sprintf.