Skip to content

Commit 86b5efb

Browse files
dschogitster
authored andcommitted
parse-opt: optionally show "--no-" option string
It is usually better to have positive options, to avoid confusing double negations. However, sometimes it is desirable to show the negative option in the help. Introduce the flag PARSE_OPT_NEGHELP to do that. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e658002 commit 86b5efb

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

parse-options.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ static int usage_with_options_internal(const char * const *usagestr,
511511
continue;
512512

513513
pos = fprintf(stderr, " ");
514-
if (opts->short_name) {
514+
if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
515515
if (opts->flags & PARSE_OPT_NODASH)
516516
pos += fprintf(stderr, "%c", opts->short_name);
517517
else
@@ -520,7 +520,9 @@ static int usage_with_options_internal(const char * const *usagestr,
520520
if (opts->long_name && opts->short_name)
521521
pos += fprintf(stderr, ", ");
522522
if (opts->long_name)
523-
pos += fprintf(stderr, "--%s", opts->long_name);
523+
pos += fprintf(stderr, "--%s%s",
524+
(opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
525+
opts->long_name);
524526
if (opts->type == OPTION_NUMBER)
525527
pos += fprintf(stderr, "-NUM");
526528

parse-options.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ enum parse_opt_option_flags {
3636
PARSE_OPT_LASTARG_DEFAULT = 16,
3737
PARSE_OPT_NODASH = 32,
3838
PARSE_OPT_LITERAL_ARGHELP = 64,
39+
PARSE_OPT_NEGHELP = 128,
3940
};
4041

4142
struct option;
@@ -80,6 +81,9 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
8081
* PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets
8182
* (i.e. '<argh>') in the help message.
8283
* Useful for options with multiple parameters.
84+
* PARSE_OPT_NEGHELP: says that the long option should always be shown with
85+
* the --no prefix in the usage message. Sometimes
86+
* useful for users of OPTION_NEGBIT.
8387
*
8488
* `callback`::
8589
* pointer to the callback to use for OPTION_CALLBACK.

0 commit comments

Comments
 (0)