Skip to content

Commit

Permalink
[Console] Allow '0' as a $shortcut in InputOption.php
Browse files Browse the repository at this point in the history
  • Loading branch information
lawsonjl-ornl authored and fabpot committed Jan 21, 2024
1 parent a75c7a6 commit 923817d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Input/InputOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct(string $name, $shortcut = null, int $mode = null, st
throw new InvalidArgumentException('An option name cannot be empty.');
}

if (empty($shortcut)) {
if ('' === $shortcut || [] === $shortcut) {
$shortcut = null;
}

Expand All @@ -78,10 +78,10 @@ public function __construct(string $name, $shortcut = null, int $mode = null, st
$shortcut = implode('|', $shortcut);
}
$shortcuts = preg_split('{(\|)-?}', ltrim($shortcut, '-'));
$shortcuts = array_filter($shortcuts);
$shortcuts = array_filter($shortcuts, 'strlen');
$shortcut = implode('|', $shortcuts);

if (empty($shortcut)) {
if ('' === $shortcut) {
throw new InvalidArgumentException('An option shortcut cannot be empty.');
}
}
Expand Down
14 changes: 14 additions & 0 deletions Tests/Input/InputOptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ public function testShortcut()
$this->assertEquals('f|ff|fff', $option->getShortcut(), '__construct() removes the leading - of the shortcuts');
$option = new InputOption('foo');
$this->assertNull($option->getShortcut(), '__construct() makes the shortcut null by default');
$option = new InputOption('foo', '');
$this->assertNull($option->getShortcut(), '__construct() makes the shortcut null when given an empty string');
$option = new InputOption('foo', []);
$this->assertNull($option->getShortcut(), '__construct() makes the shortcut null when given an empty array');
$option = new InputOption('foo', ['f', '', 'fff']);
$this->assertEquals('f|fff', $option->getShortcut(), '__construct() removes empty shortcuts');
$option = new InputOption('foo', 'f||fff');
$this->assertEquals('f|fff', $option->getShortcut(), '__construct() removes empty shortcuts');
$option = new InputOption('foo', '0');
$this->assertEquals('0', $option->getShortcut(), '-0 is an acceptable shortcut value');
$option = new InputOption('foo', ['0', 'z']);
$this->assertEquals('0|z', $option->getShortcut(), '-0 is an acceptable shortcut value when embedded in an array');
$option = new InputOption('foo', '0|z');
$this->assertEquals('0|z', $option->getShortcut(), '-0 is an acceptable shortcut value when embedded in a string-list');
}

public function testModes()
Expand Down

0 comments on commit 923817d

Please sign in to comment.