feat: Go转C
This commit is contained in:
57
c/include/cli_parse.h
Normal file
57
c/include/cli_parse.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#ifndef OMNI_CLI_PARSE_H
|
||||
#define OMNI_CLI_PARSE_H
|
||||
|
||||
#include "omni_common.h"
|
||||
|
||||
static int cli_parse_bool_text(const char *raw, int *out_value) {
|
||||
if (raw == NULL || out_value == NULL) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (strcmp(raw, "1") == 0 || strcmp(raw, "true") == 0 || strcmp(raw, "yes") == 0 || strcmp(raw, "on") == 0) {
|
||||
*out_value = 1;
|
||||
return 0;
|
||||
}
|
||||
if (strcmp(raw, "0") == 0 || strcmp(raw, "false") == 0 || strcmp(raw, "no") == 0 || strcmp(raw, "off") == 0) {
|
||||
*out_value = 0;
|
||||
return 0;
|
||||
}
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int cli_parse_value_flag(int argc, char **argv, int *index, const char *arg, const char *flag, const char **out_value) {
|
||||
size_t flag_len = strlen(flag);
|
||||
|
||||
if (strcmp(arg, flag) == 0) {
|
||||
if (*index + 1 >= argc) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
*out_value = argv[++(*index)];
|
||||
return 1;
|
||||
}
|
||||
if (strncmp(arg, flag, flag_len) == 0 && arg[flag_len] == '=') {
|
||||
*out_value = arg + flag_len + 1;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cli_parse_bool_flag(const char *arg, const char *flag, int *out_value) {
|
||||
size_t flag_len = strlen(flag);
|
||||
|
||||
if (strcmp(arg, flag) == 0) {
|
||||
*out_value = 1;
|
||||
return 1;
|
||||
}
|
||||
if (strncmp(arg, flag, flag_len) == 0 && arg[flag_len] == '=') {
|
||||
if (cli_parse_bool_text(arg + flag_len + 1, out_value) != 0) {
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user