Skip to content

Core\Cli::confirm

Core\Cli::confirm(string $question, {default?: bool}): boolCore\Cli::confirm(
    string $question,
    {default?: bool},
): bool

Asks a yes/no question at the terminal and returns true or false.

A command line program that is about to do something it cannot undo should ask first. Core\Cli::confirm writes the question, reads the answer, and returns true for y or yes and false for n or no. Upper case works too. Anything else is not an answer, so the question is asked again. That matters for a program that deletes on true: a typo never counts as a yes.

The default option is what the Enter key takes. The question shows it as a capital letter, [Y/n] or [y/N], so the person can see which way Enter goes. Where there is no terminal, a nightly job for example, the default is the answer. Give none and the call throws Core\Cli\NotInteractive at once, so the program never waits for an answer that cannot come.

The examples below ask before deleting, show what the default does, and walk through a deploy script that asks before each step.

Parameters

  • $questionstringThe text to show on the terminal. Control bytes are replaced as in ask.

Options

  • defaultbooloptionalThe answer for an empty line, and for a run with no terminal. Without it, a run with no terminal throws an error, and an empty line asks again.

Return value

Returns bool. true for yes and false for no. It is a plain bool and is never tainted, because only yes or no is kept from what was typed.

Errors

  • Core\Cli\NotInteractiveThe call has no default, and there is no terminal, its input ended, or nobody answered before the prompt deadline.

Examples

Ask before deleting

<?nvs
// Shows how to ask a yes/no question at the terminal. Run this in a terminal to
// answer it yourself. There is no terminal here, so the answer is the default.
bool $go = Core\Cli::confirm('Delete the old backups?', { default: false });
// `Core\Cli::confirm` returns `true` or `false`. The default here is `false`,
// so this prints "keeping the old backups".
if ($go) {
echo "deleting the old backups\n";
} else {
echo "keeping the old backups\n";
}
Output
keeping the old backups

The default is what enter takes

<?nvs
// Shows what the `default` option does. The question ends with `[Y/n]` or
// `[y/N]`, and the capital letter is the default. At a terminal the `Enter` key
// takes it. There is no terminal here, so both answers are the defaults.
bool $logs = Core\Cli::confirm('Send the logs with the report?', { default: true });
bool $email = Core\Cli::confirm('Send an email as well?', { default: false });
if ($logs) {
echo "the report carries the logs\n";
} else {
echo "the report carries the summary only\n";
}
if ($email) {
echo "an email goes out\n";
} else {
echo "no email goes out\n";
}
Output
the report carries the logs
no email goes out

A deploy script asks first

<?nvs
// Shows the everyday use: a deploy script asks before each step that cannot be
// undone, and skips a step the person says no to. There is no terminal here, so
// every answer is the default.
bool $migrate = Core\Cli::confirm('Run the database migrations?', { default: true });
bool $restart = Core\Cli::confirm('Restart the web server?', { default: true });
bool $drop = Core\Cli::confirm('Delete the orders_2019 table for good?', { default: false });
echo "deploying\n";
if ($migrate) {
echo " migrations: run\n";
} else {
echo " migrations: skipped\n";
}
if ($restart) {
echo " web server: restarted\n";
} else {
echo " web server: left running\n";
}
// The last question is the dangerous one, so its default is `false`. A job with
// no terminal deletes nothing.
if ($drop) {
echo " orders_2019: deleted\n";
} else {
echo " orders_2019: kept\n";
}
Output
deploying
migrations: run
web server: restarted
orders_2019: kept