From 28085da34e628aaaa0508eaf71aa7c22dc5d2ace Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 5 Feb 2013 15:07:32 -0500 Subject: [PATCH] Add Question.multiline and associated handling Some questions can't be answered in a single line without reqiring more shell knowledge than we need (e.g. `&&`). --- quizzer/question.py | 7 ++++++- quizzer/ui/cli.py | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/quizzer/question.py b/quizzer/question.py index a9d00c0..64594cf 100644 --- a/quizzer/question.py +++ b/quizzer/question.py @@ -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, diff --git a/quizzer/ui/cli.py b/quizzer/ui/cli.py index 513fddc..d659040 100644 --- a/quizzer/ui/cli.py +++ b/quizzer/ui/cli.py @@ -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') -- 2.26.2