diff --git a/src/AbstractApplication.php b/src/AbstractApplication.php index f311df9..95d5b90 100644 --- a/src/AbstractApplication.php +++ b/src/AbstractApplication.php @@ -80,6 +80,11 @@ abstract class AbstractApplication implements ApplicationInterface 'endMemory' => 0, ]; + /** + * @var int + */ + private int $exitCode = 0; + /** * @var string */ @@ -341,6 +346,10 @@ protected function afterRun(): void #[NoReturn] public function stop(int $code = 0): void { + if ($code === 0) { + $code = $this->exitCode; + } + // call 'onAppStop' event, if it is registered. $this->fire(self::ON_STOP_RUN, $this); @@ -880,4 +889,20 @@ public function getCommandName(): string { return $this->commandName; } + + /** + * @return int + */ + public function getExitCode(): int + { + return $this->exitCode; + } + + /** + * @param int $exitCode + */ + public function setExitCode(int $exitCode): void + { + $this->exitCode = $exitCode; + } } diff --git a/src/Controller.php b/src/Controller.php index dc4a7aa..193d998 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -202,14 +202,14 @@ protected function disabledCommands(): array } /** - * Will call it on action(sub-command) not found on the group. + * Will call it on subcommand not found on the group. * - * @param string $action + * @param string $command * @param array $args * * @return bool if return True, will stop goon render group help. */ - protected function onNotFound(string $action, array $args): bool + protected function onNotFound(string $command, array $args): bool { // TIP: you can add custom logic on sub-command not found. return false; @@ -310,7 +310,7 @@ public function doRun(array $args): mixed } // if command not exists. - return $this->handleNotFound($name, $action, $args); + return $this->handleNotFound($name, $command, $args); } // init flags for subcommand @@ -434,29 +434,29 @@ final public function execute(Input $input, Output $output): mixed /** * @param string $group - * @param string $action + * @param string $command * @param array $args * * @return int */ - protected function handleNotFound(string $group, string $action, array $args): int + protected function handleNotFound(string $group, string $command, array $args): int { // if user custom handle not found logic. - if ($this->onNotFound($action, $args)) { - $this->debugf('user custom handle the "%s" action "%s" not found', $group, $action); + if ($this->onNotFound($command, $args)) { + $this->debugf('group: %s - user custom handle the subcommand "%s" not found', $group, $command); return 0; } - $this->debugf('action "%s" not found on the group controller "%s"', $action, $group); + $this->debugf('group: %s - command "%s" is not found on the group', $group, $command); // if you defined the method '$this->notFoundCallback' , will call it // if (($notFoundCallback = $this->notFoundCallback) && method_exists($this, $notFoundCallback)) { // $result = $this->{$notFoundCallback}($action); // } else { - $this->output->liteError("Sorry, The command '$action' not exist of the group '$group'!"); + $this->output->liteError("Sorry, The command '$command' not exist of the group '$group'!"); // find similar command names - $similar = Helper::findSimilar($action, $this->getAllCommandMethods(null, true)); + $similar = Helper::findSimilar($command, $this->getAllCommandMethods(null, true)); if ($similar) { $this->output->writef("\nMaybe what you mean is:\n %s", implode(', ', $similar)); diff --git a/src/Util/FormatUtil.php b/src/Util/FormatUtil.php index a16f34b..21b2ca9 100644 --- a/src/Util/FormatUtil.php +++ b/src/Util/FormatUtil.php @@ -173,6 +173,7 @@ public static function spliceKeyValue(array $data, array $opts = []): string 'keyMaxWidth' => 0, // if not set, will automatic calculation 'ucFirst' => true, // upper first char for value 'endNewline' => true, // with newline on end. + 'filterEmpty' => false, ], $opts); if ($opts['keyMaxWidth'] < 1) { @@ -188,8 +189,13 @@ public static function spliceKeyValue(array $data, array $opts = []): string $keyStyle = trim($opts['keyStyle']); $keyPadPos = (int)$opts['keyPadPos']; + $filter = (bool)$opts['filterEmpty']; $fmtLines = []; foreach ($data as $key => $value) { + if ($filter && empty($value)) { + continue; + } + $hasKey = !is_int($key); $fmtLine = $opts['leftChar'];