Skip to content

Commit 919e986

Browse files
committed
fix parse flag error
1 parent d3314ce commit 919e986

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/Flags.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,12 @@ public static function parseArgv(array $params, array $config = []): array
195195
// value specified inline (<arg>=<value>)
196196
if (strpos($p, '=') !== false) {
197197
[$name, $value] = explode('=', $p, 2);
198-
$args[$name] = self::filterBool($value);
198+
199+
if (self::isValidArgName($name)) {
200+
$args[$name] = self::filterBool($value);
201+
} else {
202+
$args[] = $p;
203+
}
199204
} else {
200205
$args[] = $p;
201206
}
@@ -314,6 +319,16 @@ public static function nextIsValue($val): bool
314319
return $val[0] !== '-' && false === strpos($val, '=');
315320
}
316321

322+
/**
323+
* @param string $name
324+
*
325+
* @return bool
326+
*/
327+
public static function isValidArgName(string $name): bool
328+
{
329+
return preg_match('#^\w+$#', $name) === 1;
330+
}
331+
317332
/**
318333
* Escapes a token through escapeshellarg if it contains unsafe chars.
319334
*

test/FlagsTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
namespace Toolkit\CliTest;
1111

1212
use PHPUnit\Framework\TestCase;
13-
use function explode;
1413
use Toolkit\Cli\Flags;
14+
use function explode;
1515

1616
/**
1717
* Class FlagsTest
@@ -42,4 +42,32 @@ public function testParseArgv(): void
4242
$this->assertSame('../view', $opts['d']);
4343
$this->assertTrue($opts['only-tag']);
4444
}
45+
46+
public function testParseInvalidArgName(): void
47+
{
48+
[$args, , ] = Flags::parseArgv([
49+
'cmd',
50+
'http://some.com/path/to/merge_requests/new?utf8=%E2%9C%93&merge_request%5Bsource_project_id%5D=319'
51+
]);
52+
53+
$this->assertSame('cmd', $args[0]);
54+
$this->assertSame('http://some.com/path/to/merge_requests/new?utf8=%E2%9C%93&merge_request%5Bsource_project_id%5D=319', $args[1]);
55+
}
56+
57+
public function testisParseWithSpace(): void
58+
{
59+
[$args, , ] = Flags::parseArgv([
60+
'cmd',
61+
' -'
62+
]);
63+
64+
$this->assertSame('cmd', $args[0]);
65+
$this->assertSame(' -', $args[1]);
66+
}
67+
68+
public function testisValidArgName(): void
69+
{
70+
$this->assertTrue(Flags::isValidArgName('arg0'));
71+
$this->assertFalse(Flags::isValidArgName('/path/to'));
72+
}
4573
}

0 commit comments

Comments
 (0)