How do I get rid of this TypeError when using python's click library?

So I thought using the python click library to build a simple command line tool would be pretty simple. It seems I am stuck on something that is just not clicking with me. I have the following code:

import click
import json
import os.path
import sys


@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
              help='The person to greet.')
@click.argument('title')
@click.argument('category')
@click.argument('tech')
@click.argument('problemNotes')
@click.argument('solutionNotes')


def hello(count, name, title, category, tech, problemNotes, solutionNotes):
    print(tech + problemNotes + solutionNotes)


if __name__ == '__main__':
    hello()

This seems pretty straightforward. The CL tool should take in some options and arguments and then print some out. I then type the following at my terminal to invoke the tool:

python bugbook.py --name="steve" "Annoying stack bug" "compile bug" "xcode" "annoying" "get meowed"

But I get the following error:

Traceback (most recent call last):
  File "bugbook.py", line 22, in <module>
    hello()
  File "/Library/Python/2.7/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/Library/Python/2.7/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Library/Python/2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
TypeError: hello() got an unexpected keyword argument 'solutionnotes'

Strange, I think. It seems this error is casued by:

if __name__ == '__main__':
    hello()

Which actually invokes the main function fails to pass in the right parameters or something, but I have tried tinkering and just can't get it working. What obvious thing am i missing??

Upvotes: 0

Views: 1199

Answers (1)

RaminNietzsche
RaminNietzsche

Reputation: 2791

Token Normalization

New in version 2.0.

Starting with Click 2.0, it’s possible to provide a function that is used for normalizing tokens. Tokens are option names, choice values, or command values. This can be used to implement case insensitive options, for instance.

In order to use this feature, the context needs to be passed a function that performs the normalization of the token. For instance, you could have a function that converts the token to lowercase:

CONTEXT_SETTINGS = dict(token_normalize_func=lambda x: x.lower())

@click.command(context_settings=CONTEXT_SETTINGS)
@click.option('--name', default='Pete')
def cli(name):
    click.echo('Name: %s' % name)

Read more

Upvotes: 1

Related Questions