Say I have a function created not by def
but by a partial()
call (or even just by assignment). In the example below, how can I add bar
as a click sub-command to the cli
group? I can't use the decorator approach (as with foo
). My failed approaches are shown below in-line.
import functools
import click
@click.group()
def cli():
pass
@cli.command()
def foo(myname="foo"):
print(f"I am {myname}")
bar = functools.partial(foo, myname="bar")
# this has no effect
# cli.command(bar)
# results in: AttributeError: 'functools.partial' object has no attribute 'name'
# cli.add_command(bar)
# results in: AttributeError: 'functools.partial' object has no attribute 'hidden'
# cli.add_command(bar, name="bar")
if __name__ == "__main__":
cli()
UPDATE: Actually, it looks like the partial
is the culprit here. This answer in a different but related thread, points out that partial objects are "missing certain attributes, specifically __module__
and __name__
".