Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option is lowercased upon decorating function #1052

Closed
ZenithDK opened this issue Jun 21, 2018 · 5 comments
Closed

Option is lowercased upon decorating function #1052

ZenithDK opened this issue Jun 21, 2018 · 5 comments
Labels

Comments

@ZenithDK
Copy link
Contributor

pip list
Package Version


...
click 6.7
...


@click.command()
@click.option("--someOption", help="Help text")
def test_fun(someOption):
    print("Test print!")

if __name__ == "__main__":
    test_fun()

This will give an error:

Traceback (most recent call last):
  File "AutoGenerateVoicePromptPackage.py", line 9, in <module>
    test_fun()
  File "C:\Python27\lib\site-packages\click\core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "C:\Python27\lib\site-packages\click\core.py", line 697, in main
    rv = self.invoke(ctx)
  File "C:\Python27\lib\site-packages\click\core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Python27\lib\site-packages\click\core.py", line 535, in invoke
    return callback(*args, **kwargs)
TypeError: test_fun() got an unexpected keyword argument 'someoption'

As you can see, it expects the casing to be "someoption" - and not "someOption" as I specified it.
I'm running this on Windows 10 with Python 2.7.14 - could that be a factor?

@jcrotts
Copy link
Contributor

jcrotts commented Jun 27, 2018

I think this is expected behavior see this line in core.py I was able to replicate what you are seeing on MacOS on the current master branch of click.

What specifically are you trying to accomplish?

@ZenithDK
Copy link
Contributor Author

ZenithDK commented Jun 27, 2018

This is wholly undocumented. I see the line of code and that is how I figured out it wasn't me that was causing the problem - but it is no documented.
Yes, all the examples use lowercase, but nowhere I can find does it say only lowercase is allowed (or that it is converted into lowercase).

I want camel casing of some of the arguments that I have - like "forceDeveloper" or "forceAll".
I would need to have all lowercase or "force-developer" (knowing it is converted to "force_developer" for instance).

@jcrotts
Copy link
Contributor

jcrotts commented Jun 27, 2018

I agree, it isn't explicitly documented, would you be interested in opening a documentation PR?

Also you can provide a token normalization function to allow case insensitive command line options.
See your modified example below:

import click
CONTEXT_SETTINGS = dict(token_normalize_func=lambda x: x.lower())
@click.command(context_settings=CONTEXT_SETTINGS)
@click.option("--someOption", help="Help text")
def test_fun(someoption):
    print("Test print!")

if __name__ == "__main__":
    test_fun()

This would make all of the following work:

$python test_fun.py --someOption someresponse"
$python test_fun.py --someoption someresponse"
$python test_fun.py --SOMEOPTION someresponse"

@ZenithDK
Copy link
Contributor Author

#1055

@jcrotts jcrotts added the docs label Jun 27, 2018
@jcrotts
Copy link
Contributor

jcrotts commented Oct 6, 2018

Resolved in #1055

@jcrotts jcrotts closed this as completed Oct 6, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants