title: Adding common parameters to groups with Click
body: <p>I am trying to use the Python library <a href="http://click.pocoo.org" rel="noreferrer">Click</a>, but struggle to get an example working. I defined two groups, one of which (<code>group2</code>) is meant to handle common parameters for this group of commands. What I want to achieve is that those common parameters get processed by the group function (<code>group2</code>) and assigned to the context variable, so they can be used by the actual commands.</p>
  
  <p>A use case would be a number of commands that require username and password, while some others don't (not even optionally).</p>
  
  <p>This is the code</p>
  
  <pre><code>import click
  
  
  @click.group()
  @click.pass_context
  def group1(ctx):
      pass
  
  
  @click.group()
  @click.option('--optparam', default=None, type=str)
  @click.option('--optparam2', default=None, type=str)
  @click.pass_context
  def group2(ctx, optparam):
      print 'in group2', optparam
      ctx['foo'] = create_foo_by_processing_params(optparam, optparam2)
  
  
  @group2.command()
  @click.pass_context
  def command2a(ctx):
      print 'command2a', ctx['foo']
  
  
  @group2.command()
  @click.option('--another-param', default=None, type=str)
  @click.pass_context
  def command2b(ctx, another_param):
      print 'command2b', ctx['foo'], another_param
  
  # many more more commands here...
  # @group2.command()
  # def command2x():
  # ...
  
  
  @group1.command()
  @click.argument('argument1')
  @click.option('--option1')
  def command1(argument1, option1):
      print 'In command2', argument1, option1
  
  cli = click.CommandCollection(sources=[group1, group2])
  
  
  if __name__ == '__main__':
      cli(obj={})
  </code></pre>
  
  <p>And this is the result when using command2:</p>
  
  <pre><code>$ python cli-test.py command2 --optparam=123
  &gt; Error: no such option: --optparam`
  </code></pre>
  
  <p>What's wrong with this example. I tried to follow the docs closely, but <code>opt-param</code> doesn't seem to be recognised.</p>