refactor(argparse): 将null替换为nullptr以提高C++兼容性
- 在argparse库中将所有null指针常量替换为nullptr - 更新头文件和源文件中的指针初始化和比较操作 - 修改测试文件中的相关断言检查 - 更新AST定义文件中的注释说明
This commit is contained in:
@@ -148,11 +148,11 @@ void scc_argparse_print_error(scc_argparse_context_t *ctx,
|
||||
|
||||
static inline void scc_argparse_spec_init(scc_argparse_spec_t *spec) {
|
||||
spec->value_type = SCC_ARGPARSE_VAL_TYPE_STRING;
|
||||
spec->raw_value = null;
|
||||
spec->store.ptr_store = null;
|
||||
spec->raw_value = nullptr;
|
||||
spec->store.ptr_store = nullptr;
|
||||
|
||||
spec->choices.count = 0;
|
||||
spec->choices.values = null;
|
||||
spec->choices.values = nullptr;
|
||||
|
||||
spec->flag_required = false;
|
||||
spec->flag_store_as_count = false;
|
||||
|
||||
@@ -5,7 +5,7 @@ void scc_argparse_init(scc_argparse_t *parser, const char *program_name,
|
||||
parser->prog_name = program_name;
|
||||
parser->version = "0.1.0";
|
||||
parser->description = description;
|
||||
parser->epilog = null;
|
||||
parser->epilog = nullptr;
|
||||
|
||||
parser->lang = SCC_ARGPARSE_LANG_EN;
|
||||
parser->need_help = true;
|
||||
@@ -26,7 +26,7 @@ void scc_argparse_drop(scc_argparse_t *parser) {
|
||||
static inline scc_argparse_cmd_t *is_subcommand(scc_argparse_cmd_t *cmd,
|
||||
const char *name) {
|
||||
if (!scc_vec_size(cmd->subcmds)) {
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
scc_vec_foreach(cmd->subcmds, i) {
|
||||
scc_argparse_cmd_t *subcmd = &scc_vec_at(cmd->subcmds, i);
|
||||
@@ -34,7 +34,7 @@ static inline scc_argparse_cmd_t *is_subcommand(scc_argparse_cmd_t *cmd,
|
||||
return subcmd;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static inline void parse_cmd(scc_optparse_t *optparse,
|
||||
@@ -86,7 +86,7 @@ static inline void parse_cmd(scc_optparse_t *optparse,
|
||||
scc_optparse_set(optparse, opts->data);
|
||||
}
|
||||
static void push_help(scc_argparse_cmd_t *cmd) {
|
||||
if (cmd == null) {
|
||||
if (cmd == nullptr) {
|
||||
return;
|
||||
}
|
||||
scc_vec_push(cmd->opts, ((scc_argparse_opt_t){
|
||||
@@ -144,7 +144,8 @@ static int validate_and_cleanup(scc_argparse_context_t *ctx,
|
||||
// 检查必需参数是否都已提供
|
||||
scc_vec_foreach(ctx->current_cmd->args, i) {
|
||||
scc_argparse_arg_t *arg = &scc_vec_at(ctx->current_cmd->args, i);
|
||||
if (arg->spec.flag_required && *arg->spec.store.str_store == NULL) {
|
||||
if (arg->spec.flag_required &&
|
||||
*arg->spec.store.str_store == nullptr) {
|
||||
errcode = SCC_ARGPARSE_ERR_MISSING_ARG;
|
||||
scc_argparse_print_error(ctx, errcode);
|
||||
break;
|
||||
@@ -190,7 +191,7 @@ static int handle_option(scc_argparse_context_t *ctx, scc_argparse_t *parser) {
|
||||
*opt->spec.store.str_store = ctx->result.value;
|
||||
}
|
||||
|
||||
// // opt value == null or value != null
|
||||
// // opt value == nullptr or value != nullptr
|
||||
// scc_argparse_opt_t *org_opt =
|
||||
// (scc_argparse_opt_t *)opt_res.opt->user_data;
|
||||
// if (parser->need_help) {
|
||||
@@ -218,7 +219,7 @@ static int handle_positional_arg(scc_argparse_context_t *ctx,
|
||||
(void)parser; // TODO
|
||||
scc_argparse_cmd_t *subcmd =
|
||||
is_subcommand(ctx->current_cmd, ctx->result.value);
|
||||
if (subcmd != NULL) {
|
||||
if (subcmd != nullptr) {
|
||||
ctx->current_cmd = subcmd;
|
||||
parse_cmd(&ctx->optparse, &ctx->opts, ctx->current_cmd);
|
||||
return SCC_ARGPARSE_ERR_NONE;
|
||||
@@ -239,7 +240,7 @@ static int handle_positional_arg(scc_argparse_context_t *ctx,
|
||||
// scc_argparse_cmd_t *cmd = is_subcommand(current_cmd,
|
||||
// opt_res.value);
|
||||
|
||||
// if (cmd != null) {
|
||||
// if (cmd != nullptr) {
|
||||
// current_cmd = cmd;
|
||||
// parse_cmd(&optparse, &opts, current_cmd);
|
||||
// } else {
|
||||
@@ -275,9 +276,9 @@ int scc_argparse_parse(scc_argparse_t *parser, int argc, const char **argv) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (ctx.result.opt != null) {
|
||||
if (ctx.result.opt != nullptr) {
|
||||
errcode = handle_option(&ctx, parser);
|
||||
} else if (ctx.result.value != null) {
|
||||
} else if (ctx.result.value != nullptr) {
|
||||
errcode = handle_positional_arg(&ctx, parser);
|
||||
} else {
|
||||
UNREACHABLE(); // 不应到达此处
|
||||
|
||||
@@ -174,8 +174,8 @@ void scc_argparse_print_help(scc_argparse_t *parser, scc_argparse_cmd_t *cmd) {
|
||||
|
||||
const char *scc_argparse_find_similar_arg(scc_argparse_cmd_t *cmd,
|
||||
const char *arg) {
|
||||
if (arg == null || cmd == null) {
|
||||
return null;
|
||||
if (arg == nullptr || cmd == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
if (arg[0] == '-' && arg[1] == '-' && arg[2] != '\0') {
|
||||
// opt arg
|
||||
|
||||
@@ -45,21 +45,21 @@ void test_simple_short_options(void) {
|
||||
// 解析第一个选项 -h
|
||||
TEST_CHECK(scc_optparse_parse(&parser, &res) != 0);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(res.opt && res.opt->short_name == 'h');
|
||||
TEST_CHECK(res.value == NULL);
|
||||
TEST_CHECK(res.value == nullptr);
|
||||
|
||||
// 解析第二个选项 -v
|
||||
TEST_CHECK(scc_optparse_parse(&parser, &res) != 0);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(res.opt && res.opt->short_name == 'v');
|
||||
TEST_CHECK(res.value == NULL);
|
||||
TEST_CHECK(res.value == nullptr);
|
||||
|
||||
// 没有更多选项
|
||||
TEST_CHECK(scc_optparse_parse(&parser, &res) == 0);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.value == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(res.value == nullptr);
|
||||
}
|
||||
|
||||
void test_simple_position_arg(void) {
|
||||
@@ -70,19 +70,19 @@ void test_simple_position_arg(void) {
|
||||
// 解析第一个选项
|
||||
TEST_CHECK(scc_optparse_parse(&parser, &res) != 0);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "a") == 0);
|
||||
|
||||
// 解析第二个选项
|
||||
TEST_CHECK(scc_optparse_parse(&parser, &res) != 0);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "b") == 0);
|
||||
|
||||
// 没有更多选项
|
||||
TEST_CHECK(scc_optparse_parse(&parser, &res) == 0);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.value == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(res.value == nullptr);
|
||||
}
|
||||
|
||||
// 测试3: 带参数的短选项
|
||||
@@ -95,14 +95,14 @@ void test_short_options_with_args(void) {
|
||||
// 解析 -f file.txt
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(res.opt && res.opt->short_name == 'f');
|
||||
TEST_CHECK(strcmp(res.value, "file.txt") == 0);
|
||||
|
||||
// 解析 -o output.txt
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(res.opt && res.opt->short_name == 'o');
|
||||
TEST_CHECK(strcmp(res.value, "output.txt") == 0);
|
||||
}
|
||||
@@ -119,20 +119,20 @@ void test_long_options(void) {
|
||||
// 解析 --help
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(res.opt && strcmp(res.opt->long_name, "help") == 0);
|
||||
|
||||
// 解析 --file=test.txt
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(res.opt && strcmp(res.opt->long_name, "file") == 0);
|
||||
TEST_CHECK(strcmp(res.value, "test.txt") == 0);
|
||||
|
||||
// 解析 --output out.txt
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(res.opt && strcmp(res.opt->long_name, "output") == 0);
|
||||
TEST_CHECK(strcmp(res.value, "out.txt") == 0);
|
||||
}
|
||||
@@ -160,13 +160,13 @@ void test_mixed_options_positional(void) {
|
||||
// 解析第一个位置参数
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "positional1") == 0);
|
||||
|
||||
// 解析第二个位置参数
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "positional2") == 0);
|
||||
}
|
||||
|
||||
@@ -250,13 +250,13 @@ void test_option_terminator(void) {
|
||||
// 解析 -f (作为位置参数)
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "-f") == 0);
|
||||
|
||||
// 解析 file.txt (作为位置参数)
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "file.txt") == 0);
|
||||
}
|
||||
|
||||
@@ -270,8 +270,8 @@ void test_edge_cases(void) {
|
||||
INIT_OPTPARSE(argv1);
|
||||
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.value == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(res.value == nullptr);
|
||||
|
||||
// 测试空字符串参数
|
||||
const char *argv2[] = {"program", "-f", "", "-o", " "};
|
||||
@@ -302,23 +302,23 @@ void test_multi_argument_option(void) {
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt && res.opt->short_name == 'l');
|
||||
TEST_CHECK(res.value == NULL);
|
||||
TEST_CHECK(res.value == nullptr);
|
||||
|
||||
// 由于 -l 可以接受多个参数,后续的参数应该作为 -l 的值
|
||||
// 但根据当前实现,可能需要多次调用
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "item1") == 0);
|
||||
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "item2") == 0);
|
||||
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "item3") == 0);
|
||||
}
|
||||
|
||||
@@ -344,7 +344,7 @@ void test_long_option_with_equal_no_value(void) {
|
||||
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NOT_ENOUGH_ARGS);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(res.opt && strcmp(res.opt->long_name, "file") == 0);
|
||||
}
|
||||
|
||||
@@ -382,14 +382,14 @@ void test_combined_short_with_arg(void) {
|
||||
// 解析 -v
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(res.opt && res.opt->short_name == 'v');
|
||||
TEST_CHECK(res.value == NULL);
|
||||
TEST_CHECK(res.value == nullptr);
|
||||
|
||||
// 解析 -finput.txt
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(res.opt && res.opt->short_name == 'f');
|
||||
TEST_CHECK(strcmp(res.value, "input.txt") == 0);
|
||||
}
|
||||
@@ -405,14 +405,14 @@ void test_too_many_arguments(void) {
|
||||
// 解析 -f file1
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(res.opt && res.opt->short_name == 'f');
|
||||
TEST_CHECK(strcmp(res.value, "file1") == 0);
|
||||
|
||||
// 尝试给 -f 第二个参数,应该返回错误
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "file2") == 0);
|
||||
}
|
||||
|
||||
@@ -438,7 +438,7 @@ void test_mixed_short_and_positional(void) {
|
||||
// 解析位置参数
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "positional") == 0);
|
||||
}
|
||||
|
||||
@@ -471,7 +471,7 @@ void test_complex_multi_argument(void) {
|
||||
// 第4个参数应该是位置参数
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "extra") == 0);
|
||||
}
|
||||
|
||||
@@ -487,7 +487,7 @@ void test_long_option_multi_args(void) {
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt && strcmp(res.opt->long_name, "list") == 0);
|
||||
TEST_CHECK(res.value == NULL);
|
||||
TEST_CHECK(res.value == nullptr);
|
||||
|
||||
// 解析 item1
|
||||
scc_optparse_parse(&parser, &res);
|
||||
@@ -518,7 +518,7 @@ void test_empty_long_name(void) {
|
||||
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(parser.handle_positional == 1);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "--") == 0);
|
||||
}
|
||||
|
||||
@@ -538,7 +538,7 @@ void test_non_dash_prefix(void) {
|
||||
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.error == SCC_OPT_ERROR_NONE);
|
||||
TEST_CHECK(res.opt != NULL);
|
||||
TEST_CHECK(res.opt != nullptr);
|
||||
TEST_CHECK(res.opt && res.opt->short_name == 'h');
|
||||
TEST_CHECK(res.opt && res.opt->prefix == '/');
|
||||
}
|
||||
@@ -550,7 +550,7 @@ void test_default_value(void) {
|
||||
// scc_optparse_result_t res;
|
||||
|
||||
// static scc_optparse_opt_t default_opts[] = {
|
||||
// SCC_OPTPARSE_OPT('-', 'o', "output", 0, 1, "default.txt", NULL),
|
||||
// SCC_OPTPARSE_OPT('-', 'o', "output", 0, 1, "default.txt", nullptr),
|
||||
// SCC_OPTPARSE_OPT_END(),
|
||||
// };
|
||||
|
||||
@@ -602,7 +602,7 @@ void test_callback_function(void) {
|
||||
|
||||
// // 调用回调函数
|
||||
// if (res.opt->invoke) {
|
||||
// res.opt->invoke(NULL);
|
||||
// res.opt->invoke(nullptr);
|
||||
// TEST_CHECK(callback_called == 1);
|
||||
// }
|
||||
}
|
||||
@@ -644,7 +644,7 @@ void test_single_dash(void) {
|
||||
|
||||
// 解析单个横杠(通常表示标准输入)
|
||||
scc_optparse_parse(&parser, &res);
|
||||
TEST_CHECK(res.opt == NULL);
|
||||
TEST_CHECK(res.opt == nullptr);
|
||||
TEST_CHECK(strcmp(res.value, "-") == 0);
|
||||
|
||||
// 解析 --
|
||||
@@ -724,5 +724,5 @@ TEST_LIST = {
|
||||
{"test_complex_short_combination", test_complex_short_combination},
|
||||
{"test_single_dash", test_single_dash},
|
||||
{"test_parser_state_reset", test_parser_state_reset},
|
||||
{NULL, NULL},
|
||||
{nullptr, nullptr},
|
||||
};
|
||||
Reference in New Issue
Block a user