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 804b9cd

Browse files
committed
fix: the cli app args index error
1 parent 5c4326d commit 804b9cd

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

‎libs/cli-utils/example/liteApp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ require dirname(__DIR__) . '/test/boot.php';
99
$app = new Toolkit\Cli\App;
1010

1111
// register commands
12-
$app->addCommand('test', function () {
13-
echo "hello\n";
12+
$app->addCommand('test', function ($app) {
13+
echo "args:\n";
14+
/** @var Toolkit\Cli\App $app */
15+
print_r($app->getArgs());
16+
1417
}, 'the description text for the command: test');
1518

1619
$app->addCommand('test1', function () {

‎libs/cli-utils/src/App.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ public function __construct(array $argv = null)
9393

9494
// parse cli argv
9595
$argv = $argv ?? (array)$_SERVER['argv'];
96+
9697
// get script file
9798
$this->script = array_shift($argv);
99+
98100
// parse flags
99-
[$this->args, $this->opts] = Flags::parseArgv($argv, ['mergeOpts' => true]);
101+
[$this->args, $this->opts] = Flags::parseArgv($argv, [
102+
'mergeOpts' => true
103+
]);
100104
}
101105

102106
/**
@@ -106,14 +110,35 @@ public function __construct(array $argv = null)
106110
*/
107111
public function run(bool $exit = true): void
108112
{
109-
if (isset($this->args[0])) {
110-
$this->command = $this->args[0];
111-
unset($this->args[0]);
112-
}
113+
$this->findCommand();
113114

114115
$this->dispatch($exit);
115116
}
116117

118+
/**
119+
* find command name. it is first argument.
120+
*/
121+
protected function findCommand(): void
122+
{
123+
if (!isset($this->args[0])) {
124+
return;
125+
}
126+
127+
$newArgs = [];
128+
129+
foreach ($this->args as $key => $value) {
130+
if ($key === 0) {
131+
$this->command = trim($value);
132+
} elseif (is_int($key)) {
133+
$newArgs[] = $value;
134+
} else {
135+
$newArgs[$key] = $value;
136+
}
137+
}
138+
139+
$this->args = $newArgs;
140+
}
141+
117142
/**
118143
* @param bool $exit
119144
*
@@ -127,7 +152,7 @@ public function dispatch(bool $exit = true): void
127152
}
128153

129154
if (!isset($this->commands[$command])) {
130-
$this->displayHelp("The command {$command} not exists!");
155+
$this->displayHelp("The command '{$command}' is not exists!");
131156
return;
132157
}
133158

@@ -201,9 +226,10 @@ protected function handleException(Throwable $e): int
201226
}
202227

203228
$code = $e->getCode() !== 0 ? $e->getCode() : -1;
229+
$eTpl = "Exception(%d): %s\nFile: %s(Line %d)\nTrace:\n%s\n";
204230

205-
printf("Exception(%d): %s\nFile: %s(Line %d)\nTrace:\n%s\n", $code, $e->getMessage(), $e->getFile(),
206-
$e->getLine(), $e->getTraceAsString());
231+
// print exception message
232+
printf($eTpl, $code, $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
207233

208234
return $code;
209235
}

0 commit comments

Comments
(0)

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