Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c706e9d

Browse files
committed
fix: cli-utils App get opt error
1 parent b739899 commit c706e9d

File tree

6 files changed

+98
-29
lines changed

6 files changed

+98
-29
lines changed

‎composer.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
"Toolkit\\Traits\\": "libs/helper-utils/src/Traits"
5757
}
5858
},
59+
"scripts": {
60+
"test-cli": "phpunit -c libs/cli-utils/phpunit.xml.dist"
61+
},
5962
"suggest": {
6063
"inhere/php-validate": "Very lightweight data validate tool",
6164
"inhere/console": "a lightweight php console application library."

‎libs/cli-utils/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

‎libs/cli-utils/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

‎libs/cli-utils/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

‎libs/cli-utils/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+
}

‎libs/dev-helper/Console/DevController.php‎

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,18 @@
1111
use Inhere\Console\Controller;
1212
use Inhere\Console\IO\Input;
1313
use Inhere\Console\IO\Output;
14+
use RuntimeException;
1415
use Toolkit\Sys\Sys;
16+
use function array_filter;
17+
use function array_intersect;
18+
use function count;
19+
use function defined;
20+
use function implode;
21+
use function is_dir;
22+
use function is_file;
23+
use function sprintf;
24+
use function str_pad;
25+
use const PHP_EOL;
1526

1627
/**
1728
* Internal tool for toolkit development
@@ -54,7 +65,7 @@ public function listCommand(Input $input, Output $output): int
5465
{
5566
$this->checkEnv();
5667

57-
$output->colored('Components Total: ' . \count($this->components));
68+
$output->colored('Components Total: ' . count($this->components));
5869

5970
$buffer = [];
6071
$showRepo = (bool)$input->getOpt('show-repo');
@@ -65,9 +76,9 @@ public function listCommand(Input $input, Output $output): int
6576
continue;
6677
}
6778

68-
$remote = \sprintf($this->gitUrl, self::TYPE_HTTPS, $component);
69-
$component = \str_pad($component, 20);
70-
$buffer[] = \sprintf(' <comment>%s</comment> - %s', $component, $remote);
79+
$remote = sprintf($this->gitUrl, self::TYPE_HTTPS, $component);
80+
$component = str_pad($component, 20);
81+
$buffer[] = sprintf(' <comment>%s</comment> - %s', $component, $remote);
7182
}
7283

7384
$output->writeln($buffer);
@@ -93,7 +104,7 @@ public function listCommand(Input $input, Output $output): int
93104
* @param Input $input
94105
* @param Output $output
95106
* @return int
96-
* @throws \RuntimeException
107+
* @throws RuntimeException
97108
*/
98109
public function addCommand(Input $input, Output $output): int
99110
{
@@ -108,7 +119,7 @@ public function addCommand(Input $input, Output $output): int
108119
$config['onExec'] = function (string $name) use ($output) {
109120
$libPath = $this->componentDir . '/libs/' . $name;
110121

111-
if (\is_dir($libPath)) {
122+
if (is_dir($libPath)) {
112123
$output->liteWarning("Component cannot be repeat add: $name");
113124

114125
return false;
@@ -138,7 +149,7 @@ public function addCommand(Input $input, Output $output): int
138149
* @param Input $input
139150
* @param Output $output
140151
* @return int
141-
* @throws \RuntimeException
152+
* @throws RuntimeException
142153
*/
143154
public function pullCommand(Input $input, Output $output): int
144155
{
@@ -174,7 +185,7 @@ public function pullCommand(Input $input, Output $output): int
174185
* @param Input $input
175186
* @param Output $output
176187
* @return int
177-
* @throws \RuntimeException
188+
* @throws RuntimeException
178189
*/
179190
public function pushCommand(Input $input, Output $output): int
180191
{
@@ -194,32 +205,32 @@ public function pushCommand(Input $input, Output $output): int
194205
* @param Output $output
195206
* @param array $config
196207
* @return int
197-
* @throws \RuntimeException
208+
* @throws RuntimeException
198209
*/
199210
protected function runGitSubtree(Input $input, Output $output, array $config): int
200211
{
201212
$this->checkEnv();
202213
$output->writeln("<comment>Component Directory</comment>:\n$this->componentDir");
203214

204215
$operate = $config['operate'];
205-
$names = \array_filter($input->getArgs(), 'is_int', ARRAY_FILTER_USE_KEY);
216+
$names = array_filter($input->getArgs(), 'is_int', ARRAY_FILTER_USE_KEY);
206217

207218
if ($names) {
208219
$back = $names;
209-
$names = \array_intersect($names, $this->components);
220+
$names = array_intersect($names, $this->components);
210221

211222
if (!$names) {
212-
throw new \RuntimeException('Invalid component name entered: ' . \implode(', ', $back));
223+
throw new RuntimeException('Invalid component name entered: ' . implode(', ', $back));
213224
}
214225
} elseif ($input->getSameOpt(['a', 'all'], false)) {
215226
$names = $this->components;
216227
} else {
217-
throw new \RuntimeException('Please enter the name of the component that needs to be operated');
228+
throw new RuntimeException('Please enter the name of the component that needs to be operated');
218229
}
219230

220231
$output->writeln([
221232
"<comment>{$config['operatedNames']}</comment>:",
222-
' <info>' . \implode(', ', $names) . '</info>'
233+
' <info>' . implode(', ', $names) . '</info>'
223234
]);
224235

225236
$doneOne = ' OK';
@@ -240,8 +251,8 @@ protected function runGitSubtree(Input $input, Output $output, array $config): i
240251
}
241252

242253
$ret = null;
243-
$remote = \sprintf($this->gitUrl, $protoHost, $name);
244-
$command = \sprintf('git subtree %s --prefix=libs/%s %s master%s', $operate, $name, $remote, $squash);
254+
$remote = sprintf($this->gitUrl, $protoHost, $name);
255+
$command = sprintf('git subtree %s --prefix=libs/%s %s master%s', $operate, $name, $remote, $squash);
245256

246257
$output->writeln("> <cyan>$command</cyan>");
247258
$output->write("{$config['doing']} '$name' ...", false);
@@ -251,18 +262,18 @@ protected function runGitSubtree(Input $input, Output $output, array $config): i
251262
[$code, $ret, $err] = Sys::run($command, $workDir);
252263

253264
if ($code !== 0) {
254-
throw new \RuntimeException("Exec command failed. command: $command error: $err\nreturn: \n$ret");
265+
throw new RuntimeException("Exec command failed. command: $command error: $err\nreturn: \n$ret");
255266
}
256267
}
257268

258269
$output->colored($doneOne, 'success');
259270

260271
if ($ret && $input->getOpt('show-result')) {
261-
$output->writeln(\PHP_EOL . $ret);
272+
$output->writeln(PHP_EOL . $ret);
262273
}
263274
}
264275

265-
$output->colored(\sprintf($config['done'], \count($names)), 'success');
276+
$output->colored(sprintf($config['done'], count($names)), 'success');
266277

267278
return 0;
268279
}
@@ -284,7 +295,7 @@ protected function runGitSubtree(Input $input, Output $output, array $config): i
284295
* @param Input $input
285296
* @param Output $output
286297
* @return int
287-
* @throws \RuntimeException
298+
* @throws RuntimeException
288299
*/
289300
public function genApiCommand(Input $input, Output $output): int
290301
{
@@ -298,7 +309,7 @@ public function genApiCommand(Input $input, Output $output): int
298309
return -1;
299310
}
300311

301-
if (!\is_file($samiPath)) {
312+
if (!is_file($samiPath)) {
302313
$output->colored('The sami.phar file is not exists! File: ' . $samiPath, 'error');
303314

304315
return -1;
@@ -313,7 +324,7 @@ public function genApiCommand(Input $input, Output $output): int
313324
}
314325

315326
// php ~/Workspace/php/tools/sami.phar render --force
316-
$command = \sprintf(
327+
$command = sprintf(
317328
'php ~/Workspace/php/tools/sami.phar %s %s%s',
318329
'update',
319330
$config,
@@ -327,11 +338,11 @@ public function genApiCommand(Input $input, Output $output): int
327338
[$code, $ret,] = Sys::run($command, $workDir);
328339

329340
if ($code !== 0) {
330-
throw new \RuntimeException("Exec command failed. command: $command return: \n$ret");
341+
throw new RuntimeException("Exec command failed. command: $command return: \n$ret");
331342
}
332343

333344
if ($input->getOpt('show-result')) {
334-
$output->writeln(\PHP_EOL . $ret);
345+
$output->writeln(PHP_EOL . $ret);
335346
}
336347
}
337348

@@ -342,15 +353,15 @@ public function genApiCommand(Input $input, Output $output): int
342353

343354
private function checkEnv(): void
344355
{
345-
if (!\defined('TOOLKIT_DIR') || !TOOLKIT_DIR) {
356+
if (!defined('TOOLKIT_DIR') || !TOOLKIT_DIR) {
346357
$this->writeln('<error>Missing the TOOLKIT_DIR define</error>', true);
347358
}
348359

349360
$this->componentDir = TOOLKIT_DIR;
350361

351362
$file = TOOLKIT_DIR . '/components.inc';
352363

353-
if (!\is_file($file)) {
364+
if (!is_file($file)) {
354365
$this->writeln('<error>Missing the components config.</error>', true);
355366
}
356367

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /