Skip to content

Commit

Permalink
Format arg name and Run options hook in Commands site. (#4473)
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzman committed Jul 24, 2020
1 parent 643e8a6 commit 9a9124f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Drush is a command line shell and Unix scripting interface for Drupal. Drush cor

Resources
-----------
* [Installing (and Upgrading)](http://docs.drush.org/en/master/install/) ([Drush 8](https://docs.drush.org/en/8.x/install/))
* [Installing and Upgrading](http://docs.drush.org/en/master/install/) ([Drush 8](https://docs.drush.org/en/8.x/install/))
* [General Documentation](http://docs.drush.org) ([Drush 8](https://docs.drush.org/en/8.x/install/))
* [Commands](https://www.drush.org/commands/10.x/)
* [API Documentation](http://www.drush.org/api/master/index.html)
Expand Down
35 changes: 30 additions & 5 deletions src/Commands/core/MkCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Drush\Commands\core;

use Consolidation\AnnotatedCommand\AnnotatedCommand;
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
use Drush\Commands\DrushCommands;
use Drush\Commands\help\HelpCLIFormatter;
use Drush\Commands\help\ListCommands;
use Drush\Drush;
use Drush\SiteAlias\SiteAliasManagerAwareInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
Expand Down Expand Up @@ -41,6 +43,9 @@ public function docs($options = ['destination' => self::REQ])
$pages = $nav = [];
foreach ($namespaced as $category => $commands) {
foreach ($commands as $command) {
if ($command instanceof AnnotatedCommand) {
$command->optionsHook();
}
$name = $command->getName();
$filename = str_replace(':', '_', $name) . '.md';
$pages[] = $filename;
Expand All @@ -60,18 +65,21 @@ public function docs($options = ['destination' => self::REQ])
}
if ($args = $command->getDefinition()->getArguments()) {
$body .= "#### Arguments\n\n";
$body .= "!!! note \"Tip\"\n\n - An argument name without square brackets is mandatory.\n - An ellipsis indicates that an argument accepts multiple values separated by a space.\n\n";
foreach ($args as $arg) {
$body .= '- **' . $arg->getName() . '**. ' . $arg->getDescription() . "\n";
$arg_array = self::argToArray($arg);
$body .= '- **' . HelpCLIFormatter::formatArgumentName($arg_array) . '**. ' . $arg->getDescription() . "\n";
}
$body .= "\n";
}
if ($opts = $command->getDefinition()->getOptions()) {
$body .= "#### Options\n\n";
$body .= "!!! note \"Tip\"\n\n An option value without square brackets is mandatory. Any default value is listed at description end.\n\n";
$body .= "!!! note \"Tip\"\n\n - An option value without square brackets is mandatory.\n - Any default value is listed at description end.\n\n";
foreach ($opts as $opt) {
// @todo more rich key and default value
$opt_array = self::optionToArray($opt);
$body .= '- **' . HelpCLIFormatter::formatOptionKeys($opt_array) . '**. ' . HelpCLIFormatter::formatOptionDescription($opt_array) . "\n";
if (!HelpCLIFormatter::isGlobalOption($opt->getName())) {
$opt_array = self::optionToArray($opt);
$body .= '- **' . HelpCLIFormatter::formatOptionKeys($opt_array) . '**. ' . HelpCLIFormatter::formatOptionDescription($opt_array) . "\n";
}
}
$body .= "\n";
}
Expand Down Expand Up @@ -153,6 +161,23 @@ protected function prepare($destination)
// $fs->copy('../drush_logo-black.png', Path::join($docs_dir, 'logo.png'));
}

/**
* Build an array since thats what HelpCLIFormatter expects.
*
* @param \Symfony\Component\Console\Input\InputArgument $arg
*
* @return array
*/
public static function argToArray(InputArgument $arg)
{
$return = [
'name' => '--' . $arg->getName(),
'is_array' => $arg->isArray(),
'is_required' => $arg->isRequired(),
];
return $return;
}

/**
* Build an array since thats what HelpCLIFormatter expects.
*
Expand Down
9 changes: 7 additions & 2 deletions src/Commands/help/HelpCLIFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static function formatOptionDescription($option)
return $option['description'] . $defaults;
}

public function formatArgumentName($argument)
public static function formatArgumentName($argument)
{
$element = $argument['name'];
if (!$argument['is_required']) {
Expand All @@ -135,10 +135,15 @@ protected function cleanOptions(&$data)
{
if (array_key_exists('options', $data)) {
foreach ($data['options'] as $key => $option) {
if (substr($option['name'], 0, 8) == '--notify' || substr($option['name'], 0, 5) == '--xh-' || substr($option['name'], 0, 11) == '--druplicon') {
if (self::isGlobalOption($key)) {
unset($data['options'][$key]);
}
}
}
}

public static function isGlobalOption($name)
{
return substr($name, 0, 6) == 'notify' || substr($name, 0, 3) == 'xh-' || substr($name, 0, 9) == 'druplicon';
}
}
4 changes: 2 additions & 2 deletions src/Drupal/Commands/pm/ThemeCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getThemeInstaller()
* @param $themes A comma delimited list of themes.
* @aliases then,theme-enable
*/
public function enable($themes)
public function enable(array $themes)
{
$themes = StringUtils::csvToArray($themes);
if (!$this->getThemeInstaller()->install($themes, true)) {
Expand All @@ -48,7 +48,7 @@ public function enable($themes)
* @param $themes A comma delimited list of themes.
* @aliases thun,theme-uninstall
*/
public function uninstall($themes)
public function uninstall(array $themes)
{
$themes = StringUtils::csvToArray($themes);
// The uninstall() method has no return value. Assume it succeeded, and
Expand Down

0 comments on commit 9a9124f

Please sign in to comment.