Skip to content
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

Fixes #4558 Add Global options section to command help #5284

Merged
merged 3 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function configureGlobalOptions()

$this->getDefinition()
->addOption(
new InputOption('--uri', '-l', InputOption::VALUE_REQUIRED, 'Which multisite from the selected root to use.')
new InputOption('--uri', '-l', InputOption::VALUE_REQUIRED, 'An URL for building links and selecting a multi-site. Defaults to https://default.')
);

$this->getDefinition()
Expand Down
24 changes: 24 additions & 0 deletions src/Commands/core/MkCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,27 @@ protected static function appendOptions($command): string
return '';
}

protected static function appendOptionsGlobal($application): string
{
if ($opts = $application->getDefinition()->getOptions()) {
$body = '';
foreach ($opts as $key => $value) {
if (!in_array($key, HelpCLIFormatter::OPTIONS_GLOBAL_IMPORTANT)) {
continue;
}
$name = '--' . $key;
if ($value->getShortcut()) {
$name = '-' . $name . ', ' . $value->getShortcut();
}
$body .= '- ** ' . $name . '**. ' . self::cliTextToMarkdown($value->getDescription()) . "\n";
}
$body .= '- <code>drush topic</code>. Pick first choice to see all global options.' . "\n";
$body = "#### Global Options\n\n$body\n";
return $body;
}
return '';
}

protected static function appendArguments($command): string
{
if ($args = $command->getDefinition()->getArguments()) {
Expand Down Expand Up @@ -328,6 +349,9 @@ public function writeContentFilesAndBuildNavAndBuildRedirectMap(array $namespace
}
$body .= self::appendArguments($command);
$body .= self::appendOptions($command);
if ($destination == 'commands') {
$body .= self::appendOptionsGlobal($command->getApplication());
}
if ($command instanceof AnnotatedCommand) {
$body .= self::appendTopics($command, $destination_path);
}
Expand Down
49 changes: 41 additions & 8 deletions src/Commands/help/HelpCLIFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
class HelpCLIFormatter implements FormatterInterface
{
const OPTIONS_GLOBAL_IMPORTANT = ['uri', 'verbose', 'yes'];

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -53,16 +55,34 @@ public function write(OutputInterface $output, $data, FormatterOptions $options)

$this->cleanOptions($data);
if (!empty($data['options'])) {
$rows = [];
$output->writeln('');
$output->writeln('<comment>Options:</comment>');
foreach ($data['options'] as $option) {
if (substr($option['name'], 0, 8) !== '--notify' && substr($option['name'], 0, 5) !== '--xh-' && substr($option['name'], 0, 11) !== '--druplicon') {
$rows[] = [$this->formatOptionKeys($option), $this->formatOptionDescription($option)];
}
}
$rows = $this->optionRows($output, $data['options'], 'Options');
$formatterManager->write($output, 'table', new RowsOfFields($rows), $options);
}
unset($rows);


$output->writeln('');
$output->writeln('<comment>Global options:</comment>');
$application = Drush::getApplication();
$def = $application->getDefinition();
foreach ($def->getOptions() as $key => $value) {
if (!in_array($key, self::OPTIONS_GLOBAL_IMPORTANT)) {
continue;
}
$name = $name = '--' . $key;
if ($value->getShortcut()) {
$name = '-' . $value->getShortcut() . ', ' . $name;
}
$rows[] = [
$name,
$value->getDescription(),
];
}
$rows[] = [
'',
'Run `drush topic` and pick first choice to see all global options.',
];
$formatterManager->write($output, 'table', new RowsOfFields($rows), $options);

if (array_key_exists('topics', $data)) {
$rows = [];
Expand Down Expand Up @@ -158,4 +178,17 @@ public static function isGlobalOption($name): bool
$def = $application->getDefinition();
return array_key_exists($name, $def->getOptions()) || substr($name, 0, 6) == 'notify' || substr($name, 0, 3) == 'xh-' || substr($name, 0, 9) == 'druplicon';
}

public function optionRows(OutputInterface $output, array $options, string $title): array
{
$rows = [];
$output->writeln('');
$output->writeln("<comment>$title:</comment>");
foreach ($options as $option) {
if (substr($option['name'], 0, 8) !== '--notify' && substr($option['name'], 0, 5) !== '--xh-' && substr($option['name'], 0, 11) !== '--druplicon') {
$rows[] = [$this->formatOptionKeys($option), $this->formatOptionDescription($option)];
}
}
return $rows;
}
}