Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions system/Commands/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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') : '',
));
}
Expand Down Expand Up @@ -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>|string|null $value
*/
private function shouldShowDefault(array|string|null $value): bool
{
return $value !== null && $value !== '';
}

/**
* @param list<string>|string $value
*/
Expand Down
30 changes: 28 additions & 2 deletions tests/system/Commands/HelpCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.8.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <command>`` 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.
Expand Down