Skip to content

Commit

Permalink
Fix #2460. Use $application instead of $command in option verify (#2827)
Browse files Browse the repository at this point in the history
* Fix #2460. Use $application instead of $command in option verify

Thus removing some no longer needed code from annotation_adapter.inc

* Code style.

* Fix tests by declaring each option in .

* Remove unneeded call.

* Run options hook during option validation.
  • Loading branch information
weitzman authored Jun 30, 2017
1 parent d80a868 commit c732fdd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 53 deletions.
63 changes: 15 additions & 48 deletions includes/annotationcommand_adapter.inc
Original file line number Diff line number Diff line change
Expand Up @@ -452,24 +452,10 @@ function annotationcommand_adapter_get_commands_for_commandhandler($commandhandl
}
}
// $command['required-arguments'] = $required_arguments;

// We just need to declare the option. The values are no longer used.
foreach ($commandinfo->options()->getValues() as $option => $default) {
$description = $commandinfo->options()->getDescription($option);
$command['options'][$option] = ['description' => $description];
if (!empty($default)) {
$command['options'][$option]['example-value'] = $default;
}
$fn = 'annotationcommand_adapter_alter_option_description_' . $option;
if (function_exists($fn)) {
$command['options'][$option] = $fn($command['options'][$option], $commandinfo, $default);
}
if ($option == 'fields') {
// Allow these options to pass validation - https://github.com/drush-ops/drush/issues/2825
$command['options']['field'] = 'Select just one field, and force format to \'string\'';
$command['options']['include-field-labels'] = 'Set to 0 to avoid field labels in output.';
}
if ($commandinfo->getAnnotation('hidden-options') == $option) {
$command['options'][$option]['hidden'] = TRUE;
}
$command['options'][$option] = '';
}
$command['annotations'] = $commandinfo->getAnnotations();
// If the command has a '@return' annotation, then
Expand All @@ -491,6 +477,17 @@ function annotationcommand_adapter_get_commands_for_commandhandler($commandhandl
return $commands;
}

/**
* Determine whether command record represents an Annotated command.
*
* @param array $command
*
* @return bool
*/
function annotation_adapter_is_annotated(array $command) {
return !empty($command['annotated-command-callback']);
}

/**
* Add legacy commands to the $application. Used by help and list commands.
*
Expand All @@ -500,7 +497,7 @@ function annotationcommand_adapter_get_commands_for_commandhandler($commandhandl
function annotation_adapter_add_legacy_commands_to_application($application) {
$commands = drush_get_commands();
foreach ($commands as $command) {
if (empty($command['annotated-command-callback']) && empty($command['hidden'])) {
if (!annotation_adapter_is_annotated($command) && empty($command['hidden'])) {
$annotated = new AnnotatedCommand($command['command']);
$annotated->setDescription($command['description']);
$annotated->setAliases($command['aliases']);
Expand Down Expand Up @@ -842,36 +839,6 @@ function annotationcommand_adapter_call_validate_interface($names, $hooks, Comma
return true;
}

/**
* Alter the value of the --format description. Show available formats.
*/
function annotationcommand_adapter_alter_option_description_format($option_help, $commandinfo, $default) {
$formatterManager = annotatedcomand_adapter_get_formatter();
$return_type = $commandinfo->getReturnType();
if (!empty($return_type)) {
$available_formats = $formatterManager->validFormats($return_type);
$option_help['description'] = dt('Select output format. Available: !formats.', array('!formats' => implode(', ', $available_formats)));
if (!empty($default)) {
$option_help['description'] .= dt(' Default is !default.', array('!default' => $default));
}
}
return $option_help;
}

/**
* Add the available fields list to --fields description.
*/
function annotationcommand_adapter_alter_option_description_fields($option_help, $commandinfo, $default) {
$formatOptions = new FormatterOptions($commandinfo->getAnnotations()->getArrayCopy());
$field_labels = $formatOptions->get(FormatterOptions::FIELD_LABELS, [], '');
$default_fields = $formatOptions->get(FormatterOptions::DEFAULT_FIELDS, [], array_keys($field_labels));
$available_fields = array_keys($field_labels);
// @todo silencing a notice that will likely be fixed on views-list fixes https://github.com/consolidation/output-formatters/issues/35
$option_help['example-value'] = @implode(', ', $default_fields);
$option_help['description'] = dt('Fields to output. All available fields are: !available.', array('!available' => implode(', ', $available_fields)));
return $option_help;
}

/**
* In some circumstances, Drush just does a deep search for any *.drush.inc
* file, so that it can find all commands, in enabled and disabled modules alike,
Expand Down
22 changes: 18 additions & 4 deletions includes/command.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Consolidation\AnnotatedCommand\AnnotatedCommand;
use Drush\Drush;
use Drush\Log\LogLevel;
use Webmozart\PathUtil\Path;
Expand Down Expand Up @@ -523,11 +524,24 @@ function drush_handle_command_output($command, $structured_output) {
*/
function _drush_verify_cli_options($command) {

// Start out with just the options in the current command record.
$options = _drush_get_command_options($command);
if (annotation_adapter_is_annotated($command)) {
// The canonical options list is now in $application
$application = Drush::getApplication();
$console = $application->get($command['command']);
if ($console instanceof AnnotatedCommand) {
$console->optionsHook();
}
$options = $console->getDefinition()->getOptions();
// Add formatter options. See \Consolidation\OutputFormatters\Options\FormatterOptions
// @todo Move this out of Drush.
$options['include-field-labels'] = '';
}
else {
// Start out with just the options in the current command record.
$options = _drush_get_command_options($command);
}

// Skip all tests if the command is marked to allow anything.
// Also skip backend commands, which may have options on the commandline
// that were inherited from the calling command.
if (($command['allow-additional-options'] === TRUE)) {
return TRUE;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/help/ListCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function helpList($filter, $options = ['format' => 'listcli', 'raw' => fa
$preamble = dt('Run `drush help [command]` to view command-specific help. Run `drush topic` to read even more documentation.');
$this->renderListCLI($application, $namespaced, $this->output(), $preamble);
if (!drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_ROOT)) {
$this->io()->note(dt('Drupal root not found. Pass --root or a @siteAlias in order to see Drupal-specific commands.'));
$this->io()->note(dt('Drupal root not found. Pass --root or a @siteAlias in order to see Drupal-specific commands.'));
}
return null;
} else {
Expand Down

0 comments on commit c732fdd

Please sign in to comment.