title: python click subcommand unified error handling
body: <p>In the case where there are command groups and every sub-command may raise exceptions, how can I handle them all together in one place?</p>
  
  <p>Given the example below:</p>
  
  <pre><code>import click
  
  
  @click.group()
  def cli():
      pass
  
  @cli.command()
  def foo():
      pass
  
  if __name__ == '__main__':
      cli()
  </code></pre>
  
  <p>Both <code>cli</code> and <code>foo</code> may raise. I know that one possible solution is to place <code>try-except</code> around <code>cli()</code> in the <code>if</code> clause. But that does not work when you distribute a package. In <em>setup.py</em>, you have to specify an entry point (in this case, <code>cli</code>). The <code>if</code> clause will not be executed.</p>