From 7910a0876b7c575cf73ac8df242d1c2394653c17 Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Mon, 1 Apr 2024 08:39:32 -0400 Subject: [PATCH] Allow short options. Needed to pass along -vvv (#76) * Allow short options. Needed to pass along -vvv --verbose=3 is not allowed by its InputDefinition. --verbose does not accept a value (i.e. is VALUE_NONE). It must be passed as short option. So we enable convertOptions() to pass along short options via a magic value `-short`. Magic values of true and false are already supported so hopefully this doesnt smell too bad. This enables a fix for https://github.com/drush-ops/drush/issues/5909 * Use constant * Implement feedback * Fix test --- src/Util/ArgumentProcessor.php | 22 ++++++++++++++++++++-- tests/ArgumentProcessorTest.php | 4 ++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Util/ArgumentProcessor.php b/src/Util/ArgumentProcessor.php index c83afd9..e6fa2d8 100644 --- a/src/Util/ArgumentProcessor.php +++ b/src/Util/ArgumentProcessor.php @@ -13,6 +13,18 @@ */ class ArgumentProcessor { + private $short_options = ['vv', 'vvv']; + + public function getShortOptions(): array + { + return $this->short_options; + } + + public function setShortOptions(array $short_options): void + { + $this->short_options = $short_options; + } + /** * selectArgs selects the appropriate set of arguments for the command * to be executed and orders them as needed. @@ -81,15 +93,21 @@ protected function convertOptions($options) { $result = []; foreach ($options as $option => $value) { + $dashes = str_repeat('-', $this->dashCount($option)); if ($value === true || $value === null) { - $result[] = "--$option"; + $result[] = $dashes . $option; } elseif ($value === false) { // Ignore this option. } else { - $result[] = "--{$option}={$value}"; + $result[] = "{$dashes}{$option}={$value}"; } } return $result; } + + protected function dashCount($name): int + { + return in_array($name, $this->getShortOptions()) ? 1 : 2; + } } diff --git a/tests/ArgumentProcessorTest.php b/tests/ArgumentProcessorTest.php index e1e6867..f943477 100644 --- a/tests/ArgumentProcessorTest.php +++ b/tests/ArgumentProcessorTest.php @@ -23,10 +23,10 @@ public function argumentProcessorTestValues() ], [ - '["drush", "status", "--fields=root,uri"]', + '["drush", "status", "-vvv", "--fields=root,uri"]', [], ['drush', 'status'], - ['fields' => 'root,uri'], + ['vvv' => TRUE, 'fields' => 'root,uri'], [], ],