Skip to content

Commit 39e9d62

Browse files
committed
fix an test error
1 parent b2013cf commit 39e9d62

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/Flag/Option.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99

1010
namespace Toolkit\PFlag\Flag;
1111

12+
use Toolkit\Cli\Helper\FlagHelper;
13+
use Toolkit\PFlag\Exception\FlagException;
1214
use Toolkit\PFlag\FlagType;
1315
use function array_filter;
16+
use function array_map;
17+
use function array_unshift;
1418
use function implode;
15-
use function rtrim;
16-
use function trim;
19+
use function strlen;
1720

1821
/**
1922
* Class Option
@@ -65,8 +68,16 @@ public function getAlias(): string
6568
*/
6669
public function setAlias(string $alias): void
6770
{
68-
if ($alias = trim($alias)) {
71+
if (!$alias) {
72+
return;
73+
}
74+
75+
if (!FlagHelper::isValidName($alias)) {
76+
throw new FlagException('invalid option alias: ' . $alias);
77+
}
6978

79+
if (strlen($alias) < 2) {
80+
throw new FlagException('flag option alias length cannot be < 2 ');
7081
}
7182

7283
$this->alias = $alias;
@@ -103,25 +114,34 @@ public function getShorts(): array
103114
*/
104115
public function setShorts(array $shorts): void
105116
{
106-
$this->shorts = $shorts;
107-
$this->shortcut = '-' . rtrim(implode(', -', $shorts));
117+
if ($shorts) {
118+
$this->shorts = $shorts;
119+
$this->shortcut = '-' . implode(', -', $shorts);
120+
}
108121
}
109122

110123
/**
111124
* @return string
112125
*/
113126
public function getHelpName(): string
114127
{
115-
$shorts = $this->shortcut;
116-
117128
$names = [];
118129
if ($this->alias) {
119130
$names[] = $this->alias;
120131
}
121132

122133
$names[] = $this->name;
123134

124-
return '-' . rtrim(implode(', -', $shorts));
135+
// prepend '--'
136+
$nodes = array_map(static function (string $name) {
137+
return (strlen($name) > 1 ? '--' : '-') . $name;
138+
}, $names);
139+
140+
if ($this->shortcut) {
141+
array_unshift($nodes, $this->shortcut);
142+
}
143+
144+
return implode(', ', $nodes);
125145
}
126146

127147
/**

test/Flag/OptionTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public function testBasic(): void
2020

2121
$opt->setAlias('n1');
2222
$this->assertSame('n1', $opt->getAlias());
23+
$this->assertSame('--n1, --name', $opt->getHelpName());
24+
25+
$opt->setShortcut('n');
26+
$this->assertSame('-n, --n1, --name', $opt->getHelpName());
2327

2428
$opt->setDefault(89);
2529
$this->assertTrue($opt->hasDefault());
@@ -48,5 +52,7 @@ public function testShortcut(): void
4852
$this->assertSame(['a', 'b'], $opt->getShorts());
4953
$this->assertSame('-a, -b', $opt->getShortcut());
5054
}
55+
56+
$this->assertSame('-a, -b, --name', $opt->getHelpName());
5157
}
5258
}

test/ValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testNameValidator(): void
2727
$this->assertTrue($v('inhere', 'test'));
2828

2929
$this->expectException(FlagException::class);
30-
$this->expectExceptionMessage("flag 'test' value should match: ^\w+$");
30+
$this->expectExceptionMessage("flag 'test' value should match: " . NameValidator::DEFAULT_REGEX);
3131
$v(' inhere ', 'test');
3232
}
3333
}

0 commit comments

Comments
 (0)