[9.x] CLI Prompts - #45629
Merged
Merged
Conversation
This allows more flexibility as commands can implement the contract _and_ define their own `interact` method.
jessarcher
force-pushed
the
cli-prompts
branch
from
January 17, 2023 08:03
0b23d39 to
a9a3e5c
Compare
jessarcher
marked this pull request as ready for review
January 17, 2023 08:08
Contributor
|
You're insane, @jessarcher . Thanks for all your effort on Laravel. 🚀 |
|
Thanks |
LukeTowers
added a commit
to wintercms/storm
that referenced
this pull request
Mar 31, 2025
Adds the PromptsForMissingInput functionality added to Laravel in v9.49.0 (laravel/framework#45629) to the base Command class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces the ability to automatically prompt the user for missing command arguments instead of returning an error.
This functionality has been added to all
make:*commands.Before
After
This functionality can be added to any command by implementing the new
Illuminate\Contracts\Console\PromptsForMissingInputinterface:Commands that implement
PromptsForMissingInputwill be checked for any required arguments that were not provided, and then use the argument description to prompt for the value.It also exposes a hook that is called only after the user is prompted for missing arguments, allowing additional actions to be performed. I've used this to add additional prompts on a few of the
make:*commands that have commonly needed options:Additional prompt for
make:modelThe
make:modelcommand can prompt for additional classes:It will only prompt for options when the required argument is not provided. In other words, it won't prompt for options when calling
make:model Post.The following illustrates the different scenarios where prompts will and won't show:
make:modelPrompts for model name and additional classes.make:model --migrationPrompts only for the model name.make:model PostDoes not prompt for anything.make:model --no-interactionDoes not prompt for anything.Additional prompt for
make:listenerThe
make:listenercommand can prompt for the event class being listened for, with tab completion for events in theapp/Eventsdirectory:Additional prompt for
make:observerandmake:policyThe
make:observerandmake:policycommands will prompt for the model, also with tab completion of models in the standard locations:Additional prompt for
make:testThe
make:testcommand will prompt for the type of test you would like:Additional prompts for
make:controllerThe
make:controllercommand will prompt for the type of controller you would like. Ifapi,resource, orsingletonis selected, it will also prompt for the model:I went with the name
PromptsForMissingInputrather thanPromptsForMissingArgumentsto leave the door open for prompting when an option that requires a value is passed without the value.I realise this is a pretty big DX change, so I would appreciate feedback. I'd like to stress that these prompts will only show when an error would otherwise be displayed and only in interactive terminal sessions. If you always pass the required arguments, then nothing will change for you. But if you're like me and can never remember which
make:modeloptions you want, then you would now be able to run the command without arguments and be prompted for the common options instead of needing to run it with--helpfirst.