Skip to content

Commit 5fe6987

Browse files
committed
Merge branch 'master' of github.com:CITGuru/PyInquirer into dev
2 parents d68585d + b113d2f commit 5fe6987

File tree

5 files changed

+43
-20
lines changed

5 files changed

+43
-20
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']):

PyInquirer/prompts/input.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
`input` type question
44
"""
5+
import inspect
56
from __future__ import print_function, unicode_literals
67
from prompt_toolkit.token import Token
78
from prompt_toolkit.shortcuts import create_prompt_application
@@ -17,7 +18,7 @@ def question(message, **kwargs):
1718
default = kwargs.pop('default', '')
1819
validate_prompt = kwargs.pop('validate', None)
1920
if validate_prompt:
20-
if issubclass(validate_prompt, Validator):
21+
if inspect.isclass(validate_prompt) and issubclass(validate_prompt, Validator):
2122
kwargs['validator'] = validate_prompt()
2223
elif callable(validate_prompt):
2324
class _InputValidator(Validator):

README.rst

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

269269
A question is a dictionary containing question related values:
270270

271-
- type: (String) Type of the prompt. Defaults: input - Possible values:
271+
- ``type``: (String) Type of the prompt. Defaults: input - Possible values:
272272
input, confirm, list, rawlist, expand, checkbox, password, editor
273-
- name: (String) The name to use when storing the answer in the answers
273+
- ``name``: (String) The name to use when storing the answer in the answers
274274
hash. If the name contains periods, it will define a path in the
275275
answers hash.
276-
- message: (String\|Function) The question to print. If defined as a
276+
- ``message``: (String\|Function) The question to print. If defined as a
277277
function, the first parameter will be the current inquirer session
278278
answers.
279-
- default: (String\|Number\|Array\|Function) Default value(s) to use if
279+
- ``default``: (String\|Number\|Array\|Function) Default value(s) to use if
280280
nothing is entered, or a function that returns the default value(s).
281281
If defined as a function, the first parameter will be the current
282282
inquirer session answers.
283-
- choices: (Array\|Function) Choices array or a function returning a
283+
- ``choices``: (Array\|Function) Choices array or a function returning a
284284
choices array. If defined as a function, the first parameter will be
285285
the current inquirer session answers. Array values can be simple
286286
strings, or objects containing a name (to display in list), a value
287287
(to save in the answers hash) and a short (to display after
288288
selection) properties. The choices array can also contain a
289289
Separator.
290-
- validate: (Function) Receive the user input and should return true if
290+
- ``validate``: (Function) Receive the user input and should return true if
291291
the value is valid, and an error message (String) otherwise. If false
292292
is returned, a default error message is provided.
293-
- filter: (Function) Receive the user input and return the filtered
293+
- ``filter``: (Function) Receive the user input and return the filtered
294294
value to be used inside the program. The value returned will be added
295295
to the Answers hash.
296-
- when: (Function, Boolean) Receive the current user answers hash and
296+
- ``when``: (Function, Boolean) Receive the current user answers hash and
297297
should return true or false depending on whether or not this question
298298
should be asked. The value can also be a simple boolean.
299-
- pageSize: (Number) Change the number of lines that will be rendered
299+
- ``pageSize``: (Number) Change the number of lines that will be rendered
300300
when using list, rawList, expand or checkbox.
301301

302302
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)