Skip to content

Commit 605f619

Browse files
committed
add test for repeated flag options
1 parent 1ccc48b commit 605f619

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

system/CLI/CLI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ public static function getOption(string $name)
945945
return $value;
946946
}
947947

948-
return $value[count($value) - 1];
948+
return $value[count($value) - 1] ?? true;
949949
}
950950

951951
/**

tests/system/CLI/CLITest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,27 @@ public function testParseCommandMultipleAndArrayOptions(): void
592592
$this->assertSame(['b', 'c', 'd'], CLI::getSegments());
593593
}
594594

595+
public function testParseCommandRepeatedFlagOption(): void
596+
{
597+
service('superglobals')->setServer('argv', [
598+
'ignored',
599+
'b',
600+
'--p1',
601+
'--p2',
602+
'--p2',
603+
]);
604+
CLI::init();
605+
606+
$this->assertSame(['p1' => null, 'p2' => [null, null]], CLI::getOptions());
607+
$this->assertTrue(CLI::getOption('p1'));
608+
$this->assertTrue(CLI::getRawOption('p1'));
609+
$this->assertTrue(CLI::getOption('p2'));
610+
$this->assertSame([null, null], CLI::getRawOption('p2'));
611+
$this->assertSame('-p1 -p2 -p2 ', CLI::getOptionString());
612+
$this->assertSame('--p1 --p2 --p2', CLI::getOptionString(true, true));
613+
$this->assertSame(['b'], CLI::getSegments());
614+
}
615+
595616
/**
596617
* @param list<string> $options
597618
*/

0 commit comments

Comments
 (0)