Support parsing enums (without members)
This commit is contained in:
parent
3513b94fc7
commit
453c3610f9
38
compile.c
38
compile.c
@ -3089,6 +3089,28 @@ CORD compile_cli_arg_call(env_t *env, CORD fn_name, type_t *fn_type)
|
||||
"}\n");
|
||||
break;
|
||||
}
|
||||
case EnumType: {
|
||||
env_t *enum_env = Match(t, EnumType)->env;
|
||||
code = CORD_all(code, "else if (pop_flag(argv, &i, \"", flag, "\", &flag)) {\n",
|
||||
"if (flag.length == 0)\n"
|
||||
"USAGE_ERR(\"No value provided for '--", flag, "'\");\n");
|
||||
CORD all_tags = CORD_EMPTY;
|
||||
for (tag_t *tag = Match(t, EnumType)->tags; tag; tag = tag->next) {
|
||||
if (tag->type && Match(tag->type, StructType)->fields)
|
||||
compiler_err(NULL, NULL, NULL,
|
||||
"The type %T has enum fields with member values, which is not yet supported for command line arguments.");
|
||||
all_tags = CORD_all(all_tags, " ", tag->name);
|
||||
binding_t *b = get_binding(enum_env, tag->name);
|
||||
code = CORD_all(code,
|
||||
"if (Text$equal_ignoring_case(flag, Text(\"", tag->name, "\"))) {\n"
|
||||
"$", arg->name, " = ", b->code, ";\n",
|
||||
arg->name, "$is_set = yes;\n"
|
||||
"} else ");
|
||||
}
|
||||
code = CORD_all(code, "USAGE_ERR(\"Invalid value provided for '--", flag, "'; valid values are:", all_tags, "\");\n",
|
||||
"}\n");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
compiler_err(NULL, NULL, NULL, "Main function has unsupported argument type: %T", t);
|
||||
}
|
||||
@ -3125,6 +3147,22 @@ CORD compile_cli_arg_call(env_t *env, CORD fn_name, type_t *fn_type)
|
||||
"if (i < argc) {");
|
||||
if (t->tag == TextType) {
|
||||
code = CORD_all(code, "$", arg->name, " = Text$from_str(argv[i]);\n");
|
||||
} else if (t->tag == EnumType) {
|
||||
env_t *enum_env = Match(t, EnumType)->env;
|
||||
CORD all_tags = CORD_EMPTY;
|
||||
for (tag_t *tag = Match(t, EnumType)->tags; tag; tag = tag->next) {
|
||||
if (tag->type && Match(tag->type, StructType)->fields)
|
||||
compiler_err(NULL, NULL, NULL,
|
||||
"The type %T has enum fields with member values, which is not yet supported for command line arguments.");
|
||||
all_tags = CORD_all(all_tags, " ", tag->name);
|
||||
binding_t *b = get_binding(enum_env, tag->name);
|
||||
code = CORD_all(code,
|
||||
"if (strcasecmp(argv[i], \"", tag->name, "\") == 0) {\n"
|
||||
"$", arg->name, " = ", b->code, ";\n",
|
||||
arg->name, "$is_set = yes;\n"
|
||||
"} else ");
|
||||
}
|
||||
code = CORD_all(code, "USAGE_ERR(\"Invalid value provided for '--", arg->name, "'; valid values are:", all_tags, "\");\n");
|
||||
} else {
|
||||
code = CORD_all(
|
||||
code,
|
||||
|
Loading…
Reference in New Issue
Block a user