Skip to content

Instantly share code, notes, and snippets.

@denibertovic
Created June 27, 2017 13:25
Show Gist options
  • Save denibertovic/99abae8565a2230007fd19bcec8eb1bb to your computer and use it in GitHub Desktop.
Save denibertovic/99abae8565a2230007fd19bcec8eb1bb to your computer and use it in GitHub Desktop.
Click repro1
#!/usr/bin/env python
# REPRO
# pip install -r click==6.6
# chmod +x main.py
# ./main.py --customer customer1
# PROBLEM: dummy get's prompted regardless if it's set from the "config file"
from functools import update_wrapper
import click
def update_context(initial, kwargs):
ctx = initial
for k, v in kwargs.iteritems():
ctx[k] = v
return ctx
def read_config(customer):
if customer == 'customer1':
return {'dummy': 'config value for customer1'}
else:
return {'dummy': 'something else'}
def pass_modified_context(f):
def new_func(*args, **kwargs):
c = read_config(kwargs['customer'])
for k, v in c.iteritems():
if k not in kwargs:
kwargs[k] = v
return f(click.globals.get_current_context(), *args, **kwargs)
return update_wrapper(new_func, f)
@click.command()
@click.option('--dummy', prompt=True, help="Dummy value that's different for each customer")
@click.option('--customer', prompt=True, help='Number of greetings.')
@pass_modified_context
def cli(ctx, **kwargs):
ctx.obj = update_context({}, kwargs)
print ctx.obj
print kwargs
if __name__ == '__main__':
a = cli()
print "exiting"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment