Skip to content

Commit 363f11b

Browse files
committed
Add hidden command support
1 parent 5dc2329 commit 363f11b

File tree

6 files changed

+75
-11
lines changed

6 files changed

+75
-11
lines changed

src/ConsoleCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct(
2323

2424
/** @var array<array-key, class-string<\Tempest\Console\ConsoleMiddleware>> */
2525
public readonly array $middleware = [],
26+
public readonly bool $hidden = false,
2627
) {
2728
}
2829

src/Input/ConsoleArgumentBag.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,22 @@ final class ConsoleArgumentBag
1717
*/
1818
public function __construct(array $arguments)
1919
{
20-
$this->path = array_filter([
21-
$arguments[0] ?? null,
22-
$arguments[1] ?? null,
23-
]);
20+
$cli = $arguments[0] ?? null;
21+
unset($arguments[0]);
22+
23+
$commandName = $arguments[1] ?? null;
24+
25+
if (
26+
$commandName !== null
27+
&& ! str_starts_with($commandName, '--')
28+
&& ! str_starts_with($commandName, '-')
29+
) {
30+
unset($arguments[1]);
31+
} else {
32+
$commandName = null;
33+
}
2434

25-
unset($arguments[0], $arguments[1]);
35+
$this->path = [$cli, $commandName];
2636

2737
foreach (array_values($arguments) as $position => $argument) {
2838
if (str_starts_with($argument, '-') && ! str_starts_with($argument, '--')) {
@@ -52,6 +62,19 @@ public function all(): array
5262
return $this->arguments;
5363
}
5464

65+
public function has(string ...$names): bool
66+
{
67+
foreach ($this->arguments as $argument) {
68+
foreach ($names as $name) {
69+
if ($argument->matches($name)) {
70+
return true;
71+
}
72+
}
73+
}
74+
75+
return false;
76+
}
77+
5578
public function get(string $name): ?ConsoleInputArgument
5679
{
5780
foreach ($this->arguments as $argument) {

src/Input/ConsoleInputArgument.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ public static function fromString(string $argument, ?int $position = null): Cons
4242

4343
public function matches(string $name): bool
4444
{
45-
return $this->name === $name
46-
|| $this->name === "-{$name}";
45+
if ($this->name === null) {
46+
return false;
47+
}
48+
49+
return ltrim($this->name, '-') === ltrim($name, '-');
4750
}
4851

4952
/**

src/Middleware/OverviewMiddleware.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public function __construct(
2424
public function __invoke(Invocation $invocation, callable $next): ExitCode
2525
{
2626
if (! $invocation->argumentBag->getCommandName()) {
27-
$this->renderOverview();
27+
$this->renderOverview(showHidden: $invocation->argumentBag->has('--all', '-a'));
2828

2929
return ExitCode::SUCCESS;
3030
}
3131

3232
return $next($invocation);
3333
}
3434

35-
private function renderOverview(): void
35+
private function renderOverview(bool $showHidden = false): void
3636
{
3737
$this->console
3838
->writeln("<h1>{$this->consoleConfig->name}</h1>")
@@ -45,6 +45,10 @@ private function renderOverview(): void
4545
$commands = [];
4646

4747
foreach ($this->consoleConfig->commands as $consoleCommand) {
48+
if ($showHidden === false && $consoleCommand->hidden) {
49+
continue;
50+
}
51+
4852
$parts = explode(':', $consoleCommand->getName());
4953

5054
$group = count($parts) > 1 ? $parts[0] : 'General';

tests/Fixtures/HiddenCommand.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Console\Fixtures;
6+
7+
use Tempest\Console\Console;
8+
use Tempest\Console\ConsoleCommand;
9+
10+
final readonly class HiddenCommand
11+
{
12+
public function __construct(private Console $console)
13+
{
14+
}
15+
16+
#[ConsoleCommand(name:"hidden", hidden: true)]
17+
public function __invoke(): void
18+
{
19+
$this->console->info('boo!');
20+
}
21+
}

tests/Actions/RenderConsoleCommandOverviewTest.php renamed to tests/Middleware/OverviewMiddlewareTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
declare(strict_types=1);
44

5-
namespace Tests\Tempest\Console\Actions;
5+
namespace Tests\Tempest\Console\Middleware;
66

77
use Tests\Tempest\Console\TestCase;
88

99
/**
1010
* @internal
1111
* @small
1212
*/
13-
class RenderConsoleCommandOverviewTest extends TestCase
13+
class OverviewMiddlewareTest extends TestCase
1414
{
1515
public function test_overview(): void
1616
{
@@ -19,8 +19,20 @@ public function test_overview(): void
1919
->assertContains('Tempest')
2020
->assertContains('General')
2121
->assertContains('Hello')
22+
->assertDoesNotContain('hidden')
2223
->assertContains('hello:world <input>')
2324
->assertContains('hello:test [optionalValue=null] [--flag=false] - description')
2425
->assertContains('testcommand:test');
2526
}
27+
28+
public function test_overview_with_hidden(): void
29+
{
30+
$this->console
31+
->call('-a')
32+
->assertContains('hidden');
33+
34+
$this->console
35+
->call('--all')
36+
->assertContains('hidden');
37+
}
2638
}

0 commit comments

Comments
 (0)