Skip to content

Commit f889329

Browse files
authored
refactor: display default values for options in spark help (#10288)
1 parent 4de1324 commit f889329

3 files changed

Lines changed: 65 additions & 8 deletions

File tree

‎system/Commands/Help.php‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\CLI\Attributes\Command;
1818
use CodeIgniter\CLI\CLI;
1919
use CodeIgniter\CLI\Input\Argument;
20+
use CodeIgniter\CLI\Input\Option;
2021

2122
/**
2223
* Displays the basic usage information for a given command.
@@ -76,11 +77,9 @@ private function describeHelp(AbstractCommand $command): void
7677
CLI::write(lang('CLI.helpArguments'), 'yellow');
7778

7879
foreach ($command->getArgumentsDefinition() as $argument => $definition) {
79-
$default = '';
80-
81-
if (! $definition->required) {
82-
$default = sprintf(' [default: %s]', $this->formatDefaultValue($definition->default));
83-
}
80+
$default = ! $definition->required && $this->shouldShowDefault($definition->default)
81+
? sprintf(' [default: %s]', $this->formatDefaultValue($definition->default))
82+
: '';
8483

8584
CLI::write(sprintf(
8685
'%s%s%s',
@@ -118,10 +117,15 @@ private function describeHelp(AbstractCommand $command): void
118117
$definition->negation !== null ? sprintf('|--%s', $definition->negation) : '',
119118
);
120119

120+
$default = $this->shouldShowOptionDefault($definition)
121+
? sprintf(' [default: %s]', $this->formatDefaultValue($definition->default))
122+
: '';
123+
121124
CLI::write(sprintf(
122-
'%s%s%s',
125+
'%s%s%s%s',
123126
CLI::color($this->addPadding($optionString, 2, $maxPadding), 'green'),
124127
$definition->description,
128+
CLI::color($default, 'yellow'),
125129
$definition->isArray ? CLI::color(' (multiple values allowed)', 'yellow') : '',
126130
));
127131
}
@@ -155,6 +159,31 @@ private function getMaxPadding(AbstractCommand $command): int
155159
return $max + 4; // Account for the extra padding around the option/argument.
156160
}
157161

162+
/**
163+
* Decides whether an option's default value is worth displaying.
164+
*/
165+
private function shouldShowOptionDefault(Option $definition): bool
166+
{
167+
// Only value-accepting options carry a meaningful default. Plain flags
168+
// and negatable toggles are boolean by nature.
169+
if (! $definition->acceptsValue) {
170+
return false;
171+
}
172+
173+
return $this->shouldShowDefault($definition->default);
174+
}
175+
176+
/**
177+
* Whether a default value is concrete enough to display. `null` and the
178+
* empty string are treated as "no default".
179+
*
180+
* @param list<string>|string|null $value
181+
*/
182+
private function shouldShowDefault(array|string|null $value): bool
183+
{
184+
return $value !== null && $value !== '';
185+
}
186+
158187
/**
159188
* @param list<string>|string $value
160189
*/

‎tests/system/Commands/HelpCommandTest.php‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ public function testDescribeCommandNoArguments(): void
8787
array Unused array argument. [default: ["a", "b"]]
8888
8989
Options:
90-
-f, --foo=FOO Option that requires a value.
90+
-f, --foo=FOO Option that requires a value. [default: "qux"]
9191
-a, --bar[=BAR] Option that optionally accepts a value.
92-
-b, --baz=BAZ Option that allows multiple values. (multiple values allowed)
92+
-b, --baz=BAZ Option that allows multiple values. [default: ["a"]] (multiple values allowed)
9393
--quux|--no-quux Negatable option.
9494
-h, --help Display help for the given command.
9595
--no-header Do not display the banner when running the command.
@@ -126,6 +126,32 @@ public function testDescribeSpecificCommand(): void
126126
);
127127
}
128128

129+
public function testEmptyStringOptionDefaultIsNotDisplayed(): void
130+
{
131+
// `migrate:status` declares `--group` with an empty-string default,
132+
// which must be suppressed the same way a null default is.
133+
command('help migrate:status');
134+
135+
$this->assertSame(
136+
<<<'EOT'
137+
138+
Usage:
139+
migrate:status [options]
140+
141+
Description:
142+
Displays a list of all migrations and whether they've been run or not.
143+
144+
Options:
145+
-g, --group=GROUP Set database group.
146+
-h, --help Display help for the given command.
147+
--no-header Do not display the banner when running the command.
148+
-N, --no-interaction Do not ask any interactive questions.
149+
150+
EOT,
151+
$this->getUndecoratedBuffer(),
152+
);
153+
}
154+
129155
public function testDescribeUnavailableCommand(): void
130156
{
131157
command('help test:unavailable');

‎user_guide_src/source/changelogs/v4.8.0.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ Commands
192192
``isInteractive()`` / ``setInteractive()`` methods on ``AbstractCommand``. ``isInteractive()`` also auto-detects piped or
193193
CI environments by probing STDIN for a TTY, and the state cascades to sub-commands invoked via ``$this->call(...)``.
194194
See :ref:`non-interactive-mode`.
195+
- ``spark help <command>`` now shows the default value of each option that accepts a value (e.g., ``[default: "file"]``),
196+
the same way argument defaults are already displayed. Boolean flags and negatable toggles, which have no meaningful default to show, are unaffected.
195197
- 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.
196198
- ``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.
197199
For example: ``spark my:command -- --myarg`` will pass ``--myarg`` as an argument instead of an option.

0 commit comments

Comments
 (0)