2

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

1
  • Regarding closing the question - well I did post all of my code as well as the Traceback so if you cannot duplicate this then I would say you might be more right than not - but if you can this I would think is a relevant question Commented Aug 22, 2015 at 1:47

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

try deleting *.pyc files

5 Comments

I wondered that but notice again that the correct line of code is in the trace @click.option('--search_phrase', '-s', multiple=True)
Further, I went looking for the pyc file and after not finding it Googled and confirmed that .pyc files are not created when the code is run from IDLE. Only when the module is imported. But again the trace still shows the error with the current line
oh you are right, that is not the problem. Did you notice your line 12 has a string that breaks into the next line? that is invalid syntax - is that only when you copy pasted into stack overflow? or is that in your code as well?
It is from copy pasting into SO I always struggle with the formatting
what I would do in your place is step into the click library source code and try to make sense of it (yey for open source). Your traceback shows the exception at line 1618, but looking in github that line github.com/mitsuhiko/click/blob/master/click/core.py#L1618 looks irrelevant, so I guess the github version is different from the version on your computer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.