Skip to content

Instantly share code, notes, and snippets.

@denibertovic
Created June 27, 2017 13:43
Show Gist options
  • Save denibertovic/0b007441a8626d224a667cbdebe42968 to your computer and use it in GitHub Desktop.
Save denibertovic/0b007441a8626d224a667cbdebe42968 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# REPRO: ./main.py subcmd --customer=customer1 cmd
# PROBLEM: dummy still get's prompted
# DESIRED: dummy get's read from the "config file" and not prompted
from functools import update_wrapper
from copy import deepcopy
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 customer_option(f):
# This should merge in all the stuff from the config file
def callback(ctx, param, value):
c = read_config(value)
for k, v in c.iteritems():
if k not in ctx.obj:
ctx.obj[k] = v
return value
return click.option('--customer', "-c",
callback=callback,
prompt=True,
is_eager=True,
help='Customer name.')(f)
@click.group()
@click.pass_context
def main(ctx, **kwargs):
ctx.obj = update_context({}, kwargs)
print "running main"
@click.group()
@click.option('--dummy', prompt=True, help="Dummy value that's different for each customer")
@customer_option
@click.pass_context
def subcmd(ctx, **kwargs):
ctx.obj = update_context(deepcopy(ctx.obj), kwargs)
print "runnning group"
@click.command('cmd')
@click.pass_context
def cmd(ctx):
print "running command"
main.add_command(subcmd)
subcmd.add_command(cmd)
if __name__ == '__main__':
a = main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment