Closed computerquip-streamlabs closed 5 years ago
I implemented this in my code. It's kinda gross but looks like this essentially:
enum arg_type {
ARG_LITERAL,
ARG_STRING,
ARG_INTEGER,
ARG_END
};
...
void *arg_table[] = {
help,
dump_args,
base_url,
app_dir,
temp_dir,
pids,
end
};
/* We need type information to dump parameters generically */
enum arg_type arg_table_types[] = {
ARG_LITERAL,
ARG_LITERAL,
ARG_STRING,
ARG_STRING,
ARG_STRING,
ARG_INTEGER,
ARG_END
};
if (dump_args->count > 0) {
for (int i = 0; i < arg_table_sz; ++i) {
const arg_type type = arg_table_types[i];
switch (type) {
case ARG_LITERAL:
print_literal_arg((struct arg_lit*)arg_table[i]);
break;
case ARG_STRING:
print_string_arg((struct arg_str*)arg_table[i]);
break;
case ARG_INTEGER:
print_integer_arg((struct arg_int*)arg_table[i]);
break;
case ARG_END:
print_end_arg((struct arg_end*)arg_table[i]);
break;
}
}
}
this let's me generically print each argument and their values.
You can try the information in const char* arg_hdr::datatype
.
Might be useful told hold an enum value in
arg_hdr
to have the ability to cast back fromvoid*
.