Command-line Interface

Let’s learn about the usage of the command-line interface in our project.

So far, we’ve created the domain models, the serializers, the use cases, and the repository, but we’re still missing a system that glues everything together. This system has to get the call parameters from the user, initialize a use case with a repository, run the use case that fetches the domain models from the repository, and return them to the user. Let’s see now how the architecture we’ve just created can interact with an external system like a CLI (command-line interface). The power of clean architecture is that the external systems are pluggable, which means that we can defer the decision about the details of the system we want to use. In this case, we want to give the user an interface to query the system and get a list of the rooms contained in the storage system. The simplest choice is a command-line tool. Later, we’ll create a REST endpoint and expose it through a web server. When we do, it’ll be very clear why the architecture we’ve created is so powerful.

Execute a Python file

For now, let’s create a file cli.py in the root directory of our project. This is a simple Python script that doesn’t need any specific options to run, since it simply queries the storage for all the domain models contained in it. The contents of the file can be retrieved with the following code:

Get hands-on with 1200+ tech skills courses.