Input and output
Every command can read and write from and to the terminal by injecting the Console
interface. You don't have to configure anything, Tempests takes care of injecting the right dependencies for you behind the scenes:
use Tempest\Console\Console; final readonly class Package { public function __construct( private Console $console, ) {} #[ConsoleCommand] public function all(): void {} }
The Console
interface has a bunch of methods you can use:
readln(): string
— read one lineread(int $bytes): string
— read a specific amount of byteswrite(string $contents): Console
— write some text to the terminalwriteln(string $line = ''): Console
— write a line to the terminalask(string $question, ?array $options = null, bool $multiple = false, array $validation = []): string
— interactively ask a questionconfirm(string $question, bool $default = false): bool
— confirm with yes or nopassword(string $label = 'Password', bool $confirm = false): string
— retrieve a password, only works on terminals that support interactive componentsprogressBar(iterable $data, Closure $handler): array
— show a progress bar while doing some workinfo(string $line): Console
— write an info styled line to the terminalerror(string $line): Console
— write an error styled line to the terminalsuccess(string $line): Console
— write a success styled line to the terminalwhen(mixed $expression, callable $callback): Console
— only perform an action when a condition is met. The console is passed to the callable.component(ConsoleComponent $component, array $validation = []): mixed
— render a console component.
Output styling#
This packages uses tempest/highlight
to style console output. You can use HTML-like tags to style the output like so:
$this->console->writeln("<success>{$line}</success>");
The following tags are available:
<em>Emphasize</em>
results inEmphasize
<strong>Strong</strong>
results inStrong
<u>Underline</u>
results inUnderline
<h1>Header 1</h1>
results inHeader 1
<h2>Header 2</h2>
results inHeader 2
<question>Question</question>
results inQuestion
<error>Error</error>
results inError
<success>Success</success>
results inSuccess
<comment>Comment</comment>
results in/* Comment */
Exit codes#
Console commands may return an exit code if they wish to, but that's optional. If no exit code is provided, one will be automatically determined for you.
use Tempest\Console\ExitCode final readonly class Package { #[ConsoleCommand] public function all(): ExitCode { if (! $this->hasBeenSetup()) { return ExitCode::ERROR; } // … return ExitCode::SUCCESS; } }