Better support configurations
This commit is contained in:
32
opts/opts.c
32
opts/opts.c
@ -7,13 +7,24 @@
|
||||
static void print_help(option_t *options)
|
||||
{
|
||||
for (int i = 0; options[i].name; i++) {
|
||||
printf("%20s ", options[i].name);
|
||||
option_t *option = &options[i];
|
||||
printf("%40s\t", option->name);
|
||||
|
||||
if (options[i].ptr) {
|
||||
printf(options[i].format, options[i].ptr);
|
||||
if (option->value_string) {
|
||||
printf(option->format, option->value_string);
|
||||
} else if (option->value_mapping) {
|
||||
for (int j = 0; option->value_mapping[j].name; j++) {
|
||||
if (option->value_mapping[j].value == *option->value) {
|
||||
printf("%s - ", option->value_mapping[j].name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf(option->format, *option->value);
|
||||
} else {
|
||||
printf(options[i].format, *options[i].value);
|
||||
printf(option->format, *option->value);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@ -34,8 +45,17 @@ static int parse_opt(option_t *options, const char *key, const char *value)
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
if (option->ptr) {
|
||||
ret = sscanf(value, option->format, option->ptr);
|
||||
if (option->value_string) {
|
||||
ret = sscanf(value, option->format, option->value_string);
|
||||
} else if (option->value_mapping) {
|
||||
for (int j = 0; option->value_mapping[j].name; j++) {
|
||||
if (!strcmp(option->value_mapping[j].name, value)) {
|
||||
*option->value_uint = option->value_mapping[j].value;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
ret = sscanf(value, option->format, option->value);
|
||||
} else {
|
||||
ret = sscanf(value, option->format, option->value);
|
||||
}
|
||||
|
32
opts/opts.h
32
opts/opts.h
@ -2,16 +2,24 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct option_value_s {
|
||||
const char *name;
|
||||
unsigned value;
|
||||
} option_value_t;
|
||||
|
||||
typedef struct options_s {
|
||||
const char *name;
|
||||
void *ptr;
|
||||
char *value_string;
|
||||
union {
|
||||
unsigned *value;
|
||||
unsigned *value_uint;
|
||||
unsigned *value_hex;
|
||||
bool *value_bool;
|
||||
float *value_float;
|
||||
};
|
||||
const char *format;
|
||||
const char *help;
|
||||
option_value_t *value_mapping;
|
||||
unsigned size;
|
||||
} option_t;
|
||||
|
||||
@ -21,8 +29,11 @@ typedef struct options_s {
|
||||
__fn__; \
|
||||
})
|
||||
|
||||
#define OPTION_FORMAT_uint "%d"
|
||||
#define OPTION_FORMAT_bool "%d"
|
||||
#define OPTION_FORMAT_uint "%d"
|
||||
#define OPTION_FORMAT_hex "%08x"
|
||||
#define OPTION_FORMAT_bool "%d"
|
||||
#define OPTION_FORMAT_float "%.1f"
|
||||
#define OPTION_FORMAT_string "%s"
|
||||
|
||||
#define DEFINE_OPTION(_section, _name, _type) \
|
||||
{ \
|
||||
@ -32,11 +43,20 @@ typedef struct options_s {
|
||||
.size = sizeof(_section##_options._name), \
|
||||
}
|
||||
|
||||
#define DEFINE_OPTION_PTR(_section, _name, _format) \
|
||||
#define DEFINE_OPTION_VALUES(_section, _name, _value_mapping) \
|
||||
{ \
|
||||
.name = #_section "-" #_name, \
|
||||
.ptr = _section##_options._name, \
|
||||
.format = _format, \
|
||||
.value_hex = &_section##_options._name, \
|
||||
.format = OPTION_FORMAT_hex, \
|
||||
.value_mapping = _value_mapping, \
|
||||
.size = sizeof(_section##_options._name), \
|
||||
}
|
||||
|
||||
#define DEFINE_OPTION_PTR(_section, _name, _type) \
|
||||
{ \
|
||||
.name = #_section "-" #_name, \
|
||||
.value_##_type = _section##_options._name, \
|
||||
.format = OPTION_FORMAT_##_type, \
|
||||
.size = sizeof(_section##_options._name), \
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user