From: Lidong Yan <502024330056@xxxxxxxxxxxxxxxx> Signed-off-by: Lidong Yan <502024330056@xxxxxxxxxxxxxxxx> --- parse-options.c | 12 ++++++-- parse-options.h | 12 ++++---- t/helper/test-free-unknown-options.c | 43 ++++++++++++++-------------- t/helper/test-tool.c | 2 +- 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/parse-options.c b/parse-options.c index 4279dfe4d313..12721b83000a 100644 --- a/parse-options.c +++ b/parse-options.c @@ -638,7 +638,9 @@ static int has_subcommands(const struct option *options) return 0; } -static void set_strdup_fn(struct parse_opt_ctx_t *ctx, const struct option *options) { +static void set_strdup_fn(struct parse_opt_ctx_t *ctx, + const struct option *options) +{ for (; options->type != OPTION_END; options++) ; if (options->value && options->strdup_fn) { @@ -993,9 +995,13 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx, * This is leaky, too bad. */ if (ctx->unknown_opts && ctx->strdup_fn) { - ctx->argv[0] = ctx->strdup_fn(ctx->unknown_opts, ctx->opt - 1); + ctx->argv[0] = + ctx->strdup_fn(ctx->unknown_opts, + ctx->opt - + 1); } else { - ctx->argv[0] = xstrdup(ctx->opt - 1); + ctx->argv[0] = + xstrdup(ctx->opt - 1); } *(char *)ctx->argv[0] = '-'; goto unknown; diff --git a/parse-options.h b/parse-options.h index af06a09abb8e..57037bd381a3 100644 --- a/parse-options.h +++ b/parse-options.h @@ -391,11 +391,12 @@ static char *parse_options_noop_ignored_value MAYBE_UNUSED; } #define OPT_SUBCOMMAND(l, v, fn) OPT_SUBCOMMAND_F((l), (v), (fn), 0) -#define OPT_UNKNOWN(v, fn) { \ - .type = OPTION_END, \ - .value = (v), \ - .strdup_fn = (fn), \ -} +#define OPT_UNKNOWN(v, fn) \ + { \ + .type = OPTION_END, \ + .value = (v), \ + .strdup_fn = (fn), \ + } /* * parse_options() will filter out the processed options and leave the @@ -505,7 +506,6 @@ struct parse_opt_ctx_t { const char *prefix; const char **alias_groups; /* must be in groups of 3 elements! */ struct parse_opt_cmdmode_list *cmdmode_list; - void *unknown_opts; parse_opt_strdup_fn *strdup_fn; }; diff --git a/t/helper/test-free-unknown-options.c b/t/helper/test-free-unknown-options.c index 9d658115ba8f..59d732da23ca 100644 --- a/t/helper/test-free-unknown-options.c +++ b/t/helper/test-free-unknown-options.c @@ -2,34 +2,35 @@ #include "parse-options.h" #include "setup.h" #include "strvec.h" +#include "test-tool.h" static const char *const free_unknown_options_usage[] = { - "test-tool free-unknown-options", - NULL + "test-tool free-unknown-options", NULL }; -int cmd__free_unknown_options(int argc, const char **argv) { - struct strvec *unknown_opts = xmalloc(sizeof(struct strvec)); - strvec_init(unknown_opts); - const char *prefix = setup_git_directory(); - - bool a, b; +int cmd__free_unknown_options(int argc, const char **argv) +{ + struct strvec *unknown_opts = xmalloc(sizeof(struct strvec)); + const char *prefix = setup_git_directory(); + int a = 0, b = 0; + size_t i; struct option options[] = { OPT_BOOL('a', "test-a", &a, N_("option a, only for test use")), - OPT_BOOL('b', "test-b", &b, N_("option b, only for test use")), - OPT_UNKNOWN(unknown_opts, (parse_opt_strdup_fn *)&strvec_push), + OPT_BOOL('b', "test-b", &b, N_("option b, only for test use")), + OPT_UNKNOWN(unknown_opts, (parse_opt_strdup_fn *)&strvec_push), }; - parse_options(argc, argv, prefix, options, - free_unknown_options_usage, PARSE_OPT_KEEP_UNKNOWN_OPT); + strvec_init(unknown_opts); + parse_options(argc, argv, prefix, options, free_unknown_options_usage, + PARSE_OPT_KEEP_UNKNOWN_OPT); + + printf("a = %s\n", a ? "true" : "false"); + printf("b = %s\n", b ? "true" : "false"); - printf("a = %s\n", a? "true": "false"); - printf("b = %s\n", b? "true": "false"); + for (i = 0; i < unknown_opts->nr; i++) + printf("free unknown option: %s\n", unknown_opts->v[i]); + strvec_clear(unknown_opts); + free(unknown_opts); - int i; - for (i = 0; i < unknown_opts->nr; i++) { - printf("free unknown option: %s\n", unknown_opts->v[i]); - } - strvec_clear(unknown_opts); - free(unknown_opts); -} \ No newline at end of file + return 0; +} diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c index 79ec4f9cda07..5af5acae1cb3 100644 --- a/t/helper/test-tool.c +++ b/t/helper/test-tool.c @@ -51,7 +51,7 @@ static struct test_cmd cmds[] = { { "parse-options-flags", cmd__parse_options_flags }, { "parse-pathspec-file", cmd__parse_pathspec_file }, { "parse-subcommand", cmd__parse_subcommand }, - { "free-unknown-options", cmd__free_unknown_options}, + { "free-unknown-options", cmd__free_unknown_options }, { "partial-clone", cmd__partial_clone }, { "path-utils", cmd__path_utils }, { "path-walk", cmd__path_walk }, -- gitgitgadget