Search code examples
pythonpython-click

Python multiple click.options - got unexpected keyword argument


While using python click, I am not being able to pass the options to one of the method. Please consider following code.

import click

@click.command()
@click.option('--config', default='default.cfg', help = 'comfiguration file')
@click.option('--port', default=9093)
def foo(config_name, port):
    print('Function has been successfully called..!')

if __name__ == '__main__':
    foo()

The error I get is:

TypeError: foo() got an unexpected keyword argument 'config

and the stacktrace is:

Traceback (most recent call last):
  File "temp.py", line 10, in <module>
    foo()
  File "/home/sagar/anaconda3/lib/python3.6/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/home/sagar/anaconda3/lib/python3.6/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/sagar/anaconda3/lib/python3.6/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/sagar/anaconda3/lib/python3.6/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)

I do not understand why this is so. I read few similar questions regarding argument, that one cannot use help option, but in this case I think I am not doing anything that is not supported.

But the funny thing is: if I remove the --config option, (and of course the parameter from the function), then it works perfectly. I am sure one can use multiple options, I tried using type=string, but it also does not help.


Solution

  • You named the option --config, so click is trying to pass an argument named config to foo, but there's no config in foo's signature. Presumably, you want to write def foo(config, port): instead of def foo(config_name, port):.