@matheusbsbs / matheus-bagliano / commits / 0ce865b134
Fork 0

Add shortcuts for very often used options.

It helps with consistency of the help strings, for example. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Pierre Habouzit committed Oct 14, 2007 at 11:05 UTC 0ce865b134f8ccd60f6e584744144b0978a9fdf2
2 files changed +32
parse-options.c
+21
@@ -254,3 +254,24 @@ void usage_with_options(const char * const *usagestr,
254
255 exit(129);
256 }
257 +
258 +/*----- some often used options -----*/
259 +#include "cache.h"
260 +int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
261 +{
262 + int v;
263 +
264 + if (!arg) {
265 + v = unset ? 0 : DEFAULT_ABBREV;
266 + } else {
267 + v = strtol(arg, (char **)&arg, 10);
268 + if (*arg)
269 + return opterror(opt, "expects a numerical value", 0);
270 + if (v && v < MINIMUM_ABBREV)
271 + v = MINIMUM_ABBREV;
272 + else if (v > 40)
273 + v = 40;
274 + }
275 + *(int *)(opt->value) = v;
276 + return 0;
277 +}
parse-options.h
+11
@@ -55,4 +55,15 @@ extern int parse_options(int argc, const char **argv,
55 extern NORETURN void usage_with_options(const char * const *usagestr,
56 const struct option *options);
57
58 +/*----- some often used options -----*/
59 +extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
60 +
61 +#define OPT__VERBOSE(var) OPT_BOOLEAN('v', "verbose", (var), "be verbose")
62 +#define OPT__QUIET(var) OPT_BOOLEAN('q', "quiet", (var), "be quiet")
63 +#define OPT__DRY_RUN(var) OPT_BOOLEAN('n', "dry-run", (var), "dry run")
64 +#define OPT__ABBREV(var) \
65 + { OPTION_CALLBACK, 0, "abbrev", (var), "n", \
66 + "use <n> digits to display SHA-1s", \
67 + PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }
68 +
69 #endif