Consider you have a function run()
decorated as click.command() and multiple click.options() for default values and more specifications. In this run
function I want to call my other 'sub' functions of the tool one by one with the given parameters.
@click.command("run")
@click.options(_some_options)
def run(ctx, par1: str, par2: str, par3: int):
ctx.invoke(some_other_func, par1=par1, par2=par2, par3=par3)
This is running and works well so far. But now I'd like to open up the possibility to insert multiple values for the second parameter (par2) as a list of strings and execute the run function for each of the given strings in the list. Imagine an input like:
run(par1 = "hello", par2 = ["man", "woman", "child"], par3 = 3)
Expected Output:
"hello man 3"
"hello woman 3"
"hello child 3"
I tried using a decorator like:
def decorator(func):
@wraps(func)
def run_wrapper(ctx, par1:str, par2:list, par3:int)
run_unwrapped = run.__wrapped__
for var in par2:
run_unwrapped(ctx, par1, par2, par3)
return run_wrapper
and this worked outside of the context of click
but with click it doesn't. I read that one should use
the @click.pass_context
click.palletsprojects.com/en/7.x/commands/#decorating-commands instead but I have difficulties with this. Especially accessing the given parameters and the multiple calls of "run" just do not work.. Another difficulty is the input of a arbitrary long list - couldn't find a solution for this as well (multiple=True in the slick.options() does not work as I expected..).
Is a wrapper for this task actually a good idea? I thought of using this as I do not want to change my whole code and clutter it with for-loops.. but if there is a better idea I'd be grateful for it!
I'm a beginner with Python and Stack overflow - please be kind :)