Skip to content

Commit c10e964

Browse files
committed
fix: cli-utils App get opt error
1 parent e62fd9c commit c10e964

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

src/App.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function __construct(array $argv = null)
9595
// get script file
9696
$this->script = array_shift($argv);
9797
// parse flags
98-
[$this->args, $this->opts] = Flags::parseArgv($argv);
98+
[$this->args, $this->opts] = Flags::parseArgv($argv, ['mergeOpts' => true]);
9999
}
100100

101101
/**
@@ -207,6 +207,15 @@ protected function handleException(Throwable $e): int
207207
return $code;
208208
}
209209

210+
/**
211+
* @param callable $handler
212+
* @param array $config
213+
*/
214+
public function addByConfig(callable $handler, array $config): void
215+
{
216+
$this->addCommand($config['name'], $handler, $config);
217+
}
218+
210219
/**
211220
* @param string $command
212221
* @param callable $handler

src/Cli.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ public static function supportColor(): bool
205205
public static function isSupportColor(): bool
206206
{
207207
if (DIRECTORY_SEPARATOR === '\\') {
208-
return '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR . '.' . PHP_WINDOWS_VERSION_MINOR . '.' . PHP_WINDOWS_VERSION_BUILD || false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM')// || 'cygwin' === getenv('TERM')
208+
return '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR . '.' . PHP_WINDOWS_VERSION_MINOR . '.' . PHP_WINDOWS_VERSION_BUILD ||
209+
false !== getenv('ANSICON') ||
210+
'ON' === getenv('ConEmuANSI') ||
211+
'xterm' === getenv('TERM')// || 'cygwin' === getenv('TERM')
209212
;
210213
}
211214

src/Flags.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public static function simpleParseArgv(array $argv): array
102102
* @param array $config
103103
*
104104
* @return array [args, short-opts, long-opts]
105+
* If 'mergeOpts' is True, will return [args, opts]
105106
*/
106107
public static function parseArgv(array $params, array $config = []): array
107108
{
@@ -113,7 +114,7 @@ public static function parseArgv(array $params, array $config = []): array
113114
// List of parameters without values(bool option keys)
114115
'boolOpts' => [], // ['debug', 'h']
115116
// Whether merge short-opts and long-opts
116-
// 'mergeOpts' => false,
117+
'mergeOpts' => false,
117118
// want parsed options. if not empty, will ignore no matched
118119
'wantParsedOpts' => [],
119120
// list of option allow array values.
@@ -198,6 +199,10 @@ public static function parseArgv(array $params, array $config = []): array
198199
}
199200
}
200201

202+
if ($config['mergeOpts']) {
203+
return [$args, array_merge($sOpts, $lOpts)];
204+
}
205+
201206
return [$args, $sOpts, $lOpts];
202207
}
203208

test/FlagsTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\CliTest;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use function explode;
7+
use Toolkit\Cli\Flags;
8+
9+
/**
10+
* Class FlagsTest
11+
*
12+
* @package Toolkit\CliTest
13+
*/
14+
class FlagsTest extends TestCase
15+
{
16+
public function testParseArgv(): void
17+
{
18+
$rawArgv = explode(' ', 'git:tag --only-tag -d ../view arg0');
19+
20+
[$args, $sOpts, $lOpts] = Flags::parseArgv($rawArgv);
21+
22+
$this->assertNotEmpty($args);
23+
$this->assertSame('git:tag', $args[0]);
24+
$this->assertSame('arg0', $args[1]);
25+
26+
$this->assertSame('../view', $sOpts['d']);
27+
$this->assertTrue($lOpts['only-tag']);
28+
29+
[$args, $opts] = Flags::parseArgv($rawArgv, ['mergeOpts' => true]);
30+
31+
$this->assertNotEmpty($args);
32+
$this->assertSame('git:tag', $args[0]);
33+
$this->assertSame('arg0', $args[1]);
34+
35+
$this->assertSame('../view', $opts['d']);
36+
$this->assertTrue($opts['only-tag']);
37+
}
38+
}

0 commit comments

Comments
 (0)