Repository navigation
Add ConsoleIo::isInteractive() and --no-interaction option - #19821
dereuromark wants to merge 2 commits into
Conversation
--quiet both lowers the output level and disables prompts. There was no way to run a command non-interactively with normal output, and no getter for the interactive state, so commands checked the output level instead. --no-interaction is a long-only default option (-n is used by plugins) that disables prompts without changing the output level. The interactive state is now set before initialize(), so prompts there respect it too.
|
|
||
| $this->setOutputLevel($args, $io); | ||
| if ($args->getOption('quiet') || $args->getOption('no-interaction')) { | ||
| $io->setInteractive(false); |
There was a problem hiding this comment.
🤖 --no-interaction is applied after ConsoleOptionParser::parse() has already asked any questions defined with prompt, so commands can still wait for input and hang in automation. Could we apply it inside the parser, after reading the arguments but before prompting, and report missing required input as an error? That should avoid needing a separate argv pre-scan.
There was a problem hiding this comment.
Agreed. quiet/no-interaction are already in $params before the prompt loop, so the parser now skips prompts there and the existing required check throws. It skips rather than calling ask() non-interactive, because askChoice() with no default spins forever on ''. Pushed in facfed4 with tests.
| ])->addOption('no-interaction', [ | ||
| 'help' => 'Disable interactive prompts. Prompts use their default value.', | ||
| 'boolean' => true, |
There was a problem hiding this comment.
We could run into applications code that also defines this option. We'll need to have a note about this new option being added to the defaults and that applications can remove their implementation, or turn off the default options.
There was a problem hiding this comment.
Makes sense. App definitions still win, since addOption() replaces by name. Added a 5.5 migration note in cakephp/docs#8394: remove your own no-interaction or disable default options, and core now sets non-interactive when it is passed.
Option prompts run inside ConsoleOptionParser::parse(), before the command applies either flag, so a 'prompt' option could still block an automated run. The parser now checks the flags after reading argv and skips the prompt; a required option left empty fails with the usual missing-option error. Skipping instead of asking non-interactively also avoids askChoice() looping forever without a default.
--quietdoes two things: it lowers the output level and turns prompts off (setInteractive(false), soask()/askChoice()return their defaults). You can't get one without the other. There is also no getter for the interactive state, so commands that want to skip a confirmation check the output level instead. cakephp/migrationsSeedCommanddoes$io->level() > ConsoleIo::QUIET.This adds:
ConsoleIo::isInteractive(): bool--no-interaction, a default option that disables prompts but leaves output at the normal level. It is long-only because-nis already taken by plugins (bakefixture -n, cakephp-setupdb_backup create -n). A command that defines its ownno-interactionoption still overrides it, sinceaddOption()replaces same-named options.--quietkeeps its current behavior. The interactive flag is now set beforeinitialize(), not after it, so a prompt ininitialize()respects--quietand--no-interactiontoo.Options declared with
'prompt' => ...are asked while argv is parsed, before the command applies either flag. The parser now checks--quietand--no-interactionafter reading argv and skips those prompts; a required option left without a value fails with the usual missing-option error. This also changes--quiet, which used to still ask these prompts.Open question for review: should core also get a
--yes/ConsoleIo::confirm()for "assume yes" confirmations? cakephp/migrations#1128 adds a command-level--yesfor now.Docs: cakephp/docs#8394