Add Question.multiline and associated handling
authorW. Trevor King <wking@tremily.us>
Tue, 5 Feb 2013 20:07:32 +0000 (15:07 -0500)
committerW. Trevor King <wking@tremily.us>
Tue, 5 Feb 2013 20:07:32 +0000 (15:07 -0500)
Some questions can't be answered in a single line without reqiring
more shell knowledge than we need (e.g. `&&`).

quizzer/question.py
quizzer/ui/cli.py

index a9d00c06d54aa6491489b90a903eacedff37d1b4..64594cf14cfa2fbab6a5a6d3c90cd7a41a20ddc0 100644 (file)
@@ -19,6 +19,7 @@ class Question (object):
         'id',
         'prompt',
         'answer',
+        'multiline',
         'help',
         'dependencies',
         ]
@@ -40,6 +41,8 @@ class Question (object):
     def __setstate__(self, state):
         if 'id' not in state:
             state['id'] = state.get('prompt', None)
+        if 'multiline' not in state:
+            state['multiline'] = False
         if 'dependencies' not in state:
             state['dependencies'] = []
         for attr in self._state_attributes:
@@ -111,8 +114,10 @@ class ScriptQuestion (Question):
 
     def _invoke(self, answer):
         prefix = '{}-'.format(type(self).__name__)
+        if not self.multiline:
+            answer = [answer]
         with _tempfile.TemporaryDirectory(prefix=prefix) as tempdir:
-            script = '\n'.join(self.setup + [answer] + self.teardown)
+            script = '\n'.join(self.setup + answer + self.teardown)
             status,stdout,stderr = _util.invoke(
                 args=[self.interpreter],
                 stdin=script,
index 513fddc2dac67f09c061a3cfd36f92beef496ede..d6590409eddfd415a4fe687f71c2d21593865ef6 100644 (file)
@@ -13,6 +13,8 @@ class CommandLineInterface (UserInterface):
             if not question:
                 break
             print(question.prompt)
+            if question.multiline:
+                answers = []
             while True:
                 try:
                     answer = input('? ')
@@ -27,7 +29,14 @@ class CommandLineInterface (UserInterface):
                     print(question.prompt)
                     print(question.help)
                     continue
-                break
+                if question.multiline:
+                    answers.append(answer)
+                    if not a:
+                        break
+                else:
+                    break
+            if question.multiline:
+                answer = answers
             correct = self.process_answer(question=question, answer=answer)
             if correct:
                 print('correct\n')