OK. I got it working myself by analyzing the source code.
To do that you need a cli module inside your custom module. Then a python file mycommand.py inside of it.
The mycommand.py contains something like this:
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
from odoo.cli.command import Command
class MyCommand(Command):
"""Run a custom command"""
def run(self, args):
print "Custom command 'mycommand' worked.\n"
To get it correctly imported, you need the following in the __init__.py of your module:
from . import cli
And then you need the following in the cli/__init__.py:
import mycommand
To run the custom command, you need to type:
$ odoo --addons-path=/absolute/path/to/my/custom/addons mycommand
custom command 'mycommand' worked.
Here the custom command name "mycommand" is the lowercase name of the class inheriting from Command, i.e. "MyCommand". Note that the order of parameters passed to the odoo script matters: if you put the --addons-path after the custom command name, it won't work.
Interestingly, if you want to also pass odoo configuration to the command, it should go at the end of your parameters like this:
$ odoo --addons-path=/absolute/path/to/my/custom/addons mycommand -c odoo.conf