Why does my use of click.argument produce "got an unexpected keyword argument 'help'?
Asked Answered
C

5

44

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?

Chaney answered 1/7, 2015 at 23:30 Comment(0)
C
59

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.
Chaney answered 1/7, 2015 at 23:30 Comment(0)
U
8

You are defining commands as arguments. Note that click has a better way to define commands then what you are trying here.

@click.group()
def main():
    pass

@main.command()
def start():
    """documentation for the start command"""
    print("running command `start`")

@main.command()
def stop():
    """documentation for the stop command"""
    print("running command `stop`")

if __name__ == '__main__':
    main()

will result in the following default help text:

Usage: test_cli.py [OPTIONS] COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  start  documentation for the start command
  stop   documentation for the stop command

Having said that, should you really need arguments, you cannot use the help parameter. The click documentation indeed states that you should document your own arguments. However, I have no idea how to do that. Any hints?

EDIT

As mentioned in the comments: this is to align with the Unix standard to document arguments in the main help text.

Upswing answered 17/8, 2015 at 13:33 Comment(3)
If you """ document the function annotated with @click.command, that content will appear in the --help message. Click doesn't provide a way to associate help text with arguments at all, so writing a free-form description seems to be the only way.Chaney
Indeed. I also found documentation here saying: This is to follow the general convention of Unix tools of using arguments for only the most necessary things and to document them in the introduction text by referring to them by name.Upswing
You can add these commands to main as the decorator by using @main.command() instead of @click.command().Nanci
L
5

click library does not allow -help parameter inside click.arguments (including the current version 6.7 when this comment has been written). But, you can use the -help parameter inside click.options.
You can check current click documentation at http://click.pocoo.org/6/documentation/ or previous at http://click.pocoo.org/5/documentation/ this behaviour has not change recently. Then, it is a WAD. It is not a BUG.

Ladyinwaiting answered 28/5, 2018 at 15:39 Comment(2)
What does WAD mean ?Rowles
"Works as designed" acronymfinder.com/Works-As-Designed-(WAD).html. Typically used by software and hardware manufacturers to avoid change a specific product behaviour: it means, the product is doing what is expected.Ladyinwaiting
L
1

For me it was because my variable looked like DnsCryptExractDir and I have to chnage it too this dnscryptexractdir because *args could not find it.

Lapith answered 29/1, 2018 at 8:19 Comment(0)
I
1

For same error I got it because my argument name was url_Hook (camelCase). After I changed it to url_hook it got resolved.

Integrand answered 18/9, 2018 at 22:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.