Skip to content

Commit

Permalink
Add hidden command support
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed May 29, 2024
1 parent 5dc2329 commit 363f11b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/ConsoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(

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

Expand Down
33 changes: 28 additions & 5 deletions src/Input/ConsoleArgumentBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@ final class ConsoleArgumentBag
*/
public function __construct(array $arguments)
{
$this->path = array_filter([
$arguments[0] ?? null,
$arguments[1] ?? null,
]);
$cli = $arguments[0] ?? null;
unset($arguments[0]);

$commandName = $arguments[1] ?? null;

if (
$commandName !== null
&& ! str_starts_with($commandName, '--')
&& ! str_starts_with($commandName, '-')
) {
unset($arguments[1]);
} else {
$commandName = null;
}

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

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

public function has(string ...$names): bool
{
foreach ($this->arguments as $argument) {
foreach ($names as $name) {
if ($argument->matches($name)) {
return true;
}
}
}

return false;
}

public function get(string $name): ?ConsoleInputArgument
{
foreach ($this->arguments as $argument) {
Expand Down
7 changes: 5 additions & 2 deletions src/Input/ConsoleInputArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ public static function fromString(string $argument, ?int $position = null): Cons

public function matches(string $name): bool
{
return $this->name === $name
|| $this->name === "-{$name}";
if ($this->name === null) {
return false;
}

return ltrim($this->name, '-') === ltrim($name, '-');
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Middleware/OverviewMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public function __construct(
public function __invoke(Invocation $invocation, callable $next): ExitCode
{
if (! $invocation->argumentBag->getCommandName()) {
$this->renderOverview();
$this->renderOverview(showHidden: $invocation->argumentBag->has('--all', '-a'));

return ExitCode::SUCCESS;
}

return $next($invocation);
}

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

foreach ($this->consoleConfig->commands as $consoleCommand) {
if ($showHidden === false && $consoleCommand->hidden) {
continue;
}

$parts = explode(':', $consoleCommand->getName());

$group = count($parts) > 1 ? $parts[0] : 'General';
Expand Down
21 changes: 21 additions & 0 deletions tests/Fixtures/HiddenCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Console\Fixtures;

use Tempest\Console\Console;
use Tempest\Console\ConsoleCommand;

final readonly class HiddenCommand
{
public function __construct(private Console $console)
{
}

#[ConsoleCommand(name:"hidden", hidden: true)]
public function __invoke(): void
{
$this->console->info('boo!');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

declare(strict_types=1);

namespace Tests\Tempest\Console\Actions;
namespace Tests\Tempest\Console\Middleware;

use Tests\Tempest\Console\TestCase;

/**
* @internal
* @small
*/
class RenderConsoleCommandOverviewTest extends TestCase
class OverviewMiddlewareTest extends TestCase
{
public function test_overview(): void
{
Expand All @@ -19,8 +19,20 @@ public function test_overview(): void
->assertContains('Tempest')
->assertContains('General')
->assertContains('Hello')
->assertDoesNotContain('hidden')
->assertContains('hello:world <input>')
->assertContains('hello:test [optionalValue=null] [--flag=false] - description')
->assertContains('testcommand:test');
}

public function test_overview_with_hidden(): void
{
$this->console
->call('-a')
->assertContains('hidden');

$this->console
->call('--all')
->assertContains('hidden');
}
}

0 comments on commit 363f11b

Please sign in to comment.