Python – Why does the use of click.argument produce “got an unexpected keyword argument ‘help’

pythonpython-click

Running the following code results in this error:

TypeError: init() got an unexpected keyword argument 'help'

Code:

import click

@click.command()
@click.argument('command', required=1, help="start|stop|restart")
@click.option('--debug/--no-debug', default=False, help="Run in foreground")
def main(command, debug):
    print (command)
    print (debug)

if __name__ == '__main__':
    main()

Full error output:

$ python3 foo.py start
Traceback (most recent call last):
  File "foo.py", line 5, in <module>
    @click.option('--debug/--no-debug', default=False, help="Run in foreground")
  File "/home/cbetti/python/lib/python3/dist-packages/click-4.0-py3.4.egg/click/decorators.py", line 148, in decorator
    _param_memo(f, ArgumentClass(param_decls, **attrs))
  File "/home/cbetti/python/lib/python3/dist-packages/click-4.0-py3.4.egg/click/core.py", line 1618, in __init__
    Parameter.__init__(self, param_decls, required=required, **attrs)
TypeError: __init__() got an unexpected keyword argument 'help'

Why does this error occur?

Best Answer

I run into this again and again because the trace output does not correspond to the parameter causing the error. Notice ArgumentClass in the trace, that's your hint.

'help' is an acceptable parameter to @click.option. The click library prefers, however, that you document your own arguments. The @click.argument help parameter is causing this exception.

This code works: (notice the lack of , help="start|stop|restart" in @click.argument)

import click

@click.command()
@click.argument('command', required=1)
@click.option('--debug/--no-debug', default=False, help="Run in foreground")
def main(command, debug):
    """ COMMAND: start|stop|restart """
    print (command)
    print (debug)

if __name__ == '__main__':
        main()

Output:

$ python3 foo.py start
start
False

Help Output:

Usage: test.py [OPTIONS] COMMAND

  COMMAND: start|stop|restart

Options:
  --debug / --no-debug  Run in foreground
  --help                Show this message and exit.
Related Topic