Allow a type to customize the prompt
See original GitHub issueAs of Click 7, click.Choice
appears only to support choice prompting by string, i.e. the user must specify the exact string (modulo case sensitivity) from the choices. It would be nice if Click could support two additional modes of selection:
Numeric:
Choose from:
[1] Choice A...
[2] Choice B...
[3] Choice C...
Enter the index for one of the above choices: 2
"Choice B..." was selected
or partial matches as long as what the user enters is unique:
Choose from:
This thing
That thing
Those things
Enter choice: thi
"This thing" was selected
(“thi” is unique amongst the three strings so is chosen)
I thought it would be easy to implement this by subclassing click.Choice
but unfortunately not: click.Choice
does not provide the initial prompt message (only the prompt after an invalid choice) - this is provided by click.prompt
from the specified choices list. The prompt message in at least the first example above would require to be changed to add numeric indexes. It seems to me therefore that this functionality requires more structural changes to click and so I thought I’d submit this issue.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Selecting the choice would be much nicer. Like this: https://github.com/Mckinsey666/bullet#bullet-lists-and-checkboxes
I implemented numeric choices as a subclass of
click.Choice
:It works for me, but I’m targeting Python 3.6+ only. If this might be useful I can clean it up and make a pull request at some point. It might be best to add this as an option to the existing
click.Choice
, though.For partial string matching, perhaps
difflib
would be useful. I haven’t thought how to do that yet.