Skip to content

Commit 9181a86

Browse files
authored
Merge pull request #6383 from paulbalandan/spark-help-option
Allow calling help info using `spark --help`
2 parents 116f463 + ef360c8 commit 9181a86

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

phpstan-baseline.neon.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ parameters:
6060
count: 1
6161
path: system/Cache/Handlers/RedisHandler.php
6262

63+
-
64+
message: "#^Strict comparison using === between array and array{} will always evaluate to false\\.$#"
65+
count: 1
66+
path: system/CLI/Console.php
67+
6368
-
6469
message: "#^Call to an undefined method CodeIgniter\\\\HTTP\\\\Request\\:\\:getPost\\(\\)\\.$#"
6570
count: 1

system/CLI/Console.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function run()
3131
{
3232
$runner = Services::commands();
3333
$params = array_merge(CLI::getSegments(), CLI::getOptions());
34+
$params = $this->parseParamsForHelpOption($params);
3435
$command = array_shift($params) ?? 'list';
3536

3637
return $runner->run($command, $params);
@@ -53,4 +54,24 @@ public function showHeader(bool $suppress = false)
5354
), 'green');
5455
CLI::newLine();
5556
}
57+
58+
/**
59+
* Introspects the `$params` passed for presence of the
60+
* `--help` option.
61+
*
62+
* If present, it will be found as `['help' => null]`.
63+
* We'll remove that as an option from `$params` and
64+
* unshift it as argument instead.
65+
*/
66+
private function parseParamsForHelpOption(array $params): array
67+
{
68+
if (array_key_exists('help', $params)) {
69+
unset($params['help']);
70+
71+
$params = $params === [] ? ['list'] : $params;
72+
array_unshift($params, 'help');
73+
}
74+
75+
return $params;
76+
}
5677
}

tests/system/CLI/ConsoleTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,40 @@ public function testHelpCommandDetails()
9999
$this->assertStringContainsString('Options:', $this->getStreamFilterBuffer());
100100
}
101101

102+
public function testHelpCommandUsingHelpOption()
103+
{
104+
$this->initCLI('env', '--help');
105+
106+
(new Console())->run();
107+
108+
$this->assertStringContainsString('env [<environment>]', $this->getStreamFilterBuffer());
109+
$this->assertStringContainsString(
110+
'Retrieves the current environment, or set a new one.',
111+
$this->getStreamFilterBuffer()
112+
);
113+
}
114+
115+
public function testHelpOptionIsOnlyPassed()
116+
{
117+
$this->initCLI('--help');
118+
119+
(new Console())->run();
120+
121+
// Since calling `php spark` is the same as calling `php spark list`,
122+
// `php spark --help` should be the same as `php spark list --help`
123+
$this->assertStringContainsString('Lists the available commands.', $this->getStreamFilterBuffer());
124+
}
125+
126+
public function testHelpArgumentAndHelpOptionCombined()
127+
{
128+
$this->initCLI('help', '--help');
129+
130+
(new Console())->run();
131+
132+
// Same as calling `php spark help` only
133+
$this->assertStringContainsString('Displays basic usage information.', $this->getStreamFilterBuffer());
134+
}
135+
102136
/**
103137
* @param array $command
104138
*/

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Others
6666
- Added method ``Timer::record()`` to measure performance in a callable. Also enhanced common function ``timer()`` to accept optional callable.
6767
- Now ``spark routes`` command shows route names. See :ref:`URI Routing <routing-spark-routes>`.
6868
- Added new :ref:`entities-property-casting` class ``IntBoolCast`` for Entity.
69+
- Help information for a spark command can now be accessed using the ``--help`` option (e.g. ``php spark serve --help``)
6970

7071
Changes
7172
*******

0 commit comments

Comments
 (0)