Skip to content

Commit

Permalink
Add shortcuts for very often used options.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Pierre Habouzit authored and Junio C Hamano committed Oct 30, 2007
1 parent ffe659f commit 0ce865b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
21 changes: 21 additions & 0 deletions parse-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,24 @@ void usage_with_options(const char * const *usagestr,

exit(129);
}

/*----- some often used options -----*/
#include "cache.h"
int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
{
int v;

if (!arg) {
v = unset ? 0 : DEFAULT_ABBREV;
} else {
v = strtol(arg, (char **)&arg, 10);
if (*arg)
return opterror(opt, "expects a numerical value", 0);
if (v && v < MINIMUM_ABBREV)
v = MINIMUM_ABBREV;
else if (v > 40)
v = 40;
}
*(int *)(opt->value) = v;
return 0;
}
11 changes: 11 additions & 0 deletions parse-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,15 @@ extern int parse_options(int argc, const char **argv,
extern NORETURN void usage_with_options(const char * const *usagestr,
const struct option *options);

/*----- some often used options -----*/
extern int parse_opt_abbrev_cb(const struct option *, const char *, int);

#define OPT__VERBOSE(var) OPT_BOOLEAN('v', "verbose", (var), "be verbose")
#define OPT__QUIET(var) OPT_BOOLEAN('q', "quiet", (var), "be quiet")
#define OPT__DRY_RUN(var) OPT_BOOLEAN('n', "dry-run", (var), "dry run")
#define OPT__ABBREV(var) \
{ OPTION_CALLBACK, 0, "abbrev", (var), "n", \
"use <n> digits to display SHA-1s", \
PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }

#endif

0 comments on commit 0ce865b

Please sign in to comment.