cli: Add --all for easy review of previously-answered questions
[quizzer.git] / quizzer / cli.py
1 # Copyright (C) 2013 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of quizzer.
4 #
5 # quizzer is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # quizzer is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # quizzer.  If not, see <http://www.gnu.org/licenses/>.
16
17 import argparse as _argparse
18 import locale as _locale
19
20 from . import __doc__ as _module_doc
21 from . import __version__
22 from . import answerdb as _answerdb
23 from . import quiz as _quiz
24 from .ui import cli as _cli
25
26
27 def main():
28     encoding = _locale.getpreferredencoding(do_setlocale=True)
29
30     parser = _argparse.ArgumentParser(description=_module_doc)
31     parser.add_argument(
32         '--version', action='version',
33         version='%(prog)s {}'.format(__version__))
34     parser.add_argument(
35         '-a', '--answers', metavar='ANSWERS', default='answers.json',
36         help='path to an answers database')
37     parser.add_argument(
38         '--all', action='store_const', const=True, default=False,
39         help=('ask all questions '
40               '(not just never-correctly-answered leaf questions)'))
41     parser.add_argument(
42         'quiz', metavar='QUIZ',
43         help='path to a quiz file')
44
45     args = parser.parse_args()
46
47     quiz = _quiz.Quiz(path=args.quiz, encoding=encoding)
48     quiz.load()
49     answers = _answerdb.AnswerDatabase(path=args.answers, encoding=encoding)
50     try:
51         answers.load()
52     except IOError:
53         pass
54     stack = None
55     if args.all:
56         stack = [question for question in quiz]
57     ui = _cli.CommandLineInterface(quiz=quiz, answers=answers, stack=stack)
58     ui.run()
59     ui.answers.save()
60     ui.display_results()