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.

Choice - dropdown list instead of all comma separated values in one line

See original GitHub issue

Hi All,

This is more a question. I’m using a choice prompt with a list of values which are quite long (each value is more than 30 characters in length).

esclation_policy = click.prompt('Please choose the escalation policy',
                                    type=click.Choice(esclation_policy_list),
                                    default='Default Team')

I would like to print each value on a separate line but by default they are printed comma separated until line wraps - which is very unreadable.

Is there any way to achieve the below ?

Please choose the escalation policy:
value1
value2
value3
...
valueN

instead of:

Please choose the escalation policy(value1, value2, value3, ..., valueN)

PS: I couldn’t find in documentation support for this.

Thanks & Regards, Constantin

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
davidismcommented, Mar 21, 2019

This might become possible with #938.

1reaction
jcrottscommented, Mar 21, 2019

You could monkey patch _build_prompt to get the behavior you want.

import os
import click
from click import Choice

def _build_prompt(text, suffix, show_default=False, default=None, show_choices=True, type=None):
    prompt = text + ":"
    if type is not None and show_choices and isinstance(type, Choice):
        prompt += "\n"
        prompt += "\n".join(map(str, type.choices))
    if default is not None and show_default:
        prompt = '%s [%s]' % (prompt, default)
    return prompt + "\n" + ">"

click.termui._build_prompt = _build_prompt

escalation_policy_list = ["value1", "value2", "value3"]

esclation_policy = click.prompt('Please choose the escalation policy',
                                type=click.Choice(escalation_policy_list))

You would also need to subclass or patch Choice to change the behavior of the invalid choice error message.

I believe this is the part you would need to override. https://github.com/pallets/click/blob/2c622eed402c64e1645db3904c2410f6c05fbf19/click/types.py#L129

Feel free to continue the discussion, however I am going to close this issue because it isn’t a bug/ isn’t related to improving Click.

Read more comments on GitHub >

github_iconTop Results From Across the Web

excel dropdown list - commas used to separate items in Source
Hey guys, I am trying to create a dropdown list in excel using the Data Validation button. ... Do the commas show in...
Read more >
Better way to load dropdown list from comma separated string
I have a string of comma separated values.
Read more >
Create a dropdown list from comma-separated items - YouTube
00:00 Intro00:08 Why use list of items00:18 What it all looks like00:30 Creating a list from items01:43 Duplicating the dropdown02:01 A ...
Read more >
Comma Delimited Text makes their own Dropdowns in ...
In this video we allow the user to write their own dropdowns using comma delimited single line text fields and then they can...
Read more >
The Ultimate Guide to Dropdown Lists in Excel
Use Comma Separated List of Values for List Items. The first method is the most basic where all items are entered in the...
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