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 <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Jul 27, 2009 at 20:49 UTC
86b5efb2864ca50d86437f94ec4c26042cba193e
2 files changed
+8
-2
parse-options.c
+4
-2
@@ -511,7 +511,7 @@ static int usage_with_options_internal(const char * const *usagestr,
511
continue;
512
513
pos = fprintf(stderr, " ");
514
- if (opts->short_name) {
514
+ if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
515
if (opts->flags & PARSE_OPT_NODASH)
516
pos += fprintf(stderr, "%c", opts->short_name);
517
else
@@ -520,7 +520,9 @@ static int usage_with_options_internal(const char * const *usagestr,
520
if (opts->long_name && opts->short_name)
521
pos += fprintf(stderr, ", ");
522
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);
526
if (opts->type == OPTION_NUMBER)
527
pos += fprintf(stderr, "-NUM");
528
parse-options.h
+4
@@ -36,6 +36,7 @@ enum parse_opt_option_flags {
36
PARSE_OPT_LASTARG_DEFAULT = 16,
37
PARSE_OPT_NODASH = 32,
38
PARSE_OPT_LITERAL_ARGHELP = 64,
39
+ PARSE_OPT_NEGHELP = 128,
40
};
41
42
struct option;
@@ -80,6 +81,9 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
81
* PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets
82
* (i.e. '<argh>') in the help message.
83
* 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.
87
*
88
* `callback`::
89
* pointer to the callback to use for OPTION_CALLBACK.