Skip to content

Core\Cli::selectMany

Core\Cli::selectMany(
    string $question,
    array<T> $choices,
    {labels?: callable},
): array<T>

Shows a numbered list of choices at the terminal and returns every choice that was chosen.

Core\Cli::selectMany writes one line per choice, numbered from 1, and then the question. The person types the numbers they want, separated by commas or spaces. The call returns those choices themselves, not their numbers.

The answer is a set. A choice named twice is in it once, and the order is the order of your list. It is not the order the numbers were typed in. An empty line is the empty set, which is how somebody chooses none of them. A number that is not on the list is not an answer, so the question is asked again. 1-3 is not a way to write a range, so it is asked again too.

There is no default option, because an empty answer already means "none of these". Where there is no terminal, in a nightly job for example, the call throws Core\Cli\NotInteractive at once.

The examples below choose which features to switch on, put readable labels on a list of numbers, and restart the services somebody picked.

Parameters

  • $questionstringThe text to show above the list. Control bytes are replaced as in ask. The valid range and separator are added after it, as confirm adds [y/n].
  • $choicesarray<T>The values to choose from, listed in their own order. An empty array throws a LogicError, because there is nothing to choose.

Options

  • labelscallableoptionalA function called with each option. It returns the line shown for that option. Without it, each option is shown as text, as echo would show it.

Return value

Returns array<T>. The chosen elements of $choices, in the array's own order and each at most once. The order they were typed in does not matter. An empty line returns an empty array. There is no default, so a run with no terminal throws an error.

Errors

  • Core\Cli\NotInteractiveThere is no terminal, its input ended, or nobody answered before the prompt deadline. This prompt has no default to use instead.
  • LogicError$choices is empty, so there is nothing that could be chosen.

Changelog

VersionChange
0.0.6The Core\Cli methods that prompt or draw now say so: askSecret, showLive, showProgress and selectMany. The old names were secret, live, progress and multiSelect.

Examples

Choose several entries of a list

<?nvs
// Shows how to let somebody choose several entries of a list at the terminal. In a
// terminal the person types the numbers. This example answers itself with
// `Core\Test::answerCli`, so that it prints the same thing everywhere.
array<string> $features = ['dark mode', 'e-mail digest', 'beta pages'];
// The answer is choice 3 and choice 1. A space works in place of the comma.
Core\Test::answerCli(['3,1']);
array<string> $on = Core\Cli::selectMany('Which features should be on?', $features);
// The answer keeps the order of the list above, so this prints
// "on: dark mode, beta pages".
echo 'on: ', Core\Str::join($on, ', '), "\n";
Output
on: dark mode, beta pages

Labels name the choices

<?nvs
// Shows the `labels` option. The choices are numbers, and `labels` gives each one
// the line the person reads. This example answers itself with
// `Core\Test::answerCli`, so that it prints the same thing everywhere.
array<int> $hours = [1, 6, 24];
// The answer is choice 1 and choice 3, typed with a space between them.
Core\Test::answerCli(['1 3']);
array<int> $chosen = Core\Cli::selectMany('Which reports should run?', $hours, {
labels: fn(int $hour): string => Core\Str::format('every %d hour(s)', $hour),
});
// The result contains the numbers themselves, so you can calculate with them. The
// chosen numbers are 1 and 24, so this prints 25.
int $total = 0;
foreach ($chosen as int $hour) {
$total = $total + $hour;
}
echo $total, "\n";
Output
25

Restart the services somebody picked

<?nvs
// Shows the everyday use: a maintenance script asks which services to restart, and
// restarts only those. This member has no `default` option, so a program that may
// run without a terminal says for itself what happens instead. There is no
// terminal here.
array<string> $services = ['web', 'worker', 'scheduler'];
array<string> $restart = [];
try {
$restart = Core\Cli::selectMany('Which services should be restarted?', $services);
} catch (Core\Cli\NotInteractive $nobody) {
// Nobody can choose here, so this script restarts nothing.
$restart = [];
}
echo 'restarting ', Core\Arr::count($restart), " service(s)\n";
foreach ($services as string $service) {
if (Core\Arr::contains($restart, $service)) {
echo ' ', $service, ": restarted\n";
} else {
echo ' ', $service, ": left running\n";
}
}
Output
restarting 0 service(s)
web: left running
worker: left running
scheduler: left running