You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been converting a sprawling argparse CLI over to Click (and enjoying the improvements, btw), and I've run into a bit of an incompatibility between the two that I think should be easy for click to solve.
In short, this is a feature request for a case insensitive variant of click.Choice, or a general solution that handles that use case well.
I had case insensitive choice options like this before:
However, I was a bit surprised that this was necessary -- I would expect Click to handle this with a keyword argument of some kind, off the shelf.
This could be a special case for doing string preprocessing, but it's maybe more justifiable as a feature on ParamType, something like argument_preprocessor=func, which would allow all ParamTypes to hook in some kind of user-supplied function.
I'm thinking of a final result something like click.Choice(('apple', 'orange'), argument_preprocessor=str.lower) for my own use case, but also supporting other potentially interesting things.
Perhaps token_normalize_func should additionally be a parameter on click.Choice, though I am not sure if you really want this configuration on a per-option basis.
Sorry for not checking back on this sooner. I forgot about it. 😅
@untitaker I think there are legitimate use-cases for being case insensitive some places, but not others. That rules out changing the tokenizer as an option.
My particular need pertains to a CLI which wraps a REST API I'm working with.
As much as possible I want to be case-insensitive, but rarely I need to consume strings destined for the backend API.
Being able to patch over the tokenizer used for a given option would be perfect, and be a bit more flexible than case-insensitivity specifically for Choice types.
It also sounds harder to implement though.
I think a good number of people probably have type=str.lower in their Argparse structure, but require case-sensitivity elsewhere.
I don't like that transitioning those options required implementing my own ParamType.
I guess this depends a bit on how important feature parity with Argparse is. I don't think that custom types count for that -- that's an extension point of Click, not "out of the box" functionality.
Activity
untitaker commentedon May 3, 2016
You can already implement this globally by passing extra options to your main command/group:
Perhaps
token_normalize_funcshould additionally be a parameter onclick.Choice, though I am not sure if you really want this configuration on a per-option basis.stuaxo commentedon May 5, 2016
Found myself wanting something a bit like this, just now - I tried this:
But option doesn't take the context_settings_func
Though in my case, maybe being able to pass a dict like
{"pass": True, "fail": False}to Choice would work too.untitaker commentedon May 5, 2016
Please re-read what I wrote above.
stuaxo commentedon May 5, 2016
I had a look, but only want it on the one option, not all options of the command.
stuaxo commentedon May 5, 2016
Will go with subclassing Choice :)
untitaker commentedon May 5, 2016
I assumed you misread and thought context_settings_func was a kwarg for
click.option.Subclassing is definetly the best solution so far.
stuaxo commentedon May 5, 2016
np, here's what I came up with (based on code @ the start of this ticket)
stuaxo commentedon May 5, 2016
Oh, and example usage:
@click.option("--force-result", default=None, type=DictChoice({'fail': False, 'pass': True}))sirosen commentedon Jun 13, 2016
Sorry for not checking back on this sooner. I forgot about it. 😅
@untitaker I think there are legitimate use-cases for being case insensitive some places, but not others. That rules out changing the tokenizer as an option.
My particular need pertains to a CLI which wraps a REST API I'm working with.
As much as possible I want to be case-insensitive, but rarely I need to consume strings destined for the backend API.
Being able to patch over the tokenizer used for a given option would be perfect, and be a bit more flexible than case-insensitivity specifically for Choice types.
It also sounds harder to implement though.
I think a good number of people probably have
type=str.lowerin their Argparse structure, but require case-sensitivity elsewhere.I don't like that transitioning those options required implementing my own
ParamType.I guess this depends a bit on how important feature parity with Argparse is. I don't think that custom types count for that -- that's an extension point of Click, not "out of the box" functionality.
Add `case_sensitive=False` as an option to Choice
Add `case_sensitive=False` as an option to Choice
Merge #1576
Merge #37
Update click to 7.0 (#97)