Add options in format -opt=value
This commit is contained in:
45
opts/opts.c
45
opts/opts.c
@ -29,18 +29,28 @@ static void print_help(option_t *options)
|
||||
}
|
||||
}
|
||||
|
||||
static int parse_opt(option_t *options, const char *key, const char *value)
|
||||
static int parse_opt(option_t *options, const char *key)
|
||||
{
|
||||
option_t *option = NULL;
|
||||
const char *value = strchr(key, '=');
|
||||
|
||||
for (int i = 0; options[i].name; i++) {
|
||||
if (!strcmp(key, options[i].name)) {
|
||||
option = &options[i];
|
||||
break;
|
||||
if (value) {
|
||||
if (!strncmp(key, options[i].name, value - key)) {
|
||||
option = &options[i];
|
||||
value++; // ignore '='
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// require exact match
|
||||
if (!strcmp(key, options[i].name)) {
|
||||
option = &options[i];
|
||||
value = option->default_value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!option) {
|
||||
if (!option || !value) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
@ -68,22 +78,23 @@ int parse_opts(option_t *options, int argc, char *argv[])
|
||||
|
||||
for (arg = 1; arg < argc; arg += 2) {
|
||||
const char *key = argv[arg];
|
||||
if (!strcmp(key, "-help")) {
|
||||
|
||||
if (key[0] == '-') {
|
||||
key++;
|
||||
if (key[0] == '-')
|
||||
key++;
|
||||
} else {
|
||||
E_LOG_ERROR(NULL, "The '%s' is not option (should start with - or --).", key);
|
||||
}
|
||||
|
||||
if (!strcmp(key, "help")) {
|
||||
print_help(options);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (arg+1 == argc) {
|
||||
E_LOG_ERROR(NULL, "The %s is missing argument.", key);
|
||||
}
|
||||
|
||||
if (key[0] != '-') {
|
||||
E_LOG_ERROR(NULL, "The '%s' is not option (should start with -).", key);
|
||||
}
|
||||
|
||||
int ret = parse_opt(options, key+1, argv[arg+1]);
|
||||
int ret = parse_opt(options, key);
|
||||
if (ret <= 0) {
|
||||
E_LOG_ERROR(NULL, "Parsing '%s %s' returned '%d'.", key, argv[arg+1], ret);
|
||||
E_LOG_ERROR(NULL, "Parsing '%s' returned '%d'.", argv[arg], ret);
|
||||
}
|
||||
}
|
||||
|
||||
|
10
opts/opts.h
10
opts/opts.h
@ -21,6 +21,7 @@ typedef struct options_s {
|
||||
const char *help;
|
||||
option_value_t *value_mapping;
|
||||
unsigned size;
|
||||
const char *default_value;
|
||||
} option_t;
|
||||
|
||||
#define lambda(return_type, function_body) \
|
||||
@ -43,6 +44,15 @@ typedef struct options_s {
|
||||
.size = sizeof(_section##_options._name), \
|
||||
}
|
||||
|
||||
#define DEFINE_OPTION_DEFAULT(_section, _name, _type, _default_value) \
|
||||
{ \
|
||||
.name = #_section "-" #_name, \
|
||||
.value_##_type = &_section##_options._name, \
|
||||
.format = OPTION_FORMAT_##_type, \
|
||||
.size = sizeof(_section##_options._name), \
|
||||
.default_value = _default_value, \
|
||||
}
|
||||
|
||||
#define DEFINE_OPTION_VALUES(_section, _name, _value_mapping) \
|
||||
{ \
|
||||
.name = #_section "-" #_name, \
|
||||
|
Reference in New Issue
Block a user