From 5ac7ac84c8c6ffeda59fb490738572e1cb10c1ce Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Mon, 8 Jun 2026 16:15:00 +0800 Subject: [PATCH] refactor: display default values for options in `spark help` --- system/Commands/Help.php | 41 ++++++++++++++++++--- tests/system/Commands/HelpCommandTest.php | 30 ++++++++++++++- user_guide_src/source/changelogs/v4.8.0.rst | 2 + 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/system/Commands/Help.php b/system/Commands/Help.php index d64727ae23c0..3920e266bb59 100644 --- a/system/Commands/Help.php +++ b/system/Commands/Help.php @@ -17,6 +17,7 @@ use CodeIgniter\CLI\Attributes\Command; use CodeIgniter\CLI\CLI; use CodeIgniter\CLI\Input\Argument; +use CodeIgniter\CLI\Input\Option; /** * Displays the basic usage information for a given command. @@ -76,11 +77,9 @@ private function describeHelp(AbstractCommand $command): void CLI::write(lang('CLI.helpArguments'), 'yellow'); foreach ($command->getArgumentsDefinition() as $argument => $definition) { - $default = ''; - - if (! $definition->required) { - $default = sprintf(' [default: %s]', $this->formatDefaultValue($definition->default)); - } + $default = ! $definition->required && $this->shouldShowDefault($definition->default) + ? sprintf(' [default: %s]', $this->formatDefaultValue($definition->default)) + : ''; CLI::write(sprintf( '%s%s%s', @@ -118,10 +117,15 @@ private function describeHelp(AbstractCommand $command): void $definition->negation !== null ? sprintf('|--%s', $definition->negation) : '', ); + $default = $this->shouldShowOptionDefault($definition) + ? sprintf(' [default: %s]', $this->formatDefaultValue($definition->default)) + : ''; + CLI::write(sprintf( - '%s%s%s', + '%s%s%s%s', CLI::color($this->addPadding($optionString, 2, $maxPadding), 'green'), $definition->description, + CLI::color($default, 'yellow'), $definition->isArray ? CLI::color(' (multiple values allowed)', 'yellow') : '', )); } @@ -155,6 +159,31 @@ private function getMaxPadding(AbstractCommand $command): int return $max + 4; // Account for the extra padding around the option/argument. } + /** + * Decides whether an option's default value is worth displaying. + */ + private function shouldShowOptionDefault(Option $definition): bool + { + // Only value-accepting options carry a meaningful default. Plain flags + // and negatable toggles are boolean by nature. + if (! $definition->acceptsValue) { + return false; + } + + return $this->shouldShowDefault($definition->default); + } + + /** + * Whether a default value is concrete enough to display. `null` and the + * empty string are treated as "no default". + * + * @param list|string|null $value + */ + private function shouldShowDefault(array|string|null $value): bool + { + return $value !== null && $value !== ''; + } + /** * @param list|string $value */ diff --git a/tests/system/Commands/HelpCommandTest.php b/tests/system/Commands/HelpCommandTest.php index a8308eca3f01..83de43ecfec3 100644 --- a/tests/system/Commands/HelpCommandTest.php +++ b/tests/system/Commands/HelpCommandTest.php @@ -87,9 +87,9 @@ public function testDescribeCommandNoArguments(): void array Unused array argument. [default: ["a", "b"]] Options: - -f, --foo=FOO Option that requires a value. + -f, --foo=FOO Option that requires a value. [default: "qux"] -a, --bar[=BAR] Option that optionally accepts a value. - -b, --baz=BAZ Option that allows multiple values. (multiple values allowed) + -b, --baz=BAZ Option that allows multiple values. [default: ["a"]] (multiple values allowed) --quux|--no-quux Negatable option. -h, --help Display help for the given command. --no-header Do not display the banner when running the command. @@ -126,6 +126,32 @@ public function testDescribeSpecificCommand(): void ); } + public function testEmptyStringOptionDefaultIsNotDisplayed(): void + { + // `migrate:status` declares `--group` with an empty-string default, + // which must be suppressed the same way a null default is. + command('help migrate:status'); + + $this->assertSame( + <<<'EOT' + + Usage: + migrate:status [options] + + Description: + Displays a list of all migrations and whether they've been run or not. + + Options: + -g, --group=GROUP Set database group. + -h, --help Display help for the given command. + --no-header Do not display the banner when running the command. + -N, --no-interaction Do not ask any interactive questions. + + EOT, + $this->getUndecoratedBuffer(), + ); + } + public function testDescribeUnavailableCommand(): void { command('help test:unavailable'); diff --git a/user_guide_src/source/changelogs/v4.8.0.rst b/user_guide_src/source/changelogs/v4.8.0.rst index f1c369184f3b..3a631ed6c0df 100644 --- a/user_guide_src/source/changelogs/v4.8.0.rst +++ b/user_guide_src/source/changelogs/v4.8.0.rst @@ -190,6 +190,8 @@ Commands ``isInteractive()`` / ``setInteractive()`` methods on ``AbstractCommand``. ``isInteractive()`` also auto-detects piped or CI environments by probing STDIN for a TTY, and the state cascades to sub-commands invoked via ``$this->call(...)``. See :ref:`non-interactive-mode`. +- ``spark help `` now shows the default value of each option that accepts a value (e.g., ``[default: "file"]``), + the same way argument defaults are already displayed. Boolean flags and negatable toggles, which have no meaningful default to show, are unaffected. - You can now retrieve the last executed command in the console using the new ``Console::getCommand()`` method. This is useful for logging, debugging, or any situation where you need to know which command was run. - ``CLI`` now supports the ``--`` separator to mean that what follows are arguments, not options. This allows you to have arguments that start with ``-`` without them being treated as options. For example: ``spark my:command -- --myarg`` will pass ``--myarg`` as an argument instead of an option.