Skip to content

feat: add arrow-key select() and multiselect() to Prompt - #10659

Open
paulbalandan wants to merge 1 commit into
codeigniter4:4.8from
paulbalandan:cli-prompt-select
Open

paulbalandan wants to merge 1 commit into
codeigniter4:4.8from
paulbalandan:cli-prompt-select

Conversation

@paulbalandan

Copy link
Copy Markdown
Member

Description
Follow-up to #10655. Adds Prompt::select(), which returns the chosen key, and Prompt::multiselect(), which returns the ticked options and can require at least one.

  • On a terminal: arrow keys to move, space to tick, a to toggle all, enter to confirm. Long lists scroll.
  • Elsewhere (Windows, piped input), the options are listed by number and a number is typed instead.
  • A quiet output returns the defaults. Ctrl+C restores the terminal and re-raises SIGINT.
select.mp4
Demo script (run from the framework root)
<?php

declare(strict_types=1);

use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Exceptions\CLIException;
use CodeIgniter\CLI\Verbosity;

require getcwd() . '/system/util_bootstrap.php';

CLI::init();

if (in_array('-q', $argv, true)) {
    CLI::getOutput()->setVerbosity(Verbosity::Quiet);
}

$prompt = CLI::getPrompt();

$agents = [
    'claude-code' => 'Claude Code',
    'cursor'      => 'Cursor',
    'copilot'     => 'VS Code Copilot',
    'codex'       => 'Codex CLI',
    'gemini'      => 'Gemini CLI',
    'windsurf'    => 'Windsurf',
    'zed'         => 'Zed',
];

$favourite = $prompt->select('Which agent do you use most?', $agents, 'cursor');
$used      = $prompt->multiselect('Which agents do you use?', $agents, ['claude-code', 'cursor']);

try {
    $features = $prompt->multiselect('Which features should be set up?', ['tools' => 'Tools', 'resources' => 'Resources', 'prompts' => 'Prompts'], required: true);
} catch (CLIException $e) {
    $features = $e->getMessage();
}

fwrite(STDOUT, var_export(['select' => $favourite, 'multiselect' => $used, 'required' => $features], true) . PHP_EOL);

Add -q to see the defaults returned, or pipe the answers to see the numbered fallback: printf '\n\n1,3\n' | php prompt-demo.php.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added the 4.8 PRs that target the `4.8` branch. label Oct 25, 2026
@paulbalandan paulbalandan added the new feature PRs for new features label Oct 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch. new feature PRs for new features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant