question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow a type to customize the prompt

See original GitHub issue

As 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:open
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
StephenCarbonicommented, Mar 15, 2020

Selecting the choice would be much nicer. Like this: https://github.com/Mckinsey666/bullet#bullet-lists-and-checkboxes

4reactions
SeanDScommented, Oct 27, 2019

I implemented numeric choices as a subclass of click.Choice:

class NumericChoice(click.Choice):
    def __init__(self, choices, **kwargs):
        choicepairs = []
        choicestrs = []
        for i, choice in enumerate(choices, start=1):
            choicepairs.append((str(i), choice))
            choicestrs.append("[{}] {}".format(i, choice))
        self.choicemap = dict(choicepairs)
        super().__init__(choicestrs, **kwargs)

    def convert(self, value, param, ctx):
        try:
            return self.choicemap[value]
        except KeyError as e:
            self.fail('invalid choice: %s. (choose from %s)' %
                      (value, ', '.join(self.choices)), param, ctx)

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Customize the Command Prompt in Windows
Open the Font tab in the Properties window. · Under the Size section, select the font size you want to set as default....
Read more >
How to Change / Set up bash custom prompt (PS1) in Linux
Learn how to customize and colorize your BASH shell prompt using PS1 in Linux, macOS/Unix. You need to change and set up prompt...
Read more >
How To Customize Bash Prompt in Linux - phoenixNAP
Tutorial on how to customize the BASH prompt in Linux permanently or temporary. Set bash shell prompt variables including changing color.
Read more >
about Prompts - PowerShell | Microsoft Learn
You can customize the prompt by creating your own Prompt function ... To display the script that creates the current Prompt function, type:....
Read more >
Customize Consent Prompts - Auth0
If you require a specialized consent prompt, for example, parental consent, you need to build your own custom consent form.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found