Skip to content

[9.x] CLI Prompts - #45629

Merged
taylorotwell merged 18 commits into
9.xfrom
cli-prompts
Jan 23, 2023
Merged

[9.x] CLI Prompts#45629
taylorotwell merged 18 commits into
9.xfrom
cli-prompts

Conversation

@jessarcher

@jessarcher jessarcher commented Jan 13, 2023

Copy link
Copy Markdown
Member

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

image

After

image

This functionality can be added to any command by implementing the new Illuminate\Contracts\Console\PromptsForMissingInput interface:

use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;

class MyCommand extends Command implements PromptsForMissingInput
{
    // ...
}

Commands that implement PromptsForMissingInput will 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:model

The make:model command can prompt for additional classes:

image

Note Multiple options can be separated by commas, and the choices can be tab completed.

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:model Prompts for model name and additional classes.
  • make:model --migration Prompts only for the model name.
  • make:model Post Does not prompt for anything.
  • make:model --no-interaction Does not prompt for anything.

Additional prompt for make:listener

The make:listener command can prompt for the event class being listened for, with tab completion for events in the app/Events directory:

image

Note Anything you can pass to --event can be passed here, not just events in the app/Events directory. You can also just press enter to not specify anything.

Additional prompt for make:observer and make:policy

The make:observer and make:policy commands will prompt for the model, also with tab completion of models in the standard locations:

image

image

Note Again, anything you can pass to --model is accepted here, not just the models that are tab completed.

Additional prompt for make:test

The make:test command will prompt for the type of test you would like:

image

Additional prompts for make:controller

The make:controller command will prompt for the type of controller you would like. If api, resource, or singleton is selected, it will also prompt for the model:

image


I went with the name PromptsForMissingInput rather than PromptsForMissingArguments to 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:model options 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 --help first.

@jessarcher
jessarcher marked this pull request as ready for review January 17, 2023 08:08
@devajmeireles

devajmeireles commented Jan 17, 2023

Copy link
Copy Markdown
Contributor

You're insane, @jessarcher . Thanks for all your effort on Laravel. 🚀

@giorgiofra77

Copy link
Copy Markdown

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants