Skip to content

Commit 631498a

Browse files
committed
Merge pull request smurfix#123 from archugs/prompt_choices
Prompt choices
2 parents a497aec + d7660f7 commit 631498a

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

flask_script/cli.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,8 @@ def prompt_choices(name, choices, default=None, resolve=ascii_lowercase,
8888

8989
while True:
9090
rv = prompt(name + ' - (%s)' % ', '.join(options), default)
91-
if not rv:
92-
return default
9391
rv = resolve(rv)
9492
if rv in no_choice:
9593
return None
96-
if rv in _choices:
94+
if rv in _choices or rv == default:
9795
return rv

tests.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from flask import Flask
88
from flask.ext.script._compat import StringIO, text_type
9-
from flask.ext.script import Command, Manager, Option, prompt, prompt_bool
9+
from flask.ext.script import Command, Manager, Option, prompt, prompt_bool, prompt_choices
1010

1111
from pytest import raises
1212

@@ -689,7 +689,52 @@ def correct_n(msg):
689689
code = run('manage.py hello', manager.run)
690690
out, err = capsys.readouterr()
691691
assert 'correct [y]: no' in out
692+
693+
def test_command_with_prompt_choices(self, capsys):
692694

695+
manager = Manager(self.app)
696+
697+
@manager.command
698+
def hello():
699+
print(prompt_choices(name='hello', choices=['peter', 'john', 'sam']))
700+
701+
@Catcher
702+
def hello_john(msg):
703+
if re.search("hello", msg):
704+
return 'john'
705+
706+
with hello_john:
707+
code = run('manage.py hello', manager.run)
708+
out, err = capsys.readouterr()
709+
assert 'hello - (peter, john, sam): john' in out
710+
711+
def test_command_with_default_prompt_choices(self, capsys):
712+
713+
manager = Manager(self.app)
714+
715+
@manager.command
716+
def hello():
717+
print(prompt_choices(name='hello', choices=['peter', 'charlie', 'sam'], default="john"))
718+
719+
@Catcher
720+
def hello_john(msg):
721+
if re.search("hello", msg):
722+
return '\n'
723+
724+
with hello_john:
725+
code = run('manage.py hello', manager.run)
726+
out, err = capsys.readouterr()
727+
assert 'hello - (peter, charlie, sam) [john]: john' in out
728+
729+
@Catcher
730+
def hello_charlie(msg):
731+
if re.search("hello", msg):
732+
return 'charlie'
733+
734+
with hello_charlie:
735+
code = run('manage.py hello', manager.run)
736+
out, err = capsys.readouterr()
737+
assert 'hello - (peter, charlie, sam) [john]: charlie' in out
693738

694739
class TestSubManager:
695740

0 commit comments

Comments
 (0)