318 lines
11 KiB
C
318 lines
11 KiB
C
#ifndef __SCC_OPTPARSER_H__
|
||
#define __SCC_OPTPARSER_H__
|
||
|
||
#ifndef nullptr
|
||
#define nullptr ((void *)0)
|
||
#endif
|
||
|
||
typedef struct scc_optparse_opt {
|
||
char prefix;
|
||
char short_name;
|
||
const char *long_name;
|
||
void (*handle)(void *user_data);
|
||
void *user_data;
|
||
int min_args;
|
||
int max_args;
|
||
} scc_optparse_opt_t;
|
||
|
||
#define SCC_OPTPARSE_OPT(prefix, short_name, long_name, min_args, max_args) \
|
||
{prefix, short_name, long_name, 0, 0, min_args, max_args}
|
||
#define SCC_OPTPARSE_OPT_END() {0}
|
||
|
||
typedef enum scc_optparse_error {
|
||
SCC_OPT_ERROR_NONE,
|
||
SCC_OPT_ERROR_SEPARATOR, // 命中了分隔符本身:不是失败,交给调用方决定吞或留
|
||
SCC_OPT_ERROR_NOT_FOUND_LONG_ARG,
|
||
SCC_OPT_ERROR_NOT_FOUND_SHORT_ARG,
|
||
SCC_OPT_ERROR_NOT_ENOUGH_ARGS,
|
||
SCC_OPT_ERROR_TOO_MANY_ARGS,
|
||
} scc_optparse_error_t;
|
||
|
||
typedef struct scc_optparse_state {
|
||
const scc_optparse_opt_t *opt;
|
||
int count; // check for min_args <= count <= max_args
|
||
int arg_pos; // for argv pos
|
||
int opt_pos; // for short pos
|
||
} scc_optparse_state_t;
|
||
|
||
typedef struct scc_optparse_result {
|
||
const scc_optparse_opt_t *opt;
|
||
const char *value;
|
||
int error;
|
||
const char *raw_arg;
|
||
} scc_optparse_result_t;
|
||
|
||
typedef struct scc_optparse {
|
||
int argc;
|
||
const char **argv;
|
||
const scc_optparse_opt_t *opts;
|
||
int handle_positional;
|
||
int greedy_mode;
|
||
char short_prefix; // 短选项前缀,默认 '-'(匹配仍用每项的 opt->prefix)
|
||
const char *long_prefix; // 长选项前缀,长度任意,默认 "--";nullptr = 不支持
|
||
const char *separator; // 分隔符,整 token 精确匹配,默认 "--";nullptr = 不识别
|
||
scc_optparse_state_t current;
|
||
} scc_optparse_t;
|
||
|
||
void scc_optparse_init(scc_optparse_t *parser, int argc, const char **argv);
|
||
void scc_optparse_drop(scc_optparse_t *parser);
|
||
void scc_optparse_swap_opts(scc_optparse_t *parser, const scc_optparse_opt_t *opts);
|
||
void scc_optparse_set(scc_optparse_t *parser, const scc_optparse_opt_t *opts);
|
||
void scc_optparse_reset(scc_optparse_t *parser);
|
||
int scc_optparse_parse(scc_optparse_t *parser, scc_optparse_result_t *res);
|
||
|
||
#ifdef SCC_OPTPARSER_IMPLEMENTATION
|
||
void scc_optparse_init(scc_optparse_t *parser, int argc, const char **argv) {
|
||
parser->argc = argc;
|
||
parser->argv = argv;
|
||
parser->opts = 0;
|
||
parser->short_prefix = '-';
|
||
parser->long_prefix = "--";
|
||
parser->separator = "--";
|
||
scc_optparse_reset(parser);
|
||
}
|
||
|
||
void scc_optparse_drop(scc_optparse_t *parser) { (void)parser; }
|
||
|
||
void scc_optparse_swap_opts(scc_optparse_t *parser, const scc_optparse_opt_t *opts) {
|
||
parser->opts = opts;
|
||
parser->current.opt = 0;
|
||
parser->current.count = 0;
|
||
parser->current.opt_pos = 0;
|
||
}
|
||
|
||
void scc_optparse_set(scc_optparse_t *parser, const scc_optparse_opt_t *opts) {
|
||
parser->opts = opts;
|
||
scc_optparse_reset(parser);
|
||
}
|
||
|
||
void scc_optparse_reset(scc_optparse_t *parser) {
|
||
parser->handle_positional = 0;
|
||
parser->current.opt = 0;
|
||
parser->current.count = 0;
|
||
parser->current.arg_pos = 1;
|
||
parser->current.opt_pos = 0;
|
||
}
|
||
|
||
static inline const char *str_chr(const char *s, char c) {
|
||
while (*s && *s != c)
|
||
s++;
|
||
return *s ? s : 0;
|
||
}
|
||
|
||
// optparse 一直不依赖 libc,这几个字符串比较自己写(长度用 int,和本文件其余部分一致)。
|
||
static inline int scc_optparse_str_len(const char *s) {
|
||
int n = 0;
|
||
while (s[n])
|
||
n++;
|
||
return n;
|
||
}
|
||
|
||
static inline int scc_optparse_str_eq(const char *a, const char *b) {
|
||
while (*a && *a == *b) {
|
||
a++;
|
||
b++;
|
||
}
|
||
return *a == *b;
|
||
}
|
||
|
||
static inline int scc_optparse_str_has_prefix(const char *s, const char *prefix, int len) {
|
||
for (int i = 0; i < len; i++) {
|
||
if (s[i] != prefix[i])
|
||
return 0;
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
const scc_optparse_opt_t *scc_optparse_get_long_name(const scc_optparse_opt_t *opts,
|
||
const char *name, const char end) {
|
||
if (*name == '\0')
|
||
return 0;
|
||
for (const scc_optparse_opt_t *opt = opts; opt->prefix; ++opt) {
|
||
if (!opt->long_name) {
|
||
continue;
|
||
}
|
||
int i;
|
||
for (i = 0; opt->long_name[i]; ++i) {
|
||
if (name[i] != opt->long_name[i]) {
|
||
break;
|
||
}
|
||
}
|
||
if (opt->long_name[i] == '\0' && (name[i] == '\0' || name[i] == end))
|
||
return opt;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
const scc_optparse_opt_t *scc_optparse_get_short_name(const scc_optparse_opt_t *opts,
|
||
const char short_name) {
|
||
for (const scc_optparse_opt_t *opt = opts; opt->prefix; ++opt) {
|
||
if (opt->short_name == short_name) {
|
||
return opt;
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
static inline void scc_optparse_parse_long_name(const scc_optparse_opt_t *opts,
|
||
scc_optparse_result_t *res, const char *arg,
|
||
int name_offset) {
|
||
// caller MUST trim the configured long prefix already
|
||
const scc_optparse_opt_t *ret = 0;
|
||
const char *chr = str_chr(arg, '=');
|
||
if (chr) {
|
||
// --file=a.txt
|
||
ret = scc_optparse_get_long_name(opts, arg + name_offset, '=');
|
||
if (ret == 0) {
|
||
res->error = SCC_OPT_ERROR_NOT_FOUND_LONG_ARG;
|
||
} else {
|
||
res->opt = ret;
|
||
if (chr[1] == '\0') {
|
||
// TODO maybe can empty like -file=
|
||
res->error = SCC_OPT_ERROR_NOT_ENOUGH_ARGS;
|
||
}
|
||
res->value = chr + 1;
|
||
}
|
||
} else {
|
||
// --file a.txt
|
||
ret = scc_optparse_get_long_name(opts, arg + name_offset, '\0');
|
||
if (ret == 0) {
|
||
res->error = SCC_OPT_ERROR_NOT_FOUND_LONG_ARG;
|
||
} else {
|
||
res->opt = ret;
|
||
}
|
||
}
|
||
}
|
||
|
||
static inline void scc_optparse_parse_short_name(const scc_optparse_opt_t *opts,
|
||
scc_optparse_result_t *res, const char *arg,
|
||
int arg_start) {
|
||
// const char *chr =
|
||
// str_chr(arg + arg_start, '='); // TODO maybe short can have -I=/usr
|
||
const scc_optparse_opt_t *ret = scc_optparse_get_short_name(opts, arg[arg_start]);
|
||
if (ret == 0) {
|
||
res->error = SCC_OPT_ERROR_NOT_FOUND_SHORT_ARG;
|
||
} else {
|
||
res->opt = ret;
|
||
}
|
||
}
|
||
|
||
static inline void scc_optparse_parse_position_arg(scc_optparse_t *parser, const char *arg,
|
||
scc_optparse_result_t *res) {
|
||
parser->current.arg_pos++;
|
||
res->value = arg;
|
||
}
|
||
|
||
static inline int scc_optparse_check(scc_optparse_t *parser, scc_optparse_result_t *res) {
|
||
if (!res->opt || res->error)
|
||
return 0;
|
||
if (res->value)
|
||
parser->current.count += 1;
|
||
if (parser->current.count < res->opt->max_args && res->opt->max_args > 0)
|
||
parser->current.opt = res->opt;
|
||
if (parser->current.count < res->opt->min_args && !res->value)
|
||
return 1;
|
||
return 0;
|
||
}
|
||
|
||
int scc_optparse_parse(scc_optparse_t *parser, scc_optparse_result_t *res) {
|
||
const scc_optparse_opt_t *opts = parser->opts;
|
||
const char *arg = 0;
|
||
res->opt = 0;
|
||
res->value = 0;
|
||
res->error = SCC_OPT_ERROR_NONE;
|
||
|
||
if (parser->current.opt_pos != 0) {
|
||
arg = parser->argv[parser->current.arg_pos];
|
||
goto CONTINUE_SHORT_OPTION;
|
||
}
|
||
|
||
while (parser->current.arg_pos < parser->argc) {
|
||
arg = parser->argv[parser->current.arg_pos];
|
||
|
||
if (arg[0] == '\0' || parser->handle_positional) {
|
||
scc_optparse_parse_position_arg(parser, arg, res);
|
||
goto RETURN;
|
||
}
|
||
|
||
// 分隔符:整 token 精确匹配,与前后缀解耦。以特殊 error 上报,不替调用方决定是吞是留。
|
||
if (parser->separator && parser->separator[0] &&
|
||
scc_optparse_str_eq(arg, parser->separator)) {
|
||
parser->handle_positional = 1; // “其后全是位置参数”属于分词
|
||
parser->current.arg_pos++;
|
||
res->error = SCC_OPT_ERROR_SEPARATOR;
|
||
res->value = arg;
|
||
goto RETURN;
|
||
}
|
||
|
||
// 长选项:按配置的前缀串匹配(长度任意);"分隔符"本身已在上一步处理掉。
|
||
int long_len = parser->long_prefix ? scc_optparse_str_len(parser->long_prefix) : 0;
|
||
if (long_len > 0 && scc_optparse_str_has_prefix(arg, parser->long_prefix, long_len) &&
|
||
arg[long_len] != '\0') {
|
||
parser->current.opt = 0;
|
||
parser->current.count = 0;
|
||
scc_optparse_parse_long_name(opts, res, arg, long_len);
|
||
parser->current.arg_pos += 1;
|
||
if (scc_optparse_check(parser, res)) {
|
||
goto NEXT;
|
||
}
|
||
goto RETURN;
|
||
}
|
||
|
||
for (const scc_optparse_opt_t *opt = opts; opt && opt->prefix; ++opt) {
|
||
if (arg[0] != opt->prefix) {
|
||
continue;
|
||
}
|
||
|
||
parser->current.opt = 0;
|
||
parser->current.count = 0;
|
||
// short option like -
|
||
if (arg[1] == '\0') {
|
||
// the `-`
|
||
res->value = arg;
|
||
parser->current.arg_pos++;
|
||
goto RETURN;
|
||
}
|
||
parser->current.opt_pos = 1;
|
||
CONTINUE_SHORT_OPTION:
|
||
scc_optparse_parse_short_name(opts, res, arg, parser->current.opt_pos++);
|
||
int have_next = arg[parser->current.opt_pos];
|
||
|
||
parser->current.opt_pos = have_next ? parser->current.opt_pos : 0;
|
||
parser->current.arg_pos += have_next ? 0 : 1;
|
||
if (scc_optparse_check(parser, res)) {
|
||
if (have_next) {
|
||
res->value = arg + parser->current.opt_pos;
|
||
parser->current.opt_pos = 0;
|
||
parser->current.arg_pos += 1;
|
||
goto RETURN;
|
||
}
|
||
goto NEXT;
|
||
}
|
||
goto RETURN;
|
||
}
|
||
// position arg
|
||
if (parser->current.opt && parser->current.count < parser->current.opt->max_args) {
|
||
res->opt = parser->current.opt;
|
||
res->value = arg;
|
||
parser->current.count++;
|
||
parser->current.arg_pos++;
|
||
goto RETURN;
|
||
} else {
|
||
scc_optparse_parse_position_arg(parser, arg, res);
|
||
goto RETURN;
|
||
}
|
||
NEXT:
|
||
continue;
|
||
}
|
||
RETURN:
|
||
if (res->opt && res->opt->max_args > 0 && parser->current.count > res->opt->max_args) {
|
||
res->error = SCC_OPT_ERROR_TOO_MANY_ARGS;
|
||
}
|
||
res->raw_arg = arg;
|
||
return res->opt != 0 || res->value != 0 || res->error != SCC_OPT_ERROR_NONE;
|
||
}
|
||
#endif // SCC_OPTPARSER_IMPLEMENTATION
|
||
|
||
#endif /* __SCC_OPTPARSER_H__ */
|