I am trying to use decorators with the Click framework to perform work common to multiple commands without using arguments in the root element of the group (see https://github.com/pallets/click/issues/295 for why). In short, something like this:
@click.group()
def main():
pass
@main.command()
@click.option('--argument-a')
@parse_config_file
@init_session
def do_something_in_session(argument_a, config, session):
# code
return
where the decorators also have arguments:
def init_session(f):
@wraps(f)
@click.option('--argument-B')
def wrapper(*args, **kwargs):
# do something with argument-B, and add session to list of arguments.
del kwargs['argument-B']
kwargs['session'] = session_created_above
return f(*args,**kwargs)
return wrapper
def parse_config_file(f):
@wraps(f)
@click.option('--argument-C')
def wrapper(*args, **kwargs):
# do something with argument-C, and add config to list of arguments.
del kwargs['argument-C']
kwargs['config'] = config_parsed_above
return f(*args,**kwargs)
return wrapper
However, when running do_something_in_session --help
, only the arguments of the decorator just above the function definition, in this case @init_session
, are shown. Is there a way for me to decorate the decorators so that Click parses the arguments properly?