parse-options: Allow to hide options from the default usage.
This is useful for backward-compatibility aliases, or very advanced command line switches introduced for internal git usages and have no real use for a user. parse-options still shows them if the user asks for --help-all. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pierre Habouzit committed
Nov 19, 2007 at 10:21 UTC
dd3bf0f4a5bab17011e94bfeb808ee64dd2ad040
2 files changed
+18
-2
parse-options.c
+15
-2
@@ -216,6 +216,9 @@ is_abbreviated:
216
return error("unknown option `%s'", arg);
217
}
218
219
+static NORETURN void usage_with_options_internal(const char * const *,
220
+ const struct option *, int);
221
+
222
int parse_options(int argc, const char **argv, const struct option *options,
223
const char * const usagestr[], int flags)
224
{
@@ -249,6 +252,8 @@ int parse_options(int argc, const char **argv, const struct option *options,
252
break;
253
}
254
255
+ if (!strcmp(arg + 2, "help-all"))
256
+ usage_with_options_internal(usagestr, options, 1);
257
if (!strcmp(arg + 2, "help"))
258
usage_with_options(usagestr, options);
259
if (parse_long_opt(&args, arg + 2, options))
@@ -263,8 +268,8 @@ int parse_options(int argc, const char **argv, const struct option *options,
268
#define USAGE_OPTS_WIDTH 24
269
#define USAGE_GAP 2
270
266
-void usage_with_options(const char * const *usagestr,
267
- const struct option *opts)
271
+void usage_with_options_internal(const char * const *usagestr,
272
+ const struct option *opts, int full)
273
{
274
fprintf(stderr, "usage: %s\n", *usagestr++);
275
while (*usagestr && **usagestr)
@@ -285,6 +290,8 @@ void usage_with_options(const char * const *usagestr,
290
fprintf(stderr, "%s\n", opts->help);
291
continue;
292
}
293
+ if (!full && (opts->flags & PARSE_OPT_HIDDEN))
294
+ continue;
295
296
pos = fprintf(stderr, " ");
297
if (opts->short_name)
@@ -335,6 +342,12 @@ void usage_with_options(const char * const *usagestr,
342
exit(129);
343
}
344
345
+void usage_with_options(const char * const *usagestr,
346
+ const struct option *opts)
347
+{
348
+ usage_with_options_internal(usagestr, opts, 0);
349
+}
350
+
351
/*----- some often used options -----*/
352
#include "cache.h"
353
parse-options.h
+3
@@ -24,6 +24,7 @@ enum parse_opt_option_flags {
24
PARSE_OPT_OPTARG = 1,
25
PARSE_OPT_NOARG = 2,
26
PARSE_OPT_NONEG = 4,
27
+ PARSE_OPT_HIDDEN = 8,
28
};
29
30
struct option;
@@ -57,6 +58,8 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
58
* PARSE_OPT_OPTARG: says that the argument is optionnal (not for BOOLEANs)
59
* PARSE_OPT_NOARG: says that this option takes no argument, for CALLBACKs
60
* PARSE_OPT_NONEG: says that this option cannot be negated
61
+ * PARSE_OPT_HIDDEN this option is skipped in the default usage, showed in
62
+ * the long one.
63
*
64
* `callback`::
65
* pointer to the callback to use for OPTION_CALLBACK.