Skip to content

Commit 4b66138

Browse files
committed
enhance: option name rule sort is not required
1 parent 5df05eb commit 4b66138

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/AbstractFlags.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,11 +705,18 @@ protected function parseRuleOptName(string $key): array
705705
// support like '--name, -n'
706706
$k = ltrim($k, '-');
707707

708-
if ($i === 0) {
708+
// long string as option name.
709+
if (!$name && strlen($k) > 1) {
709710
$name = $k;
710-
} else {
711-
$shorts[] = $k;
711+
continue;
712712
}
713+
714+
$shorts[] = $k;
715+
}
716+
717+
// no long name, first short name as option name.
718+
if (!$name) {
719+
$name = array_shift($shorts);
713720
}
714721

715722
return [$name, $shorts];

test/SFlagsTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ public function testParseDefined(): void
4747
// int
4848
$flags = ['-n', 'inhere', '--age', '99'];
4949
$fs->parseDefined($flags, [
50-
'name, n' => FlagType::STRING, // add an alias
51-
'age' => FlagType::INT,
50+
// 'name,n' => FlagType::STRING, // add an alias
51+
'n,name' => FlagType::STRING, // add an alias
52+
'age' => FlagType::INT,
5253
]);
5354
// vdump($fs);
5455
$this->assertSame('inhere', $fs->getOption('name'));
5556
$this->assertSame(99, $fs->getOption('age'));
5657
$this->assertCount(0, $fs->getRawArgs());
58+
$this->assertTrue($fs->hasAlias('n'));
59+
$this->assertSame('name', $fs->resolveAlias('n'));
5760

5861
$fs->reset();
5962
$this->assertFalse($fs->isParsed());

0 commit comments

Comments
 (0)