Skip to content

Commit 04679e6

Browse files
authored
Merge pull request #55 from adhocore/54-option-parse-bug
54 option parse bug
2 parents e9df922 + cf968ba commit 04679e6

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/Helper/Normalizer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ public function normalizeArgs(array $args): array
3636
$normalized = [];
3737

3838
foreach ($args as $arg) {
39-
if (\preg_match('/^\-\w{2,}/', $arg)) {
39+
if (\preg_match('/^\-\w=/', $arg)) {
40+
$normalized = \array_merge($normalized, explode('=', $arg));
41+
} elseif (\preg_match('/^\-\w{2,}/', $arg)) {
4042
$splitArg = \implode(' -', \str_split(\ltrim($arg, '-')));
4143
$normalized = \array_merge($normalized, \explode(' ', '-' . $splitArg));
42-
} elseif (\preg_match('/^\-\-\w{2,}\=/', $arg)) {
44+
} elseif (\preg_match('/^\-\-([^\s\=]+)\=/', $arg)) {
4345
$normalized = \array_merge($normalized, explode('=', $arg));
4446
} else {
4547
$normalized[] = $arg;

tests/Input/CommandTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,21 @@ public function test_user_options()
185185
$this->assertSame('user', reset($o)->name());
186186
}
187187

188+
public function test_complex_value_option()
189+
{
190+
$p = $this->newCommand()
191+
->option('-l --limit', 'limit', 'intval')
192+
->option('-o --order-by', 'order by');
193+
194+
// `--order-by="id desc"` in terminal becomes `--order-by=id desc` in PHP.
195+
$v = $p->parse(['cmd', '-l=5', '--order-by=id desc'])->values();
196+
197+
$this->assertArrayHasKey('limit', $v);
198+
$this->assertArrayHasKey('orderBy', $v);
199+
$this->assertSame(5, $v['limit']);
200+
$this->assertSame('id desc', $v['orderBy']);
201+
}
202+
188203
public function test_usage()
189204
{
190205
$p = $this->newCommand()->usage('Usage: $ cmd [...]');

0 commit comments

Comments
 (0)