How to add custom console commands and handle them?

Magento 2 has a lot of console commands.
The console command can be of 2 types:

You can add and handle new commands with your custom module.
Let see as an example the cache:enable core command.
It is declared by Mage_Backend core module in di.xml file:

It is implemented in \Magento\Backend\Console\Command\CacheEnableCommand class.
Please note that a command must have a name set:

You must place the handling into the overriden execute method:

See also:

The simplest example