Commands in Masonite are generally designed to make your development life easier. Commands can range from creating controllers and models to installing packages and publishing assets.
Masonite uses the cleo package for shell command feature.
Available commands for Masonite can be displayed by running:
This will show a list of commands already available for Masonite.
Every command has a documentation screen which describes the command's available arguments and options. In order to view a command documentation, prefix the name of the command with help
. For example, to see help of serve
command you can run:
Commands can be created with a simple command class inheriting from Masonite Command
class:
Command's name, description and arguments are parsed from the Command docstring.
The docstring should start with the description of the command
and then after a blank line you can define the command name.
After the name of the command in the docstring should come the arguments. Arguments are defined with one indent and enclosed into brackets.
Positional (mandatory) arguments are defined without dashes (-
or --
).
Here is how to define a positional argument called name
with a description:
Inside the command, positional arguments can be retrieved with self.argument(arg_name)
Optional arguments are defined with dashes and can be used in any order in the command call. An optional argument --force
can have a short name --f
.
Here is how to define two optional arguments iterations
and force
with a description:
Notice how we provided the short version for the force
argument but not for the iterations
arguments
Now the command can be used like this:
If the optional argument is requiring a value you should add the =
suffix:
Here when using iterations
, the user should provide a value.
If the argument may or may not have a value, you can use the suffix =?
instead.
Finally if a default value should be used when no value is provided, add the suffix ={default}
:
Inside the command, optional arguments can be retrieved with self.option(arg_name)
You can print messages to console with different formatting:
self.info("Info Message")
: will output a message in green
self.warning("Warning Message")
: will output a message in yellow
self.error("Error Message")
: will output a message in bold red
self.comment("Comment Message")
: will output a message in light blue
You can find more information and more features for creating commands in Cleo documentation.
Masonite Command
class is inheriting Cleo Command
class so you should be able to use all Cleo features when creating commands.
Once created you can register the command to Masonite's Service Container inside a Service Provider (if you don't have one, you should create one):
add()
method takes one or multiple commands:
When you run python craft
you will now see the command you added.