v3.0
Official Packages
Masonite Essentials
Tutorials
Creating a Blog
Creating Commands

Introduction

It's extremely simple to add commands to Masonite via the craft command tool and Service Providers. If you have been using Masonite for any amount of time you will learn that commands are a huge part of developing web applications with Masonite. We have made it extremely easy to create these commands and add them to craft to build really fast personal commands that you might use often.
Masonite uses the Cleo package for creating and consuming commands so for more extensive documentation on how to utilize commands themselves, how to get arguments and options, and how to print colorful text to the command line.
Read more about Cleo by visiting the Cleo Documentation.

Getting Started

You can create commands by using craft itself:
terminal
1
$ python craft command Hello
Copied!
This will create a app/commands/HelloCommand.py file with boilerplate code that looks like this:
app/commands/HelloCommand.py
1
""" A HelloCommand Command """
2
from cleo import Command
3
4
5
class HelloCommand(Command):
6
"""
7
Description of command
8
9
command:name
10
{argument : description}
11
"""
12
13
def handle(self):
14
pass
Copied!
Let's create a simple hello name application which prints "hello your-name" to the console.
Where it says command:name inside the docstring we can put hello and inside the argument we can put name like so:
app/commands/HelloCommand.py
1
""" A HelloCommand Command """
2
from cleo import Command
3
4
5
class HelloCommand(Command):
6
"""
7
Say hello to you
8
9
hello
10
{name : Your name}
11
"""
12
13
def handle(self):
14
pass
Copied!
Inside the handle method we can get the argument passed by specifying self.argument('name'). Simply put:
app/commands/HelloCommand.py
1
""" A HelloCommand Command """
2
from cleo import Command
3
4
5
class HelloCommand(Command):
6
"""
7
Say hello to you
8
9
hello
10
{name : Your name}
11
"""
12
13
def handle(self):
14
print('Hello {0}'.format(self.argument('name')))
Copied!
That's it! Now we just have to add it to our craft command.

Adding Our Command To Craft

We can add commands to craft by creating a Service Provider and registering our command into the container. Craft will automatically run all the register methods on all containers and retrieve all the commands.
Let's create a Service Provider:
terminal
1
$ python craft provider Hello
Copied!
This will create a provider in app/providers/HelloProvider.py that looks like:
app/providers/HelloProvider.py
1
''' A HelloProvider Service Provider '''
2
from masonite.provider import ServiceProvider
3
4
5
class HelloProvider(ServiceProvider):
6
7
def register(self):
8
pass
9
10
def boot(self):
11
pass
Copied!
Let's import our command and register it into the container. Also because we are only registering things into the container, we can set wsgi = False so it is not ran on every request and only before the server starts:
app/providers/HelloProvider.py
1
''' A HelloProvider Service Provider '''
2
from masonite.provider import ServiceProvider
3
from app.commands.HelloCommand import HelloCommand
4
5
class HelloProvider(ServiceProvider):
6
7
wsgi = False
8
9
def register(self):
10
self.app.bind('HelloCommand', HelloCommand())
11
12
def boot(self):
13
pass
Copied!
Make sure you instantiate the command. Also the command name needs to end in "Command". So binding HelloCommand will work but binding Hello will not. Craft will only pick up commands that end in Command. This is also case sensitive so make sure Command is capitalized.

Adding The Service Provider

Like normal, we need to add our Service Provider to the PROVIDERS list inside our config/providers.py file:
config/providers.py
1
from app.providers.HelloProvider import HelloProvider
2
3
PROVIDERS = [
4
...
5
# Application Providers
6
UserModelProvider,
7
MiddlewareProvider,
8
9
# New Hello Provider
10
HelloProvider,
11
]
Copied!
That's it! Now if we run:
terminal
1
$ python craft
Copied!
We will see our new hello command:
terminal
1
help Displays help for a command
2
hello Say hello to you
3
install Installs all of Masonite's dependencies
Copied!
and if we run:
terminal
1
$ python craft hello Joseph
Copied!
We will see an output of:
terminal
1
Hello Joseph
Copied!
Last modified 8mo ago