Commit 6063c043 authored by platypro's avatar platypro

Made gen command-line interface much cleaner

parent bb25539f
{
"name" : "Test set",
"questions": [
{
"id":"basic",
"numQuestions":5
},
{
"id":"net.platypro.ptable",
"numQuestions":5
......
......@@ -443,7 +443,7 @@ GENMODE exercise_toGenMode(char* name)
if(!strcmp(name, QTYPE_EXAM_UNORDERED_))
return QTYPE_EXAM_UNORDERED;
return QTYPE_NORMAL;
return QTYPE_RANDOM;
}
EXERCISEVARIABLE* exercise_getVariable(QUESTION* q, char* id)
......@@ -493,7 +493,7 @@ QUESTION* question_create(PG_STATE* state, QUIZ* q)
// Pick the next exercise
switch(q->qtype)
{
case QTYPE_NORMAL:
case QTYPE_RANDOM:
{
choice = (q->set->numExercises > 1) ? pcg32_random() % q->set->numExercises : 0;
EXERCISEREF* mark;
......
......@@ -134,7 +134,7 @@ typedef struct PG_Exercise
typedef enum GenMode
{
QTYPE_NORMAL,
QTYPE_RANDOM,
QTYPE_EXAM_ORDERED,
QTYPE_EXAM_UNORDERED,
} GENMODE;
......
......@@ -174,7 +174,7 @@ bool session_common_process(PG_STATE* state, WJElement request, WJWriter respons
quiz_destroy(u->quiz);
u->quiz = quiz_create(state, _pset, genmode, 5);
WJWUInt32(KEYCOUNT(KEY_QUESTION), genmode == QTYPE_NORMAL ? 5 : u->quiz->ordered_rand.top, response);
WJWUInt32(KEYCOUNT(KEY_QUESTION), genmode == QTYPE_RANDOM ? 5 : u->quiz->ordered_rand.top, response);
if(giveFirst && user_nextQuestion(state, u, response))
WJWString(KEY_ACTION, ACTION_QUIZ_NEW, true, response);
}
......
......@@ -9,10 +9,8 @@
#include "keys.h"
#include "args.h"
#define OPT_OUTPUT_FILE 'f'
#define OPT_OUTPUT_SFILE 's'
#define OPT_OUTPUT_TYPE 't'
#define OPT_INPUT_PSET 'p'
#define OPT_INPUT_GENMODE 'g'
#define OPT_INPUT_NUM 'n'
#if _QUIZGRIND_BUILD_PDF
......@@ -26,13 +24,15 @@ struct option_info gen_opts[] =
{
{0, OPT_HELP, "help", NULL, "Show help and exit"},
// File options
{ARGS_ARGUMENT, OPT_OUTPUT_FILE, NULL, "FILE", "Output file"},
{ARGS_ARGUMENT, OPT_OUTPUT_SFILE, NULL, "FILE",
"Solution output. If not set, added to regular output file."},
{ARGS_ARGUMENT, OPT_OUTPUT_TYPE, NULL, "TYPE", "Output type"},
{ARGS_ARGUMENT, OPT_OUTPUT_TYPE, NULL, "TYPE", "Output type (auto, json"
#ifdef _QUIZGRIND_BUILD_PDF
", pdf"
#endif
")"},
{ARGS_ARGUMENT, OPT_INPUT_GENMODE, NULL, "MODE",
"Question generation type (random, ordered, unordered)"},
{ARGS_ARGUMENT, OPT_INPUT_PSET, NULL, "FILE", "Problem set to generate"},
{ARGS_ARGUMENT, OPT_INPUT_NUM, NULL, "NUM", "Number of questions"},
#ifdef _QUIZGRIND_BUILD_PDF
// PDF options
......@@ -50,7 +50,7 @@ bool gen_json(PG_STATE* state, PROBLEMSET* pset)
if(!f) return false;
WJWriter writer = WJWOpenFILEDocument(true, f);
WJWriter solwriter = writer;
QUIZ* quiz = quiz_create(state, pset, QTYPE_NORMAL, state->gen.num);
QUIZ* quiz = quiz_create(state, pset, QTYPE_RANDOM, state->gen.num);
QUESTION* q = NULL;
if(state->gen.sfile)
......@@ -87,25 +87,21 @@ bool gen_parse_arg(int id, char* arg, void* udata)
switch(id)
{
case OPT_HELP:
printf("QuizGrind Generator\nUsage: quizgrind gen ");
printf("QuizGrind Generator\nUsage: quizgrind gen <pset> <file>");
args_show_short(gen_opts);
puts("\n\nOptions:");
args_show_long(gen_opts, 2);
exit(0);
break;
case OPT_OUTPUT_FILE:
state->gen.file = arg;
break;
case OPT_OUTPUT_SFILE:
state->gen.sfile = arg;
break;
case OPT_INPUT_GENMODE:
state->gen.mode = exercise_toGenMode(arg);
break;
case OPT_INPUT_PSET:
state->gen.pset = arg;
break;
case OPT_OUTPUT_TYPE:
if(!strcmp(arg, "auto"))
state->gen.type = GTYPE_AUTO;
if(!strcmp(arg, "json"))
state->gen.type = GTYPE_JSON;
#ifdef _QUIZGRIND_BUILD_PDF
......@@ -135,11 +131,19 @@ bool gen_parse_arg(int id, char* arg, void* udata)
return true;
}
bool endswith(char* src, char* end)
{
uint32_t srclen = strlen(src);
uint32_t endlen = strlen(end);
return (srclen > endlen && !strcmp(src + srclen - endlen, end));
}
int handle_file(void* data, int argc, char** argv)
{
PG_STATE* state = data;
state->gen.type = GTYPE_JSON;
state->gen.type = GTYPE_AUTO;
state->gen.num = 10;
state->gen.mode = QTYPE_EXAM_ORDERED;
#ifdef _QUIZGRIND_BUILD_PDF
state->gen.pdf.headers = DEFAULT_PDF_HEADERS;
......@@ -148,19 +152,39 @@ int handle_file(void* data, int argc, char** argv)
state->gen.pdf.pageWidth = DEFAULT_PDF_PAGE_WIDTH;
#endif /* _QUIZGRIND_BUILD_PDF */
args_parse(gen_opts,
argv = args_parse(gen_opts,
gen_parse_arg,
state,
&argc, argv);
if(!state->gen.pset)
printf("Missing problem set!!\n");
if(!state->gen.file)
printf("Missing file!\n");
if(argc)
{
state->gen.pset = *argv;
} else printf("Missing problem set!!\n");
argv = args_parse(gen_opts,
gen_parse_arg,
state,
&argc, argv);
if(argc)
{
state->gen.file = *argv;
} else printf("Missing file!\n");
if(!state->gen.file || !state->gen.pset) return 1;
if(state->gen.type == GTYPE_AUTO)
{
state->gen.type = GTYPE_JSON;
#ifdef _QUIZGRIND_BUILD_PDF
if(endswith(state->gen.file, ".pdf"))
{
state->gen.type = GTYPE_PDF;
}
#endif
}
char* setPath = pset_loadOne(state, state->gen.pset);
if(!setPath) return false;
......@@ -173,6 +197,7 @@ int handle_file(void* data, int argc, char** argv)
#ifdef _QUIZGRIND_BUILD_PDF
case GTYPE_PDF: return gen_pdf(state, pset);
#endif
default: return false;
}
exercise_cleanup(state);
......
......@@ -8,6 +8,7 @@ struct PG_State;
typedef enum GenType
{
GTYPE_AUTO,
GTYPE_JSON,
#ifdef _QUIZGRIND_BUILD_PDF
GTYPE_PDF
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment