Skip to content

Commit b113d2f

Browse files
authored
Merge pull request #9 from bmwant/bmwant/choices-accepts-function
Choices should accept callable to generate options
2 parents 6a88177 + 361333a commit b113d2f

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

PyInquirer/prompt.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@ def prompt(questions, answers=None, **kwargs):
2020
eventloop = kwargs.pop('eventloop', None)
2121
kbi_msg = kwargs.pop('keyboard_interrupt_msg', 'Cancelled by user')
2222

23-
2423
for question in questions:
2524
# import the question
26-
if not 'type' in question:
25+
if 'type' not in question:
2726
raise PromptParameterException('type')
28-
if not 'name' in question:
27+
if 'name' not in question:
2928
raise PromptParameterException('name')
30-
if not 'message' in question:
29+
if 'message' not in question:
3130
raise PromptParameterException('message')
3231
try:
32+
choices = question.get('choices')
33+
if choices is not None and callable(choices):
34+
question['choices'] = choices(answers)
35+
3336
_kwargs = {}
3437
_kwargs.update(kwargs)
3538
_kwargs.update(question)
@@ -38,6 +41,7 @@ def prompt(questions, answers=None, **kwargs):
3841
message = _kwargs.pop('message')
3942
when = _kwargs.pop('when', None)
4043
filter = _kwargs.pop('filter', None)
44+
4145
if when:
4246
# at least a little sanity check!
4347
if callable(question['when']):

README.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,35 +248,35 @@ Question Properties
248248

249249
A question is a dictionary containing question related values:
250250

251-
- type: (String) Type of the prompt. Defaults: input - Possible values:
251+
- ``type``: (String) Type of the prompt. Defaults: input - Possible values:
252252
input, confirm, list, rawlist, expand, checkbox, password, editor
253-
- name: (String) The name to use when storing the answer in the answers
253+
- ``name``: (String) The name to use when storing the answer in the answers
254254
hash. If the name contains periods, it will define a path in the
255255
answers hash.
256-
- message: (String\|Function) The question to print. If defined as a
256+
- ``message``: (String\|Function) The question to print. If defined as a
257257
function, the first parameter will be the current inquirer session
258258
answers.
259-
- default: (String\|Number\|Array\|Function) Default value(s) to use if
259+
- ``default``: (String\|Number\|Array\|Function) Default value(s) to use if
260260
nothing is entered, or a function that returns the default value(s).
261261
If defined as a function, the first parameter will be the current
262262
inquirer session answers.
263-
- choices: (Array\|Function) Choices array or a function returning a
263+
- ``choices``: (Array\|Function) Choices array or a function returning a
264264
choices array. If defined as a function, the first parameter will be
265265
the current inquirer session answers. Array values can be simple
266266
strings, or objects containing a name (to display in list), a value
267267
(to save in the answers hash) and a short (to display after
268268
selection) properties. The choices array can also contain a
269269
Separator.
270-
- validate: (Function) Receive the user input and should return true if
270+
- ``validate``: (Function) Receive the user input and should return true if
271271
the value is valid, and an error message (String) otherwise. If false
272272
is returned, a default error message is provided.
273-
- filter: (Function) Receive the user input and return the filtered
273+
- ``filter``: (Function) Receive the user input and return the filtered
274274
value to be used inside the program. The value returned will be added
275275
to the Answers hash.
276-
- when: (Function, Boolean) Receive the current user answers hash and
276+
- ``when``: (Function, Boolean) Receive the current user answers hash and
277277
should return true or false depending on whether or not this question
278278
should be asked. The value can also be a simple boolean.
279-
- pageSize: (Number) Change the number of lines that will be rendered
279+
- ``pageSize``: (Number) Change the number of lines that will be rendered
280280
when using list, rawList, expand or checkbox.
281281

282282
User Interfaces and Styles

examples/list.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
from examples import custom_style_2
1212

1313

14+
15+
def get_delivery_options(answers):
16+
options = ['bike', 'car', 'truck']
17+
if answers['size'] == 'jumbo':
18+
options.append('helicopter')
19+
return options
20+
21+
1422
questions = [
1523
{
1624
'type': 'list',
@@ -34,7 +42,13 @@
3442
'message': 'What size do you need?',
3543
'choices': ['Jumbo', 'Large', 'Standard', 'Medium', 'Small', 'Micro'],
3644
'filter': lambda val: val.lower()
37-
}
45+
},
46+
{
47+
'type': 'list',
48+
'name': 'delivery',
49+
'message': 'Which vehicle you want to use for delivery?',
50+
'choices': get_delivery_options,
51+
},
3852
]
3953

4054
answers = prompt(questions, style=custom_style_2)

tests/test_example_list.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ def test_list(example_app):
3131
example_app.write(keys.ENTER)
3232
example_app.expect(textwrap.dedent("""\
3333
? What size do you need? Jumbo
34-
{
35-
"size": "jumbo",
36-
"theme": "Order a pizza"
37-
}
38-
34+
? Which vehicle you want to use for delivery? (Use arrow keys)
35+
❯ bike
36+
car
37+
truck
38+
helicopter"""))
39+
example_app.write(keys.ENTER)
40+
example_app.expect(textwrap.dedent("""\
41+
? Which vehicle you want to use for delivery? bike
42+
{'delivery': 'bike', 'size': 'jumbo', 'theme': 'Order a pizza'}
3943
"""))

0 commit comments

Comments
 (0)