diff --git a/README.md b/README.md index 530ae53..e315d7c 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,14 @@ Once you no longer need a command, you can remove it with … kirby remove:command hello ``` +## Debugging + +Use the `-d` or `--debug` argument to run the command in debug mode: + +```bash +kirby make:command hello --debug +``` + If you have a local and a global command, you can choose which one to delete. ## Formatting Output diff --git a/src/CLI/CLI.php b/src/CLI/CLI.php index 58a2471..ff0fa58 100644 --- a/src/CLI/CLI.php +++ b/src/CLI/CLI.php @@ -501,7 +501,7 @@ public function run(?string $name = null, ...$args): void ] ]); - // add help as last argument + // add help argument $this->climate->arguments->add([ 'help' => [ 'description' => 'Prints a usage statement', @@ -511,6 +511,16 @@ public function run(?string $name = null, ...$args): void ] ]); + // add debug argument + $this->climate->arguments->add([ + 'debug' => [ + 'description' => 'Enables debug mode', + 'prefix' => 'd', + 'longPrefix' => 'debug', + 'noValue' => true + ] + ]); + // build the args array $argv = [ 'kirby', @@ -521,6 +531,10 @@ public function run(?string $name = null, ...$args): void try { $this->climate->arguments->parse($argv); } catch (Throwable $e) { + if ($this->climate->arguments->defined('debug') === true) { + throw $e; + } + $this->error($e->getMessage()); exit; } @@ -536,7 +550,16 @@ public function run(?string $name = null, ...$args): void return; } - $command['command']($this); + try { + $command['command']($this); + } catch (Throwable $e) { + if ($this->climate->arguments->defined('debug') === true) { + throw $e; + } + + $this->error($e->getMessage()); + exit; + } } /**