Creating a command-line interface

If you sync more than a few models, it’s nice to wrap that up in a command-line interface. This can be done easily using the Click library.

Example interface


import os
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"

import django

import click
from synctool.client import Client

client = Client(
def cli():
    """A tool for syncing data."""

@click.option("--clean/--no-clean", default=False)
@click.option("--images/--no-images", default=False)
def blogs(clean, images):
    """ Sync blogs """
    client.sync("blogs", clean=True)

if __name__ == "__main__":

Now you can sync data using a command like:

python blogs --clean

You can make this yet better by integrating with setuptools.

This would enable you to simplify it to something like:

sync blogs --clean

Further, if your application is installed in a virtualenv, you can call the command without needing to activate the virtualenv.