Commands
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:
1
python craft
Copied!
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:
1
python craft help serve
Copied!

Creating Commands

Commands can be created with a simple command class inheriting from Masonite Command class:
1
from masonite.commands import Command
2
3
class MyCommand(Command):
4
"""
5
Description of Command
6
7
command:signature
8
{user : A positional argument for the command}
9
{--f|flag : An optional argument for the command}
10
{--o|option=default: An optional argument for the command with default value}
11
"""
12
13
def handle(self):
14
pass
Copied!
Command's name, description and arguments are parsed from the Command docstring.

Name and Description

The docstring should start with the description of the command
1
"""
2
Description of Command
3
4
"""
Copied!
and then after a blank line you can define the command name.
1
"""
2
Description of Command
3
4
command_name
5
"""
Copied!

Positional Arguments

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:
1
"""
2
{name : Description of the name argument}
3
"""
Copied!
Inside the command, positional arguments can be retrieved with self.argument(arg_name)
1
def handle(self):
2
name = self.argument("name")
Copied!

Optional Arguments

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:
1
"""
2
command_name
3
{--iterations : Description of the iterations argument}
4
{--f|force : Description of the force argument}
5
"""
Copied!
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:
1
python craft command_name --f --iterations
2
python craft command_name --iterations --force
Copied!
If the optional argument is requiring a value you should add the = suffix:
1
"""
2
command_name
3
{--iterations= : Description of the iterations argument}
4
"""
Copied!
Here when using iterations, the user should provide a value.
1
python craft command_name --iterations 3
2
python craft command_name --iterations=3
Copied!
If the argument may or may not have a value, you can use the suffix =? instead.
1
"""
2
command_name
3
{--iterations=?: Description of the iterations argument}
4
"""
Copied!
1
python craft command_name --iterations
2
python craft command_name --iterations 3
Copied!
Finally if a default value should be used when no value is provided, add the suffix ={default}:
1
"""
2
command_name
3
{--iterations=3: Description of the iterations argument}
4
"""
Copied!
1
# iterations will be equal to 3
2
python craft command_name --iterations
3
# iterations will be equal to 1
4
python craft command_name --iterations 1
Copied!
Inside the command, optional arguments can be retrieved with self.option(arg_name)
1
def handle(self):
2
name = self.option("iterations")
Copied!

Printing Messages

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

Advanced

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.

Registering 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:
1
from some.place.YourCommand import YourCommand
2
3
class AppProvider(Provider):
4
def __init__(self, application):
5
self.application = application
6
7
def register(self):
8
self.application.make('commands').add(YourCommand())
Copied!
When you run python craft you will now see the command you added.