Skip to content

Core\Cli::Stream

Names which of a program's three standard streams a call is about.

Core\Cli::Stream is declared in the class Core\Cli. The methods isTty and write take it.

Every command-line program starts with three connections to the world outside it: standard input, which carries what somebody types or what an earlier program in a pipeline produced; standard output, which carries the answer; and standard error, which carries the notes, warnings and progress a person reads while the program runs. Core\Cli::Stream has one case for each — In, Out and Err — so a call that is about one stream names it, instead of there being three members or a piece of text to misspell.

In plain words: output is the answer, error is the running commentary. Keeping them apart is what makes myprogram > results.txt leave a file holding only the answer while the warnings still reach the screen.

Good to know: input has a case because asking is this a terminal is a fair question about all three streams. Writing to it is not, so Core\Cli::write throws rather than quietly doing nothing.

Examples

The answer and the notes go apart

<?nvs
// A program writes two different things: the answer somebody asked for, and
// notes about how the run went. Name the stream for each, and a person can
// save the answer to a file while the notes still reach the screen.
// The answer. Standard output is what a write goes to when you name no stream.
Core\Cli::write('berlin,2', { newline: true });
Core\Cli::write('vienna,5', { newline: true });
// The notes. These go to standard error, so `nvs run counts.nvs > counts.csv`
// leaves the file holding the two lines above and nothing else.
Core\Cli::write('two cities counted', { stream: Core\Cli::Stream::Err, newline: true });
Output
berlin,2
vienna,5

The stream is a value you choose

<?nvs
// A `Core\Cli::Stream` is an ordinary value. You can store it in a variable,
// return it from a function and pass it to another function.
class Report
{
// A result goes to standard output. Every other message goes to standard error.
public static function streamFor(string $kind): Core\Cli::Stream
{
return match ($kind) {
'result' => Core\Cli::Stream::Out,
default => Core\Cli::Stream::Err,
};
}
public static function say(string $kind, string $message): void
{
Core\Cli::write($message, { stream: Report::streamFor($kind), newline: true });
}
}
Report::say('result', 'eur 41.90');
Report::say('warning', 'one price was missing and was left out');
Report::say('result', 'eur 12.00');
// `Core\Cli::Stream::In` is standard input, which a program reads. Writing to it
// throws an error, and the message explains why the case exists.
try {
Core\Cli::write('who would read this?', { stream: Core\Cli::Stream::In });
} catch (Throwable $refused) {
echo $refused->message, "\n";
}
Output
eur 41.90
eur 12.00
Core\Cli::write(): standard input is not a stream a program writes to — `Core\Cli::Stream::In` is a case so that `Core\Cli::isTty` can ask its question about all three

An export that survives a redirect

<?nvs
// The shape most command-line tools end up with. The data goes to standard
// output, so `nvs run export.nvs > customers.csv` writes a file you can open
// in a spreadsheet. Everything a person reads goes to standard error, so not
// one word of it lands in that file.
class Export
{
public static function run(array<array<string>> $rows): void
{
uint $written = 0;
uint $skipped = 0;
Core\Cli::write(
'exporting ' . Core\Arr::count($rows) . ' rows',
{ stream: Core\Cli::Stream::Err, newline: true },
);
Core\Cli::write('name,email', { newline: true });
foreach ($rows as array<string> $row) {
if (($row['email'] ?? '') == '') {
$skipped = $skipped + 1;
Core\Cli::write(
'skipped ' . $row['name'] . ': no email address',
{ stream: Core\Cli::Stream::Err, newline: true },
);
continue;
}
Core\Cli::write($row['name'] . ',' . $row['email'], { newline: true });
$written = $written + 1;
}
Core\Cli::write(
'wrote ' . $written . ', skipped ' . $skipped,
{ stream: Core\Cli::Stream::Err, newline: true },
);
}
}
Export::run([
['name' => 'Ada Lovelace', 'email' => 'ada@example.com'],
['name' => 'Grace Hopper', 'email' => ''],
['name' => 'Alan Turing', 'email' => 'alan@example.com'],
]);
Output
name,email
Ada Lovelace,ada@example.com
Alan Turing,alan@example.com