Creating a Command
You can create a new command using themake:command command:
commands/ directory:
Command Structure
Command Name
ThecommandName property defines how users will invoke your command:
Description
Thedescription is shown in the commands list and help output:
Help Text
You can provide additional help text using thehelp property:
Arguments and Flags
Arguments
Arguments are positional values passed to your command. Define them using the@args decorator:
Multiple Arguments
You can accept multiple arguments:Spread Arguments
Use@args.spread() to accept a variable number of arguments:
Flags
Flags are named options passed with-- or - prefix. Define them using the @flags decorator:
Boolean Flags
String Flags
Number Flags
Array Flags
Negated Boolean Flags
You can show negated variants in help usingshowNegatedVariantInHelp:
Lifecycle Hooks
Commands support lifecycle hooks that run at different stages:prepare()
Runs first, before any user interaction. Use it to set up the command state:interact()
Runs afterprepare(). Use it to prompt the user for additional input:
run()
The main command logic. This is where you implement your command’s functionality:completed()
Runs after the command completes or fails. You can access errors viathis.error:
User Interaction
Logger
Use the logger to output messages:Prompts
Use prompts to ask for user input:Text Input
Secure Input
Confirmation
Choice
Multiple Choice
Dependency Injection
Commands support full dependency injection. You can inject services into lifecycle methods:Accessing the Application
Commands have access to the application instance viathis.app:
Using Codemods
ThecreateCodemods() method provides access to code generation utilities:
Command Options
You can configure command behavior using theoptions property:
Allow Unknown Flags
Allow passing flags that aren’t defined in your command:Stays Alive
Indicate that your command will keep the process alive:Start App
Automatically boot the application before running the command:Running Other Commands
You can execute other commands from within your command:Terminating the Application
Use theterminate() method to gracefully shut down the application:
Best Practices
Use descriptive command names
Use descriptive command names
Command names should clearly indicate what the command does. Use namespaces (like
make:, db:, cache:) to group related commands.Validate user input
Validate user input
Always validate arguments and flags, especially when prompting users:
Provide helpful error messages
Provide helpful error messages
When commands fail, provide clear, actionable error messages:
Use lifecycle hooks appropriately
Use lifecycle hooks appropriately
prepare()for setting up stateinteract()for user promptsrun()for main logiccompleted()for cleanup and error handling