Skip to content

Commit

Permalink
Merge pull request #65 from getkirby/feature/debug-mode
Browse files Browse the repository at this point in the history
New debug mode
  • Loading branch information
bastianallgeier authored Feb 26, 2024
2 parents 6430c1f + c701b09 commit a9c3c83
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ kirby remove:command hello

If you have a local and a global command, you can choose which one to delete.

## Debugging

Use the `-d` or `--debug` argument to run the command in debug mode:

```bash
kirby make:command hello --debug
```

## Formatting Output

Sending messages to the terminal is super easy.
Expand Down
36 changes: 31 additions & 5 deletions src/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static function command(...$args): void
try {
$cli->run(...$args);
} catch (Throwable $e) {
$cli->error((string)$e);
$cli->handleException($e);
}
}

Expand Down Expand Up @@ -312,6 +312,19 @@ public function dir(?string $folder = null): string
return $folder;
}

/**
* Handles exception with throwing exception or out error message
*/
protected function handleException(Throwable $e): never
{
if ($this->isDefined('debug') === true) {
throw $e;
}

$this->error($e->getMessage());
exit;
}

/**
* Gets path for global commands (respecting 'XDG_CONFIG_HOME' if set)
*
Expand Down Expand Up @@ -501,7 +514,17 @@ public function run(?string $name = null, ...$args): void
]
]);

// add help as last argument
// add debug argument
$this->climate->arguments->add([
'debug' => [
'description' => 'Enables debug mode',
'prefix' => 'd',
'longPrefix' => 'debug',
'noValue' => true
]
]);

// add help argument
$this->climate->arguments->add([
'help' => [
'description' => 'Prints a usage statement',
Expand All @@ -521,8 +544,7 @@ public function run(?string $name = null, ...$args): void
try {
$this->climate->arguments->parse($argv);
} catch (Throwable $e) {
$this->error($e->getMessage());
exit;
$this->handleException($e);
}

// enable quiet mode
Expand All @@ -536,7 +558,11 @@ public function run(?string $name = null, ...$args): void
return;
}

$command['command']($this);
try {
$command['command']($this);
} catch (Throwable $e) {
$this->handleException($e);
}
}

/**
Expand Down

0 comments on commit a9c3c83

Please sign in to comment.