parse-options: add --git-completion-helper-all
--git-completion-helper excludes hidden options, such as --allow-empty for git commit. This is typically helpful, but occasionally we want auto-completion for obscure flags. --git-completion-helper-all returns all options, even if they are marked as hidden or nocomplete. Signed-off-by: Ryan Zoeller <rtzoeller@rtzoeller.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ryan Zoeller committed
Aug 19, 2020 at 23:06 UTC
a0abe5e3b72c721a687ad9dc1a4a9bafe938c738
1 file changed
+17
-9
parse-options.c
+17
-9
@@ -525,7 +525,8 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
525
parse_options_start_1(ctx, argc, argv, prefix, options, flags);
526
}
527
528
-static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
528
+static void show_negated_gitcomp(const struct option *opts, int show_all,
529
+ int nr_noopts)
530
{
531
int printed_dashdash = 0;
532
@@ -535,7 +536,8 @@ static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
536
537
if (!opts->long_name)
538
continue;
538
- if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
539
+ if (!show_all &&
540
+ (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
541
continue;
542
if (opts->flags & PARSE_OPT_NONEG)
543
continue;
@@ -572,7 +574,7 @@ static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
574
}
575
}
576
575
-static int show_gitcomp(const struct option *opts)
577
+static int show_gitcomp(const struct option *opts, int show_all)
578
{
579
const struct option *original_opts = opts;
580
int nr_noopts = 0;
@@ -582,7 +584,8 @@ static int show_gitcomp(const struct option *opts)
584
585
if (!opts->long_name)
586
continue;
585
- if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
587
+ if (!show_all &&
588
+ (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
589
continue;
590
591
switch (opts->type) {
@@ -610,8 +613,8 @@ static int show_gitcomp(const struct option *opts)
613
nr_noopts++;
614
printf(" --%s%s", opts->long_name, suffix);
615
}
613
- show_negated_gitcomp(original_opts, -1);
614
- show_negated_gitcomp(original_opts, nr_noopts);
616
+ show_negated_gitcomp(original_opts, show_all, -1);
617
+ show_negated_gitcomp(original_opts, show_all, nr_noopts);
618
fputc('\n', stdout);
619
return PARSE_OPT_COMPLETE;
620
}
@@ -723,9 +726,14 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
726
if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
727
goto show_usage;
728
726
- /* lone --git-completion-helper is asked by git-completion.bash */
727
- if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
728
- return show_gitcomp(options);
729
+ /*
730
+ * lone --git-completion-helper and --git-completion-helper-all
731
+ * are asked by git-completion.bash
732
+ */
733
+ if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper"))
734
+ return show_gitcomp(options, 0);
735
+ if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all"))
736
+ return show_gitcomp(options, 1);
737
738
if (arg[1] != '-') {
739
ctx->opt = arg + 1;