util: add opt_value
and opt_string
methods
This commit is contained in:
parent
61e9d6bae4
commit
eec4e7bb39
@ -8,6 +8,26 @@
|
|||||||
|
|
||||||
#define OPT_LENGTH 30
|
#define OPT_LENGTH 30
|
||||||
|
|
||||||
|
const char *opt_value_to_string(const option_value_t *values, int value, const char *def)
|
||||||
|
{
|
||||||
|
for (int i = 0; values[i].name; i++) {
|
||||||
|
if (values[i].value == value) {
|
||||||
|
return values[i].name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return def;
|
||||||
|
}
|
||||||
|
|
||||||
|
int opt_string_to_value(const option_value_t *values, const char *name, int def)
|
||||||
|
{
|
||||||
|
for (int i = 0; values[i].name; i++) {
|
||||||
|
if (!strcmp(values[i].name, name)) {
|
||||||
|
return values[i].value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return def;
|
||||||
|
}
|
||||||
|
|
||||||
static void print_version(const char *cmd)
|
static void print_version(const char *cmd)
|
||||||
{
|
{
|
||||||
printf("%s (%s)\n", GIT_VERSION, GIT_REVISION);
|
printf("%s (%s)\n", GIT_VERSION, GIT_REVISION);
|
||||||
@ -75,12 +95,10 @@ static void print_help(option_t *options, const char *cmd)
|
|||||||
bool found = false;
|
bool found = false;
|
||||||
len += printf(" --%s=", option->name);
|
len += printf(" --%s=", option->name);
|
||||||
if (option->value_mapping) {
|
if (option->value_mapping) {
|
||||||
for (int j = 0; option->value_mapping[j].name; j++) {
|
const char *name = opt_value_to_string(option->value_mapping, *option->value_uint, NULL);
|
||||||
if (option->value_mapping[j].value == *option->value_uint) {
|
if (name) {
|
||||||
printf("%s", option->value_mapping[j].name);
|
len += printf("%s", name);
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,12 +159,11 @@ static int parse_opt(option_t *options, const char *key, int dash)
|
|||||||
} else if (option->value_string) {
|
} else if (option->value_string) {
|
||||||
ret = sscanf(value, option->format, option->value_string);
|
ret = sscanf(value, option->format, option->value_string);
|
||||||
} else if (option->value_mapping) {
|
} else if (option->value_mapping) {
|
||||||
for (int j = 0; option->value_mapping[j].name; j++) {
|
int ret = opt_string_to_value(option->value_mapping, value, -1);
|
||||||
if (!strcmp(option->value_mapping[j].name, value)) {
|
if (ret != -1) {
|
||||||
*option->value_uint = option->value_mapping[j].value;
|
*option->value_uint = ret;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ret = sscanf(value, option->format, option->value);
|
ret = sscanf(value, option->format, option->value);
|
||||||
} else {
|
} else {
|
||||||
|
@ -73,4 +73,7 @@ typedef struct options_s {
|
|||||||
.description = _desc, \
|
.description = _desc, \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *opt_value_to_string(const option_value_t *values, int value, const char *def);
|
||||||
|
int opt_string_to_value(const option_value_t *values, const char *name, int def);
|
||||||
|
|
||||||
int parse_opts(option_t *options, int argc, char *argv[]);
|
int parse_opts(option_t *options, int argc, char *argv[]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user