Skip to content

Core\Cli::ask

Core\Cli::ask(
    string $question,
    {default?: string, validate?: callable},
): tainted string

Asks a question at the terminal and returns the line the person typed.

A command line program often needs one thing from the person running it: a name, a path, a port number. Core\Cli::ask writes the question and returns the answer, without the line ending. It reads the terminal itself and not the standard input, so a program that is reading piped data can still ask. A validate function of yours checks each answer, and an answer it turns down is asked again.

Where there is no terminal, a nightly job for example, nobody can answer. Give a default and that is what you get. Give none and the call throws Core\Cli\NotInteractive at once, so the program never waits for an answer that cannot come.

Good to know: the answer is tainted. A person typed it, so anything can be in it. Check it before you use it as a file path or in a database query.

The examples below ask one question, ask again until the answer is a number, and fill in the settings a new project needs.

Parameters

  • $questionstringThe text to show on the terminal. Control bytes are replaced as echo replaces them. So a question built from untrusted text cannot move the cursor or redraw the screen around the answer.

Options

  • defaultstringoptionalThe value returned when there is no terminal, or when the user types an empty line. Without it, both cases throw Core\Cli\NotInteractive.
  • validatecallableoptionalA function called with each answer. If it returns a falsy value, the question is asked again. It runs only when there is a terminal, so it is never called with default.

Return value

Returns tainted string. The line the user typed, without the line ending. It is always tainted, because it comes from outside the program, like a request body.

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 one question

<?nvs
// Shows how to ask a question at the terminal. Run this in a terminal to type
// your own answer. There is no terminal here, so the answer is the default.
tainted string $name = Core\Cli::ask('What is the new project called?', { default: 'shop' });
// The answer is tainted, because a person typed it. You can print it and you
// can measure it. This prints 4.
echo 'creating the project ', $name, "\n";
echo 'the name is ', Core\Str::length($name), " characters long\n";
Output
creating the project shop
the name is 4 characters long

Ask again until the answer fits

<?nvs
// Shows how to keep asking until the answer fits. `validate` is called with
// each answer, and an answer it says `false` for is asked again. It runs at a
// terminal only, so this program takes the default.
tainted string $port = Core\Cli::ask(
'Which port should the server use?',
{
default: '8080',
// `as ?int` converts the answer to a whole number. Text that is not a
// number becomes `null`, and that answer is asked again.
validate: fn(tainted string $answer): bool => ($answer as ?int) != null,
},
);
echo 'the server will use port ', $port, "\n";
Output
the server will use port 8080

The settings a new project needs

<?nvs
// Shows the everyday use: a setup script asks for what it cannot guess. Every
// question has a default, so pressing `Enter` at a terminal takes it. There is
// no terminal here, so all three answers are the defaults.
tainted string $name = Core\Cli::ask('Project name?', { default: 'shop' });
tainted string $host = Core\Cli::ask('Database host?', { default: 'localhost' });
tainted string $port = Core\Cli::ask('Database port?', { default: '5432' });
echo "writing nvs.toml\n";
echo ' name = ', $name, "\n";
echo ' database = ', $host, ':', $port, "\n";
Output
writing nvs.toml
name = shop
database = localhost:5432