I have a class that gets initialized with a previously unknown number of arguments and I want it to be done on CLI using Python's click package. My issue is that I can't manage to initialize it and run a click command:
$ python mycode.py arg1 arg2 ... argN click_command
Setting a defined number of arguments, like nargs=5, solves the issue of missing command but obligates me to input 5 arguments before my command. With variadic arguments like nargs=-1, click doesn't recognize click_command as a command.
How can I input n-many arguments, and then run the command using click?
import click
class Foo(object):
def __init__(self, *args):
self.args = args
def log(self):
print('self.args:', self.args)
pass_foo = click.make_pass_decorator(Foo)
@click.group()
@click.argument('myargs', nargs=-1)
@click.pass_context
def main(ctx, myargs):
ctx.obj = Foo(myargs)
print("arguments: ", myargs)
@main.command()
@pass_foo
def log(foo):
foo.log()
main()
I expect to be able to run a click command after passing n-many args to my Foo() class, so I can initialize it and run its log() method as a CLI command, but the output is:
Error: Missing command