Skip to content

Commit

Permalink
up: update some for format and not found handle
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 17, 2022
1 parent d5fec40 commit 5e3d2d3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
25 changes: 25 additions & 0 deletions src/AbstractApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ abstract class AbstractApplication implements ApplicationInterface
'endMemory' => 0,
];

/**
* @var int
*/
private int $exitCode = 0;

/**
* @var string
*/
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
}
22 changes: 11 additions & 11 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 <info>%s</info>", implode(', ', $similar));
Expand Down
6 changes: 6 additions & 0 deletions src/Util/FormatUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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'];

Expand Down

0 comments on commit 5e3d2d3

Please sign in to comment.