Skip to content

Core\Cli\NotInteractive

The error a question throws when nobody is there to answer it.

A command-line program can ask a question, such as a name, a yes or no, or a choice from a list. It can also run where no person is watching, such as a nightly job, a build step or a queue worker. Novis does not wait there for an answer that never comes. A question with a default returns that value and the program goes on. A question without a default throws this error at once.

So you decide what happens. A program that should run without a person gives every question a default. A program that must not guess catches this error and stops, and says what it needed. For example, a program that deletes data, sends messages or charges money must not guess.

Good to know: a question is shown on the terminal the program was started from, not read from its standard input. So a program that reads piped data can still ask a question.

The examples below show a question nobody answers, the same question with a default, and a release script that stops when nobody answers.

Examples

Nobody is there to answer

<?nvs
// `Core\Cli::ask` needs a person at a terminal. A nightly job or a build step has
// no terminal, so `ask` throws `Core\Cli\NotInteractive` immediately. The `catch`
// block decides what happens next.
try {
tainted string $who = Core\Cli::ask('Who is running this release?');
echo 'released by ', $who, "\n";
} catch (Core\Cli\NotInteractive $nobody) {
echo $nobody->message, "\n";
echo "released by the nightly job\n";
}
Output
no answer for Core\Cli::ask: there is no controlling terminal
released by the nightly job

A fallback answer for an empty room

<?nvs
// The same questions, each given the answer to use when nobody is there. None
// of them can raise this error any more, so one script works both at a desk and
// in a job nobody is watching.
tainted string $who = Core\Cli::ask('Who is running this release?', { default: 'the nightly job' });
echo 'released by ', $who, "\n";
bool $migrate = Core\Cli::confirm('Run the database migration too?', { default: false });
echo 'migration: ', $migrate ? 'yes' : 'no', "\n";
array<string> $rooms = ['staging', 'production'];
string $room = Core\Cli::select('Where should this go?', $rooms, { default: 'staging' });
echo 'target: ', $room, "\n";
Output
released by the nightly job
migration: no
target: staging

A release script that refuses to guess

<?nvs
// This script deletes old releases only when a person confirms it. In a job with
// no terminal, `Core\Cli::confirm` throws `Core\Cli\NotInteractive`. The script
// then prints what it needs and deletes nothing.
final class Cleanup
{
public static function sweep(int $old): string
{
try {
if (Core\Cli::confirm('Delete the ' . ($old as string) . ' oldest releases?')) {
return 'deleted ' . ($old as string) . ' releases';
}
return 'kept everything';
} catch (Core\Cli\NotInteractive $nobody) {
return 'kept everything: nobody could answer. Run this from a terminal, or pass --delete-old to answer yes in advance';
}
}
}
echo Cleanup::sweep(4), "\n";
Output
kept everything: nobody could answer. Run this from a terminal, or pass --delete-old to answer yes in advance