Skip to content

Commit

Permalink
Allow short options. Needed to pass along -vvv (#76)
Browse files Browse the repository at this point in the history
* 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 drush-ops/drush#5909

* Use constant

* Implement feedback

* Fix test
  • Loading branch information
weitzman authored Apr 1, 2024
1 parent 6c44638 commit 7910a08
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/Util/ArgumentProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions tests/ArgumentProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
[],
],

Expand Down

0 comments on commit 7910a08

Please sign in to comment.