stage2 ai重构

This commit is contained in:
zzy committed 2026-10-26 12:51:32 +08:00
1 parent 4c0317fcb5
commit 2c531f23de
26 files changed
+1251 -1578

No files matched your search

+412 -91
View File
@@ -96,6 +96,9 @@ typedef struct {
usize capacity;
} scc_argparse_cmd_vec_t;
/**
* @brief 值类型:决定该参数怎么解析、写进 spec.store 的哪个成员。
*/
typedef enum scc_argparse_val_type {
SCC_ARGPARSE_VAL_TYPE_UNKNOWN,
SCC_ARGPARSE_VAL_TYPE_STRING,
@@ -107,39 +110,51 @@ typedef enum scc_argparse_val_type {
SCC_ARGPARSE_VAL_TYPE_COUNT,
} scc_argparse_val_type_t;
/**
* @brief 解析结果码。
* @note 帮助/版本请求也走返回值上报(PNT_DEFAULT),调用方应把它当成功处理。
*/
typedef enum scc_argparse_err {
SCC_ARGPARSE_ERR_NONE,
SCC_ARGPARSE_ERR_UNREACHABLE,
SCC_ARGPARSE_ERR_PNT_DEFAULT,
SCC_ARGPARSE_ERR_UNKNOWN_ERR,
SCC_ARGPARSE_ERR_INVALID_ARG,
SCC_ARGPARSE_ERR_INVALID_VALUE,
SCC_ARGPARSE_ERR_MISSING_ARG,
SCC_ARGPARSE_ERR_MISSING_VALUE,
SCC_ARGPARSE_ERR_UNKNOWN_ARG,
SCC_ARGPARSE_ERR_UNKNOWN_VALUE,
SCC_ARGPARSE_ERR_NONE, ///< 成功
SCC_ARGPARSE_ERR_UNREACHABLE, ///< 内部状态错误,不应出现
SCC_ARGPARSE_ERR_PNT_DEFAULT, ///< -h/--help/--version 已处理,调用方按成功退出
SCC_ARGPARSE_ERR_UNKNOWN_ERR, ///< 底层解析器返回了未映射的错误
SCC_ARGPARSE_ERR_INVALID_ARG, ///< 参数/选项本身非法
SCC_ARGPARSE_ERR_INVALID_VALUE, ///< 值不合法(枚举越界、整数/浮点解析失败等)
SCC_ARGPARSE_ERR_MISSING_ARG, ///< 缺少必需的位置参数或选项
SCC_ARGPARSE_ERR_MISSING_VALUE, ///< 选项缺少值
SCC_ARGPARSE_ERR_UNKNOWN_ARG, ///< 未知参数
SCC_ARGPARSE_ERR_UNKNOWN_VALUE, ///< 未知值
SCC_ARGPARSE_ERR_SUBCMD_TOO_DEEP, ///< 子命令嵌套超过 max_subcmd_depth
} scc_argparse_err_t;
/**
* @brief 字符串列表:LIST 类型的存储,也是 --filter 这类可重复选项的落点。
* @note items 由库 push,调用方负责 free。
*/
typedef struct {
const char **items;
usize count;
usize capacity;
} scc_argparse_list_t;
// 约束规范结构体
/**
* @brief 一条参数/选项的约束与落点:值类型、存储位置、枚举候选、各种标志。
* @note 用 scc_argparse_spec_init 复位,再用 spec_setup_* / spec_set_* 配置。
*/
struct scc_argparse_spec {
scc_argparse_val_type_t value_type; // 值类型
const char *raw_value;
const char *raw_value; // 最近一次收到的原始值(validate 用它判断"给没给")
// 存储位置
// 存储位置(按 value_type 选其一)
union {
cbool *bool_store; // 布尔值存储
int *int_store; // 整数存储
int *int_store; // 整数/计数/枚举下标存储
float *float_store; // 浮点数存储
const char **str_store; // 字符串存储
char **str_alloc_store; // 字符串存储(使用alloc,需要free)
void **ptr_store; // 通用指针存储
scc_argparse_list_t *vec_store; // 新增:指向字符串向量的指针
scc_argparse_list_t *vec_store; // 列表存储
} store;
// 枚举值约束
@@ -149,27 +164,37 @@ struct scc_argparse_spec {
} choices;
// 其他约束标志
cbool flag_required; // 是否必须
cbool flag_store_as_count; // 是否存储为计数(如 -vvv 返回3)
cbool flag_allow_empty; // 是否允许空字符串
cbool flag_hidden; // 是否隐藏(不显示在帮助)
cbool flag_takes_multiple; // 是否接受多个值(如 -I dir1 -I dir2)
cbool flag_parse_complex; // 是否进行复杂解析(如 1K, 2M, 3G)
cbool flag_required; // 必需:缺失报 MISSING_ARG
cbool flag_store_as_count; // 计数模式(-vvv → 3),由 setup_count 自动开启
cbool flag_allow_empty; // 允许 --opt= 这样的空值
cbool flag_hidden; // 不出现在帮助里
cbool flag_takes_multiple; // LIST 一次吃掉后续多个 token;false 表示一次一个值、靠重复出现
cbool flag_parse_complex; // 预留(1K/2M/3G 之类),当前未实现
};
/**
* @brief 位置参数:按添加顺序消费,适合"主语"型输入(文件名、目标名)。
*/
struct scc_argparse_arg {
scc_argparse_spec_t spec;
const char *name;
const char *description;
};
/**
* @brief 选项:短名/长名至少有一个,值怎么解析由 spec 决定。
*/
struct scc_argparse_opt {
scc_argparse_spec_t spec;
char short_name;
const char *long_name;
const char *description;
const char *group; // 帮助里的分组标题;NULL = 归到默认的 "Options" 段
};
/**
* @brief 一条命令或子命令:自身的参数/选项,加上它下面的子命令。
*/
struct scc_argparse_cmd {
const char *name;
const char *description;
@@ -179,6 +204,9 @@ struct scc_argparse_cmd {
scc_argparse_cmd_vec_t subcmds;
};
/**
* @brief 帮助/错误文本的语言。
*/
typedef enum {
SCC_ARGPARSE_LANG_EN,
SCC_ARGPARSE_LANG_ZH,
@@ -187,6 +215,10 @@ typedef enum {
#ifndef __SCC_FMT_H__
#define __SCC_FMT_H__
/**
* @brief 动态 char 数组,用作帮助/错误文本的输出缓冲。
* @note 不是以 '\0' 结尾的 C 字符串;输出时请用 items + count。
*/
typedef struct {
char *items;
usize count;
@@ -399,6 +431,10 @@ void scc_fmt(scc_str_t *out, const char *tpl, ...) {
}
#endif // __SCC_FMT_H__
/**
* @brief 解析器:配置 + 根命令 + 输出缓冲。
* @note 用 scc_argparse_init 初始化;帮助/错误文本落在 out 里,库不负责打印。
*/
typedef struct {
const char *prog_name;
const char *version;
@@ -407,14 +443,15 @@ typedef struct {
scc_str_t out; // 用于打印帮助和错误信息的缓冲区
scc_argparse_cmd_t root_cmd;
scc_argparse_lang_t lang;
char short_prefix; // 短选项前缀,默认 '-'
const char *long_prefix; // 长选项前缀,默认 "--"
const char *separator; // 分隔符,默认 "--";nullptr = 不识别
cbool need_help;
cbool need_version;
cbool need_debug;
cbool need_error_add_help;
cbool need_error_add_usage;
char short_prefix; // 短选项前缀,默认 '-'
const char *long_prefix; // 长选项前缀,默认 "--"
const char *separator; // 分隔符,默认 "--";nullptr = 不识别
int max_subcmd_depth; // 子命令最大嵌套层数(root 算 0),0 = 不限(默认)
cbool need_help; // 自动挂 -h/--help
cbool need_version; // 自动挂 --version
cbool need_debug; // 每次 parse 打印一条调试行
cbool need_error_add_help; // 报错时附完整帮助
cbool need_error_add_usage; // 报错时附用法行
} scc_argparse_t;
typedef struct {
@@ -423,44 +460,138 @@ typedef struct {
usize capacity;
} scc_optparse_opt_vec_t;
/**
* @brief 一次解析的上下文:当前命令、底层解析器状态、位置参数游标。
* @note 只在 scc_argparse_parse 内部使用,生命周期止于该函数返回。
*/
typedef struct {
scc_argparse_cmd_t *current_cmd; // 当前正在解析的命令
scc_optparse_t optparse; // 底层解析器
scc_optparse_opt_vec_t opts; // 当前命令的选项列表
scc_optparse_result_t result; // 底层解析结果
const void *err_ctx; // 错误上下文(期待的参数或者当前解析的参数)
int depth; // 当前子命令嵌套深度,root = 0
int positional_index; // 当前处理的位置参数索引
cbool parsing_done; // 是否已完成解析
} scc_argparse_context_t;
/**
* @brief 初始化解析器:清零输出缓冲、设置默认前缀/分隔符与语言/开关位。
* @param parser 待初始化的解析器,不必预先清零。
* @param program_name 程序名,用于用法行与 --version。
* @param description 一句话描述,可为 NULL。
*/
void scc_argparse_init(scc_argparse_t *parser, const char *program_name, const char *description);
/**
* @brief 取根命令,用来挂选项、位置参数和子命令。
* @param parser 已初始化的解析器。
* @return 根命令;生命周期与 parser 相同,不要单独释放。
*/
scc_argparse_cmd_t *scc_argparse_get_root(scc_argparse_t *parser);
/**
* @brief 释放解析器持有的所有 vector(含各子命令拷贝里的 args/opts/subcmds)与输出缓冲。
* @param parser 解析器。
* @note 不会释放调用方绑到 store 上的存储。
*/
void scc_argparse_drop(scc_argparse_t *parser);
/**
* @brief 解析 argv。
* @param parser 已配置好的解析器。
* @param argc 参数个数。
* @param argv 参数数组;argv[0] 视为程序名并跳过。
* @return SCC_ARGPARSE_ERR_NONE 成功;SCC_ARGPARSE_ERR_PNT_DEFAULT 表示 -h/--version 已处理
* (应视为成功退出);其余为错误码,具体见 scc_argparse_err_t。
* @note 帮助与错误文本只追加进 parser->out,库从不打印,调用方需自行回显。
*/
int scc_argparse_parse(scc_argparse_t *parser, int argc, const char **argv);
// 选项前缀与分隔符:nullptr = 关闭该项(例如 DOS 风格用 short='/', long="/", separator=nullptr)。
/**
* @brief 配置选项前缀与分隔符,需在 parse 之前调用。
* @param short_prefix 短选项前缀,0 表示不支持短选项。
* @param long_prefix 长选项前缀(长度任意),NULL 表示不支持长选项。
* @param separator 分隔符(整 token 精确匹配),NULL 表示不识别。
* @note DOS 风格可以传 ('/', "/", NULL);短名与长名同前缀时,/x 会先按长名查找。
*/
void scc_argparse_set_prefix(scc_argparse_t *parser, char short_prefix, const char *long_prefix,
const char *separator);
/**
* @brief 限制子命令的最大嵌套层数(root 算第 0 层),需在 parse 之前调用。
* @param parser 解析器。
* @param depth 最大层数:1 表示只允许 root 的直接子命令;0 表示不限(默认)。
* @note 超限时报 SCC_ARGPARSE_ERR_SUBCMD_TOO_DEEP,错误上下文是那个子命令名。
*/
void scc_argparse_set_max_subcmd_depth(scc_argparse_t *parser, int depth);
/**
* @brief 初始化一条命令/子命令。
* @param cmd 待初始化的命令。
* @param name 命令名;NULL 时取 "cmd_name"。
* @param description 描述,可为 NULL。
*/
void scc_argparse_cmd_init(scc_argparse_cmd_t *cmd, const char *name, const char *description);
/**
* @brief 释放命令持有的 vector,并递归到子命令。
* @param cmd 命令。
* @warning 已被 add_subcmd 浅拷贝走全部权的命令,不要再对它调用本函数。
*/
void scc_argparse_cmd_drop(scc_argparse_cmd_t *cmd);
/**
* @brief 绑定"该子命令被命中"的标记。
* @param cmd 子命令。
* @param store 命中时置 true 的存储;NULL 表示不记录。
* @note 库负责路由,调用方解析后只读这些标记就能分派。
*/
static inline void scc_argparse_cmd_set_triggered(scc_argparse_cmd_t *cmd, cbool *store) {
cmd->triggered = store;
}
/**
* @brief 追加一个位置参数,按添加顺序消费。
* @param cmd 命令。
* @param arg 参数描述;按值拷贝,arg 本身可复用。
* @note LIST 类型会吃掉其后所有位置参数;ENUM 类型同样支持 choices 校验。
*/
void scc_argparse_cmd_add_arg(scc_argparse_cmd_t *cmd, const scc_argparse_arg_t *arg);
/**
* @brief 追加一个选项。
* @param cmd 命令。
* @param opt 选项描述;按值拷贝。
*/
void scc_argparse_cmd_add_opt(scc_argparse_cmd_t *cmd, const scc_argparse_opt_t *opt);
/**
* @brief 追加一个子命令。
* @param cmd 父命令。
* @param subcmd 子命令。
* @warning 浅拷贝:subcmd->args / opts / subcmds 的底层 items 所有权转移给 cmd。
* 调用 add_subcmd 之后,不要再对 subcmd 调用 scc_argparse_cmd_drop,
* 也不要再修改 subcmd 及其 vector。parser 销毁时会递归释放。
*/
void scc_argparse_cmd_add_subcmd(scc_argparse_cmd_t *cmd, const scc_argparse_cmd_t *subcmd);
/**
* @brief 把命令的帮助文本追加进 parser->out。
* @param parser 解析器(提供输出缓冲与语言)。
* @param cmd 要打印的命令。
*/
void scc_argparse_print_help(scc_argparse_t *parser, scc_argparse_cmd_t *cmd);
/**
* @brief 把错误文本(含用法行或帮助提示)追加进 parser->out。
* @param ctx 解析上下文,提供出错位置信息。
* @param parser 解析器。
* @param err 错误码。
*/
void scc_argparse_print_error(scc_argparse_context_t *ctx, scc_argparse_t *parser,
scc_argparse_err_t err);
/**
* @brief 把 spec 复位成"字符串值 + 无标志位"的默认状态。
* @param spec 约束。
*/
static inline void scc_argparse_spec_init(scc_argparse_spec_t *spec) {
spec->value_type = SCC_ARGPARSE_VAL_TYPE_STRING;
spec->raw_value = nullptr;
@@ -477,14 +608,38 @@ static inline void scc_argparse_spec_init(scc_argparse_spec_t *spec) {
spec->flag_parse_complex = false;
}
/**
* @brief 初始化一个选项,spec 取默认值(字符串类型)。
* @param opt 选项。
* @param short_name 短名,0 表示没有短名。
* @param long_name 长名,NULL 表示没有长名。
* @param desc 帮助描述,可为 NULL。
*/
static inline void scc_argparse_opt_init(scc_argparse_opt_t *opt, char short_name,
const char *long_name, const char *desc) {
opt->short_name = short_name;
opt->long_name = long_name;
opt->description = desc;
opt->group = nullptr;
scc_argparse_spec_init(&opt->spec);
}
/**
* @brief 给选项设置帮助分组标题。
* @param opt 选项。
* @param group 分组名,NULL 表示回到默认的 "Options" 段。
* @note 分组只影响帮助排版;分组按首次声明顺序出现。
*/
static inline void scc_argparse_opt_set_group(scc_argparse_opt_t *opt, const char *group) {
opt->group = group;
}
/**
* @brief 初始化一个位置参数,spec 取默认值(字符串类型)。
* @param arg 位置参数。
* @param name 帮助里显示的名字。
* @param desc 帮助描述,可为 NULL。
*/
static inline void scc_argparse_arg_init(scc_argparse_arg_t *arg, const char *name,
const char *desc) {
arg->name = name;
@@ -492,32 +647,66 @@ static inline void scc_argparse_arg_init(scc_argparse_arg_t *arg, const char *na
scc_argparse_spec_init(&arg->spec);
}
/**
* @brief 设成布尔开关:出现即置 *store = true。
* @param spec 约束。
* @param store 存储;必须非 NULL。
*/
static inline void scc_argparse_spec_setup_bool(scc_argparse_spec_t *spec, cbool *store) {
spec->value_type = SCC_ARGPARSE_VAL_TYPE_BOOL;
spec->store.bool_store = store;
}
/**
* @brief 设成计数:每出现一次 *store += 1(-vvv 得到 3)。
* @param spec 约束。
* @param store 存储;必须非 NULL。
*/
static inline void scc_argparse_spec_setup_count(scc_argparse_spec_t *spec, int *store) {
spec->value_type = SCC_ARGPARSE_VAL_TYPE_COUNT;
spec->flag_store_as_count = true; // 自动设为计数模式
spec->store.int_store = store;
}
/**
* @brief 设成字符串:*store 直接指向 argv 里的原串。
* @param spec 约束。
* @param store 存储;必须非 NULL。
* @note 不做拷贝,生命周期跟随 argv。
*/
static inline void scc_argparse_spec_setup_string(scc_argparse_spec_t *spec, const char **store) {
spec->value_type = SCC_ARGPARSE_VAL_TYPE_STRING;
spec->store.str_store = store;
}
/**
* @brief 设成整数:解析失败报 SCC_ARGPARSE_ERR_INVALID_VALUE。
* @param spec 约束。
* @param store 存储;必须非 NULL。
*/
static inline void scc_argparse_spec_setup_int(scc_argparse_spec_t *spec, int *store) {
spec->value_type = SCC_ARGPARSE_VAL_TYPE_INT;
spec->store.int_store = store;
}
/**
* @brief 设成浮点:解析失败报 SCC_ARGPARSE_ERR_INVALID_VALUE。
* @param spec 约束。
* @param store 存储;必须非 NULL。
*/
static inline void scc_argparse_spec_setup_float(scc_argparse_spec_t *spec, float *store) {
spec->value_type = SCC_ARGPARSE_VAL_TYPE_FLOAT;
spec->store.float_store = store;
}
/**
* @brief 设成枚举:存命中项在 values 里的下标,非法值报 INVALID_VALUE。
* @param spec 约束。
* @param values 允许值数组,生命周期需覆盖 parse。
* @param count values 的个数。
* @param store 命中下标存储;未命中时被写成 -1。
* @note 选项和位置参数都支持枚举校验。
*/
static inline void scc_argparse_spec_setup_choices(scc_argparse_spec_t *spec, const char **values,
int count, int *store) {
spec->value_type = SCC_ARGPARSE_VAL_TYPE_ENUM;
@@ -526,7 +715,13 @@ static inline void scc_argparse_spec_setup_choices(scc_argparse_spec_t *spec, co
spec->store.int_store = store;
}
// 添加设置列表的辅助函数(内联)
/**
* @brief 设成列表:每次出现往 vec 里 push 一个原串。
* @param spec 约束。
* @param vec 目标列表;由库 push,调用方负责释放其 items。
* @note 默认一次一个值(可重复出现);要"一次吃掉后面所有 token"请再调
* scc_argparse_spec_set_takes_multiple(spec, true)。
*/
static inline void scc_argparse_spec_setup_list(scc_argparse_spec_t *spec,
scc_argparse_list_t *vec) {
spec->value_type = SCC_ARGPARSE_VAL_TYPE_LIST;
@@ -534,6 +729,14 @@ static inline void scc_argparse_spec_setup_list(scc_argparse_spec_t *spec,
spec->flag_takes_multiple = false;
}
/**
* @brief 生成 scc_argparse_spec_set_<attr>() 标志位设置函数。
* - required:缺失时报 SCC_ARGPARSE_ERR_MISSING_ARG
* - allow_empty:允许 --opt= 这样的空值
* - hidden:不出现在帮助里
* - takes_multiple:LIST 一次吃掉多个值
* - parse_complex:预留,当前未实现
*/
#define SCC_ARGPARSE_MACRO_SETTER(attr) \
static inline void scc_argparse_spec_set_##attr(scc_argparse_spec_t *spec, cbool flag) { \
spec->flag_##attr = flag; \
@@ -561,6 +764,7 @@ void scc_argparse_init(scc_argparse_t *parser, const char *program_name, const c
parser->short_prefix = '-';
parser->long_prefix = "--";
parser->separator = "--";
parser->max_subcmd_depth = 0;
parser->need_help = true;
parser->need_version = true;
parser->need_debug = false;
@@ -583,6 +787,10 @@ void scc_argparse_set_prefix(scc_argparse_t *parser, char short_prefix, const ch
parser->separator = separator;
}
void scc_argparse_set_max_subcmd_depth(scc_argparse_t *parser, int depth) {
parser->max_subcmd_depth = depth;
}
static inline scc_argparse_cmd_t *is_subcommand(scc_argparse_cmd_t *cmd, const char *name) {
if (!cmd->subcmds.count) {
return nullptr;
@@ -705,6 +913,7 @@ static void init_context(scc_argparse_context_t *ctx, scc_argparse_t *parser, in
scc_vec_init(ctx->opts);
prepare_cmd(&ctx->optparse, &ctx->opts, ctx->current_cmd);
ctx->positional_index = 0;
ctx->depth = 0;
ctx->parsing_done = false;
}
@@ -749,27 +958,91 @@ static int check_choices(const scc_argparse_spec_t *spec, const char *value, int
return SCC_ARGPARSE_ERR_INVALID_VALUE;
}
static int scc_argparse_parse_int(const char *s, usize *out) {
// 手写十进制解析:不接受空白/十六进制,溢出按 int 范围判。
// out 必须是 int*:store.int_store 指向的是 4 字节 int。
static int scc_argparse_parse_int(const char *s, int *out) {
if (!s || !*s)
return SCC_ARGPARSE_ERR_MISSING_VALUE;
char *end = NULL;
// TODO 手动解析整数,避免使用 strtoull
usize v = strtoull(s, &end, 10);
if (end == s || *end != '\0')
const char *p = s;
int negative = 0;
if (*p == '+' || *p == '-') {
negative = *p == '-';
p++;
}
if (*p == '\0')
return SCC_ARGPARSE_ERR_INVALID_VALUE;
*out = v;
// 上限从 unsigned 推出来,不依赖平台宏。
long long limit = (long long)(((unsigned)-1) >> 1) + (negative ? 1 : 0);
long long v = 0;
for (; *p; p++) {
if (*p < '0' || *p > '9')
return SCC_ARGPARSE_ERR_INVALID_VALUE;
v = v * 10 + (*p - '0');
if (v > limit)
return SCC_ARGPARSE_ERR_INVALID_VALUE;
}
*out = (int)(negative ? -v : v);
return SCC_ARGPARSE_ERR_NONE;
}
// 手写浮点解析:[+-]digits[.digits][eE[+-]digits],不支持 inf/nan/十六进制。
static int scc_argparse_parse_float(const char *s, float *out) {
if (!s || !*s)
return SCC_ARGPARSE_ERR_MISSING_VALUE;
char *end = NULL;
// TODO 手动解析浮点数,避免使用 strtof
float v = strtof(s, &end);
if (end == s || *end != '\0')
const char *p = s;
int negative = 0;
if (*p == '+' || *p == '-') {
negative = *p == '-';
p++;
}
double v = 0.0;
int digits = 0;
while (*p >= '0' && *p <= '9') {
v = v * 10.0 + (double)(*p - '0');
p++;
digits++;
}
if (*p == '.') {
p++;
double scale = 0.1;
while (*p >= '0' && *p <= '9') {
v += (double)(*p - '0') * scale;
scale *= 0.1;
p++;
digits++;
}
}
if (digits == 0)
return SCC_ARGPARSE_ERR_INVALID_VALUE;
*out = v;
if (*p == 'e' || *p == 'E') {
p++;
int exp_negative = 0;
if (*p == '+' || *p == '-') {
exp_negative = *p == '-';
p++;
}
if (*p < '0' || *p > '9')
return SCC_ARGPARSE_ERR_INVALID_VALUE;
int exp = 0;
while (*p >= '0' && *p <= '9') {
if (exp < 1000) // 再大只会走到 inf/0,不必继续累加
exp = exp * 10 + (*p - '0');
p++;
}
for (int i = 0; i < exp; i++)
v = exp_negative ? v / 10.0 : v * 10.0;
}
if (*p != '\0')
return SCC_ARGPARSE_ERR_INVALID_VALUE;
*out = (float)(negative ? -v : v);
return SCC_ARGPARSE_ERR_NONE;
}
@@ -795,7 +1068,7 @@ static int scc_argparse_store_value(scc_argparse_spec_t *spec, const char *value
case SCC_ARGPARSE_VAL_TYPE_INT:
if (!spec->store.int_store)
return SCC_ARGPARSE_ERR_INVALID_ARG;
return scc_argparse_parse_int(value, (void *)spec->store.int_store);
return scc_argparse_parse_int(value, spec->store.int_store);
case SCC_ARGPARSE_VAL_TYPE_FLOAT:
if (!spec->store.float_store)
@@ -916,10 +1189,14 @@ static int handle_option(scc_argparse_context_t *ctx, scc_argparse_t *parser) {
}
static int handle_positional_arg(scc_argparse_context_t *ctx, scc_argparse_t *parser) {
(void)parser;
scc_argparse_cmd_t *subcmd = is_subcommand(ctx->current_cmd, ctx->result.value);
if (subcmd != nullptr) {
int next_depth = ctx->depth + 1;
if (parser->max_subcmd_depth > 0 && next_depth > parser->max_subcmd_depth) {
ctx->err_ctx = subcmd->name;
return SCC_ARGPARSE_ERR_SUBCMD_TOO_DEEP;
}
ctx->depth = next_depth;
ctx->current_cmd = subcmd;
if (subcmd->triggered)
*subcmd->triggered = true;
@@ -1037,6 +1314,7 @@ enum {
ARGPARSE_INVALID_VALUE_FOR_OPTION,
ARGPARSE_OPTION_MISSING_VALUE,
ARGPARSE_DID_YOU_MEAN,
ARGPARSE_SUBCMD_TOO_DEEP,
};
static const char *fmt_en[] = {
@@ -1054,6 +1332,7 @@ static const char *fmt_en[] = {
[ARGPARSE_INVALID_VALUE_FOR_OPTION] = "Invalid value for option '{:s}'\n",
[ARGPARSE_OPTION_MISSING_VALUE] = "Option '{:s}' missing value\n",
[ARGPARSE_DID_YOU_MEAN] = "Did you mean: '{:s}'?\n",
[ARGPARSE_SUBCMD_TOO_DEEP] = "Subcommand nesting too deep: '{:s}'\n",
};
static const char *fmt_zh[] = {
@@ -1071,6 +1350,7 @@ static const char *fmt_zh[] = {
[ARGPARSE_INVALID_VALUE_FOR_OPTION] = "无效的选项值 '{:s}'\n",
[ARGPARSE_OPTION_MISSING_VALUE] = "选项 '{:s}' 缺少值\n",
[ARGPARSE_DID_YOU_MEAN] = "您是不是想要输入: '{:s}'?\n",
[ARGPARSE_SUBCMD_TOO_DEEP] = "子命令嵌套过深: '{:s}'\n",
};
/**
@@ -1139,6 +1419,63 @@ static void scc_print_usage(scc_argparse_t *parser, scc_argparse_cmd_t *cmd, scc
scc_fmt(out, "\n\n");
}
// 两个 group 是否同段:NULL 只和 NULL 同段。
static int scc_argparse_same_group(const char *a, const char *b) {
if (!a || !b)
return a == b;
return scc_strcmp(a, b) == 0;
}
// upto 之前是否已经出现过同一个 group(用来保证每组只打一次标题)。
static int scc_argparse_group_seen(const scc_argparse_cmd_t *cmd, usize upto, const char *group) {
for (usize i = 0; i < upto; i += 1) {
if (scc_argparse_same_group(cmd->opts.items[i].group, group))
return 1;
}
return 0;
}
// 选项一行:`-s, --long=<a|b|c>` 加描述,名字列宽 25。
static void scc_argparse_print_one_opt(scc_str_t *out, scc_argparse_opt_t *opt,
const char **lines) {
if (opt->short_name == 'h')
opt->description = lines[ARGPARSE_SHOW_HELP_MSG];
scc_str_t buf = {0};
if (opt->short_name) {
scc_vec_push(&buf, '-');
scc_vec_push(&buf, opt->short_name);
if (opt->long_name) {
scc_vec_push(&buf, ',');
scc_vec_push(&buf, ' ');
}
}
if (opt->long_name) {
scc_vec_push(&buf, '-');
scc_vec_push(&buf, '-');
for (const char *p = opt->long_name; *p; p++)
scc_vec_push(&buf, *p);
}
if (opt->spec.value_type == SCC_ARGPARSE_VAL_TYPE_ENUM) {
scc_vec_push(&buf, '=');
scc_vec_push(&buf, '<');
for (int j = 0; j < opt->spec.choices.count; j += 1) {
if (opt->spec.choices.values[j] == 0)
continue;
if (j != 0)
scc_vec_push(&buf, '|');
for (const char *p = opt->spec.choices.values[j]; *p; p++)
scc_vec_push(&buf, *p);
}
scc_vec_push(&buf, '>');
}
scc_vec_push(&buf, '\0');
scc_fmt(out, " {:<25s} {:s}", buf.items, opt->description ? opt->description : "");
scc_fmt(out, "\n");
scc_vec_free(buf);
}
void scc_argparse_print_help(scc_argparse_t *parser, scc_argparse_cmd_t *cmd) {
const char **lines = (parser->lang == SCC_ARGPARSE_LANG_ZH) ? fmt_zh : fmt_en;
scc_str_t *out = &parser->out;
@@ -1160,53 +1497,34 @@ void scc_argparse_print_help(scc_argparse_t *parser, scc_argparse_cmd_t *cmd) {
scc_fmt(out, "\n");
}
if (cmd->opts.count > 0) {
scc_fmt(out, lines[ARGPARSE_SHOW_OPT]);
for (usize i = 0; i < cmd->opts.count; i++) {
scc_argparse_opt_t *opt = &cmd->opts.items[i];
if (opt->spec.flag_hidden)
// 按 group 首次出现顺序分段;group==NULL 的进默认 "Options" 段。
for (usize i = 0; i < cmd->opts.count; i += 1) {
const char *group = cmd->opts.items[i].group;
if (scc_argparse_group_seen(cmd, i, group))
continue;
// 整段都被隐藏就不打标题
int visible = 0;
for (usize j = i; j < cmd->opts.count; j += 1) {
if (scc_argparse_same_group(cmd->opts.items[j].group, group) &&
!cmd->opts.items[j].spec.flag_hidden) {
visible = 1;
break;
}
}
if (!visible)
continue;
if (group)
scc_fmt(out, "{:s}:\n", group);
else
scc_fmt(out, lines[ARGPARSE_SHOW_OPT]);
for (usize j = i; j < cmd->opts.count; j += 1) {
scc_argparse_opt_t *opt = &cmd->opts.items[j];
if (!scc_argparse_same_group(opt->group, group) || opt->spec.flag_hidden)
continue;
if (opt->short_name == 'h')
opt->description = lines[ARGPARSE_SHOW_HELP_MSG];
scc_str_t buf = {0};
if (opt->short_name) {
scc_vec_push(&buf, '-');
scc_vec_push(&buf, opt->short_name);
if (opt->long_name) {
scc_vec_push(&buf, ',');
scc_vec_push(&buf, ' ');
}
}
if (opt->long_name) {
scc_vec_push(&buf, '-');
scc_vec_push(&buf, '-');
for (const char *p = opt->long_name; *p; p++) {
scc_vec_push(&buf, *p);
}
}
if (opt->spec.value_type == SCC_ARGPARSE_VAL_TYPE_ENUM) {
scc_vec_push(&buf, '=');
scc_vec_push(&buf, '<');
for (int j = 0; j < opt->spec.choices.count; j += 1) {
if (opt->spec.choices.values[j] == 0) {
continue;
}
if (j != 0) {
scc_vec_push(&buf, '|');
}
for (const char *p = opt->spec.choices.values[j]; *p; p++) {
scc_vec_push(&buf, *p);
}
}
scc_vec_push(&buf, '>');
}
scc_vec_push(&buf, '\0');
scc_fmt(out, " {:<25s} {:s}", buf.items, opt->description ? opt->description : "");
// if (opt->spec.default_value)
// scc_fmt(out, " (default: {:s})", opt->spec.default_value);
scc_fmt(out, "\n");
scc_argparse_print_one_opt(out, opt, lines);
}
scc_fmt(out, "\n");
}
@@ -1270,6 +1588,9 @@ void scc_argparse_print_error(scc_argparse_context_t *ctx, scc_argparse_t *parse
scc_fmt(out, lines[ARGPARSE_DID_YOU_MEAN], similar_arg);
}
break;
case SCC_ARGPARSE_ERR_SUBCMD_TOO_DEEP:
scc_fmt(out, lines[ARGPARSE_SUBCMD_TOO_DEEP], ctx->err_ctx);
break;
// FACK ERROR
case SCC_ARGPARSE_ERR_PNT_DEFAULT:
return;