Core\Cli::select
Shows a numbered list of choices at the terminal and returns the one that was chosen.
Core\Cli::select writes one line per choice, numbered from 1, and then the question. The person types a number. The call returns the choice itself, not its number, so the value goes straight into the rest of your program. A number that is not on the list is not an answer, so the question is asked again. A list with no choices in it throws a LogicError.
The labels option is a function that turns one choice into the line the person reads. A list of numbers or of objects needs it. The default option is the choice the Enter key takes. Where there is no terminal, in a nightly job for example, the default is the answer. Give none and the call throws Core\Cli\NotInteractive at once.
The examples below choose a region to deploy to, put readable labels on a list of numbers, and read the settings of the environment somebody picked.
Parameters
$questionstringThe text to show above the list. Control bytes are replaced as inask.$choicesarray<T>The values to choose from, listed in their own order. An empty array throws aLogicError, because there is nothing to return.
Options
labelscallableoptionalA function called with each option. It returns the line shown for that option. Without it, each option is shown as text, asechowould show it.defaultToptionalThe choice for an empty line, and for a run with no terminal. It has the element type of$choices, so it cannot be a value that is not offered.
Return value
Returns T. The chosen element of $choices, with that array's element type.
Errors
Core\Cli\NotInteractiveThe call has nodefault, and there is no terminal, its input ended, or nobody answered before the prompt deadline.LogicError$choicesis empty, so there is nothing that could be chosen.
Examples
Choose one entry of a list
<?nvs// Shows how to let somebody choose one entry of a list at the terminal. Run this// in a terminal to choose yourself. There is no terminal here, so the answer is// the default.
array<string> $regions = ['eu-west', 'us-east', 'ap-south'];
string $region = Core\Cli::select('Where should this run?', $regions, { default: 'us-east' });
// `Core\Cli::select` returns the entry itself, not the number that was typed.// The default here is "us-east", so this prints "deploying to us-east".echo 'deploying to ', $region, "\n";deploying to us-eastLabels name the choices
<?nvs// Shows the `labels` option. The choices are numbers, and `labels` gives each one// the line the person reads. There is no terminal here, so the answer is the// default.
array<int> $days = [1, 7, 30];
int $keep = Core\Cli::select('How long should a backup be kept?', $days, { labels: fn(int $day): string => Core\Str::format('%d day(s)', $day), default: 7,});
// The result is the number itself, so you can calculate with it. The default here// is 7, so this prints 168.echo $keep * 24, "\n";168A release script asks where
<?nvs// Shows the everyday use: a release script asks which environment to deploy to,// and the name that comes back reads the settings for it. There is no terminal// here, so the answer is the default.
array<string> $hosts = [ 'staging' => 'staging.example.com', 'canary' => 'canary.example.com', 'production' => 'www.example.com',];
string $target = Core\Cli::select('Deploy where?', Core\Arr::keys($hosts), { default: 'staging' });
// The choices are the keys of the list above, so the name that comes back reads// the host out of it. This prints "staging" and "staging.example.com".echo 'environment: ', $target, "\n";echo 'host: ', $hosts[$target], "\n";environment: staginghost: staging.example.com