Skip to content

Commit

Permalink
Add "other" alternative to choice automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
bebraw committed Nov 16, 2011
1 parent 13632bb commit 609379d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
1 change: 0 additions & 1 deletion demos/persistency/questions.yaml
Expand Up @@ -12,7 +12,6 @@ choices:
- BSD
- GPL
- MIT
- Other
---
id: use_fizzler
q: Do you want to use fizzler?
Expand Down
1 change: 0 additions & 1 deletion demos/question_types/questions.yaml
Expand Up @@ -12,7 +12,6 @@ choices:
- BSD
- GPL
- MIT
- Other
---
id: use_fizzler
q: Do you want to use fizzler?
Expand Down
2 changes: 1 addition & 1 deletion pyqa/pyqa.py
@@ -1,6 +1,6 @@
from __future__ import print_function
from functools import partial
from questions import boolean, choice, match
from questions import boolean, choice


def ask(questions, answers={}):
Expand Down
13 changes: 11 additions & 2 deletions pyqa/questions.py
@@ -1,5 +1,6 @@
from __future__ import print_function
from string import lower
from copy import copy


def boolean(answers):
Expand All @@ -19,11 +20,19 @@ def in_range(o, i, j):
except (TypeError, ValueError):
return False
lower_choices = map(lower, choices)
shown_choices = copy(choices)
shown_choices.append('Other')

answer = ''
while (not in_range(answer, 0, len(choices) - 1)) and (answer.lower() not in lower_choices):
map(lambda (i, c): print(str(i) + ': ' + c), enumerate(choices))
answer = answers.next() or ''
map(lambda (i, c): print(str(i) + ': ' + c), enumerate(shown_choices))
answer = answers.next().strip() or ''

if answer == str(len(choices)) or answer.lower() == 'other':
answer = ''
while not answer:
answer = answers.next().strip()
break

try:
return choices[int(answer)]
Expand Down
9 changes: 9 additions & 0 deletions pyqa/questions.spec
Expand Up @@ -32,3 +32,12 @@ accepts choice by name with upper
asks choice multiple times
questions.choice(['foo', 'bar', 'baz'], values('zob', '3', '2')) == 'baz'

accepts other choice
questions.choice(['foo', 'bar', 'baz'], values('3', 'booboo')) == 'booboo'

accepts other choice by name
questions.choice(['foo', 'bar', 'baz'], values('other', 'booboo')) == 'booboo'

accepts other choice by name with upper
questions.choice(['foo', 'bar', 'baz'], values('oTher', 'booboo')) == 'booboo'

0 comments on commit 609379d

Please sign in to comment.