PyNEwbie
PyNEwbie

Reputation: 4940

Python Click Error from help string associated with an option

I am trying to learn how to use Python-click. I was not able to use a help parameter with one of my options so I finally gave up and changed the code to not include help for that option. However, despite closing and restarting Python and now rebooting my computer the error message associated with trying to use the help parameter is still appearing.

Code:

import click

def something():
    pass

@click.command()
@click.argument('dest_dir',type=click.Path(exists=True, readable=True,
                resolve_path=True, dir_okay=True), 
                help='Location of directory where results will be saved')
@click.option('--use_terms', is_flag=True, 
              help='Process strings based on terms or phrases')
@click.option('--use_query', is_flag=True, help='Process string based on
               search query')
@click.option('--search_phrase', '-s', multiple=True)


def do_process(dest_dir,use_terms,use_query,*search_phrase):
""" testing setting parameters for snip tables"""
    outref = open('e:\\myTemp\\testq.txt')
    ms = dest_dir + '\n'
    if use_terms:
        ms += use_term + '\n'
    else:
        ms += use_query + '\n'
    for each in search_phrase:
        x = something()
        ms += each + '\n'
    outref.writelines(ms)
    outref.close()


if __name__ == "__main__":
    do_process()

Originally for the last @click.option I had

@click.option('--search_phrase', '-s', multiple=True, help='The search phrase to use')

I kept getting an error message that I could not solve relating to having an unknown parameter help. I ditched it, changed to what is above and now I am getting a similar error,

I then shut down Python, I closed my module and then restarted Python opened and ran my code again and still getting this error message

Traceback:

Traceback (most recent call last):
 File "C:\Program Files\PYTHON\snipTables\test_snip_click.py", line 14, in <module>
@click.option('--search_phrase', '-s', multiple=True)
File "C:\Program Files\PYTHON\lib\site-packages\click\decorators.py", line 148, in decorator
_param_memo(f, ArgumentClass(param_decls, **attrs))
 File "C:\Program Files\PYTHON\lib\site-packages\click\core.py", line 1618, in __init__
Parameter.__init__(self, param_decls, required=required, **attrs)
TypeError: __init__() got an unexpected keyword argument 'help'

So then I shut down Python Idle, I saved and closed my code and then restarted Python, reopened my code, but I am still getting the same traceback except notice that the traceback has the line of code I switched to after beating my head hard against the monitor and giving up

I am getting ready to reboot but am really curious as to the cause.

I rebooted and still am getting the same error

Renaming the file and running again did not change outcome - same traceback

Upvotes: 2

Views: 3135

Answers (2)

PyNEwbie
PyNEwbie

Reputation: 4940

The problem is that click does not accept a help string with an argument parameter. It is interesting behavior. The help string associated with the argument will be the string in the function that processes the argument and options.

The error message will always show up associated with the last option. So the correct code for this example would be

import click

def something():
    pass

@click.command()
@click.argument('dest_dir',type=click.Path(exists=True, readable=True,
                resolve_path=True, dir_okay=True)
##Notice no help string here

@click.option('--use_terms', is_flag=True, 
              help='Process strings based on terms or phrases')
@click.option('--use_query', is_flag=True, help='Process string based on
               search query')
@click.option('--search_phrase', '-s', multiple=True)


def do_process(dest_dir,use_terms,use_query,*search_phrase):
""" testing setting parameters for snip tables"""
    outref = open('e:\\myTemp\\testq.txt')
    ms = dest_dir + '\n'
    if use_terms:
        ms += use_term + '\n'
    else:
        ms += use_query + '\n'
    for each in search_phrase:
        x = something()
        ms += each + '\n'
    outref.writelines(ms)
    outref.close()



if __name__ == "__main__":
    do_process()

This runs fine the problem I was originally having is that click was not doing a good job of explaining the source of the error. Above, even though I got rid of the help string in the option the click parser associates the help string from the argument with the last option it parses.

Upvotes: 5

Iftah
Iftah

Reputation: 9572

Maybe you renamed the source file and you are running an old version that was compiled with the previous name?

try deleting *.pyc files

Upvotes: 0

Related Questions