Skip to content

Commit 5e3d2d3

Browse files
committed
up: update some for format and not found handle
1 parent d5fec40 commit 5e3d2d3

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

src/AbstractApplication.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ abstract class AbstractApplication implements ApplicationInterface
8080
'endMemory' => 0,
8181
];
8282

83+
/**
84+
* @var int
85+
*/
86+
private int $exitCode = 0;
87+
8388
/**
8489
* @var string
8590
*/
@@ -341,6 +346,10 @@ protected function afterRun(): void
341346
#[NoReturn]
342347
public function stop(int $code = 0): void
343348
{
349+
if ($code === 0) {
350+
$code = $this->exitCode;
351+
}
352+
344353
// call 'onAppStop' event, if it is registered.
345354
$this->fire(self::ON_STOP_RUN, $this);
346355

@@ -880,4 +889,20 @@ public function getCommandName(): string
880889
{
881890
return $this->commandName;
882891
}
892+
893+
/**
894+
* @return int
895+
*/
896+
public function getExitCode(): int
897+
{
898+
return $this->exitCode;
899+
}
900+
901+
/**
902+
* @param int $exitCode
903+
*/
904+
public function setExitCode(int $exitCode): void
905+
{
906+
$this->exitCode = $exitCode;
907+
}
883908
}

src/Controller.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,14 @@ protected function disabledCommands(): array
202202
}
203203

204204
/**
205-
* Will call it on action(sub-command) not found on the group.
205+
* Will call it on subcommand not found on the group.
206206
*
207-
* @param string $action
207+
* @param string $command
208208
* @param array $args
209209
*
210210
* @return bool if return True, will stop goon render group help.
211211
*/
212-
protected function onNotFound(string $action, array $args): bool
212+
protected function onNotFound(string $command, array $args): bool
213213
{
214214
// TIP: you can add custom logic on sub-command not found.
215215
return false;
@@ -310,7 +310,7 @@ public function doRun(array $args): mixed
310310
}
311311

312312
// if command not exists.
313-
return $this->handleNotFound($name, $action, $args);
313+
return $this->handleNotFound($name, $command, $args);
314314
}
315315

316316
// init flags for subcommand
@@ -434,29 +434,29 @@ final public function execute(Input $input, Output $output): mixed
434434

435435
/**
436436
* @param string $group
437-
* @param string $action
437+
* @param string $command
438438
* @param array $args
439439
*
440440
* @return int
441441
*/
442-
protected function handleNotFound(string $group, string $action, array $args): int
442+
protected function handleNotFound(string $group, string $command, array $args): int
443443
{
444444
// if user custom handle not found logic.
445-
if ($this->onNotFound($action, $args)) {
446-
$this->debugf('user custom handle the "%s" action "%s" not found', $group, $action);
445+
if ($this->onNotFound($command, $args)) {
446+
$this->debugf('group: %s - user custom handle the subcommand "%s" not found', $group, $command);
447447
return 0;
448448
}
449449

450-
$this->debugf('action "%s" not found on the group controller "%s"', $action, $group);
450+
$this->debugf('group: %s - command "%s" is not found on the group', $group, $command);
451451

452452
// if you defined the method '$this->notFoundCallback' , will call it
453453
// if (($notFoundCallback = $this->notFoundCallback) && method_exists($this, $notFoundCallback)) {
454454
// $result = $this->{$notFoundCallback}($action);
455455
// } else {
456-
$this->output->liteError("Sorry, The command '$action' not exist of the group '$group'!");
456+
$this->output->liteError("Sorry, The command '$command' not exist of the group '$group'!");
457457

458458
// find similar command names
459-
$similar = Helper::findSimilar($action, $this->getAllCommandMethods(null, true));
459+
$similar = Helper::findSimilar($command, $this->getAllCommandMethods(null, true));
460460

461461
if ($similar) {
462462
$this->output->writef("\nMaybe what you mean is:\n <info>%s</info>", implode(', ', $similar));

src/Util/FormatUtil.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public static function spliceKeyValue(array $data, array $opts = []): string
173173
'keyMaxWidth' => 0, // if not set, will automatic calculation
174174
'ucFirst' => true, // upper first char for value
175175
'endNewline' => true, // with newline on end.
176+
'filterEmpty' => false,
176177
], $opts);
177178

178179
if ($opts['keyMaxWidth'] < 1) {
@@ -188,8 +189,13 @@ public static function spliceKeyValue(array $data, array $opts = []): string
188189
$keyStyle = trim($opts['keyStyle']);
189190
$keyPadPos = (int)$opts['keyPadPos'];
190191

192+
$filter = (bool)$opts['filterEmpty'];
191193
$fmtLines = [];
192194
foreach ($data as $key => $value) {
195+
if ($filter && empty($value)) {
196+
continue;
197+
}
198+
193199
$hasKey = !is_int($key);
194200
$fmtLine = $opts['leftChar'];
195201

0 commit comments

Comments
 (0)