Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| 2 | #include "parse-options.h" |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 3 | |
| 4 | #define OPT_SHORT 1 |
| 5 | #define OPT_UNSET 2 |
| 6 | |
| 7 | struct optparse_t { |
| 8 | const char **argv; |
| 9 | int argc; |
| 10 | const char *opt; |
| 11 | }; |
| 12 | |
| 13 | static inline const char *get_arg(struct optparse_t *p) |
| 14 | { |
| 15 | if (p->opt) { |
| 16 | const char *res = p->opt; |
| 17 | p->opt = NULL; |
| 18 | return res; |
| 19 | } |
| 20 | p->argc--; |
| 21 | return *++p->argv; |
| 22 | } |
| 23 | |
| 24 | static inline const char *skip_prefix(const char *str, const char *prefix) |
| 25 | { |
| 26 | size_t len = strlen(prefix); |
| 27 | return strncmp(str, prefix, len) ? NULL : str + len; |
| 28 | } |
| 29 | |
| 30 | static int opterror(const struct option *opt, const char *reason, int flags) |
| 31 | { |
| 32 | if (flags & OPT_SHORT) |
| 33 | return error("switch `%c' %s", opt->short_name, reason); |
| 34 | if (flags & OPT_UNSET) |
| 35 | return error("option `no-%s' %s", opt->long_name, reason); |
| 36 | return error("option `%s' %s", opt->long_name, reason); |
| 37 | } |
| 38 | |
| 39 | static int get_value(struct optparse_t *p, |
| 40 | const struct option *opt, int flags) |
| 41 | { |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 42 | const char *s, *arg; |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 43 | const int unset = flags & OPT_UNSET; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 44 | |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 45 | if (unset && p->opt) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 46 | return opterror(opt, "takes no value", flags); |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 47 | if (unset && (opt->flags & PARSE_OPT_NONEG)) |
| 48 | return opterror(opt, "isn't available", flags); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 49 | |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 50 | if (!(flags & OPT_SHORT) && p->opt) { |
| 51 | switch (opt->type) { |
| 52 | case OPTION_CALLBACK: |
| 53 | if (!(opt->flags & PARSE_OPT_NOARG)) |
| 54 | break; |
| 55 | /* FALLTHROUGH */ |
| 56 | case OPTION_BOOLEAN: |
| 57 | case OPTION_BIT: |
| 58 | case OPTION_SET_INT: |
| 59 | case OPTION_SET_PTR: |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 60 | return opterror(opt, "takes no value", flags); |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 61 | default: |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL); |
| 67 | switch (opt->type) { |
| 68 | case OPTION_BIT: |
| 69 | if (unset) |
| 70 | *(int *)opt->value &= ~opt->defval; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 71 | else |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 72 | *(int *)opt->value |= opt->defval; |
| 73 | return 0; |
| 74 | |
| 75 | case OPTION_BOOLEAN: |
| 76 | *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1; |
| 77 | return 0; |
| 78 | |
| 79 | case OPTION_SET_INT: |
| 80 | *(int *)opt->value = unset ? 0 : opt->defval; |
| 81 | return 0; |
| 82 | |
| 83 | case OPTION_SET_PTR: |
| 84 | *(void **)opt->value = unset ? NULL : (void *)opt->defval; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 85 | return 0; |
| 86 | |
| 87 | case OPTION_STRING: |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 88 | if (unset) { |
| 89 | *(const char **)opt->value = NULL; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 90 | return 0; |
| 91 | } |
Pierre Habouzit | c43a248 | 2007-12-21 11:41:41 +0100 | [diff] [blame] | 92 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 93 | *(const char **)opt->value = (const char *)opt->defval; |
| 94 | return 0; |
| 95 | } |
| 96 | if (!arg) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 97 | return opterror(opt, "requires a value", flags); |
| 98 | *(const char **)opt->value = get_arg(p); |
| 99 | return 0; |
| 100 | |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 101 | case OPTION_CALLBACK: |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 102 | if (unset) |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 103 | return (*opt->callback)(opt, NULL, 1); |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 104 | if (opt->flags & PARSE_OPT_NOARG) |
Pierre Habouzit | f481e22 | 2007-10-16 00:32:38 +0200 | [diff] [blame] | 105 | return (*opt->callback)(opt, NULL, 0); |
Pierre Habouzit | c43a248 | 2007-12-21 11:41:41 +0100 | [diff] [blame] | 106 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 107 | return (*opt->callback)(opt, NULL, 0); |
| 108 | if (!arg) |
| 109 | return opterror(opt, "requires a value", flags); |
| 110 | return (*opt->callback)(opt, get_arg(p), 0); |
| 111 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 112 | case OPTION_INTEGER: |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 113 | if (unset) { |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 114 | *(int *)opt->value = 0; |
| 115 | return 0; |
| 116 | } |
Pierre Habouzit | c43a248 | 2007-12-21 11:41:41 +0100 | [diff] [blame] | 117 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 118 | *(int *)opt->value = opt->defval; |
| 119 | return 0; |
| 120 | } |
| 121 | if (!arg) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 122 | return opterror(opt, "requires a value", flags); |
| 123 | *(int *)opt->value = strtol(get_arg(p), (char **)&s, 10); |
| 124 | if (*s) |
| 125 | return opterror(opt, "expects a numerical value", flags); |
| 126 | return 0; |
| 127 | |
| 128 | default: |
| 129 | die("should not happen, someone must be hit on the forehead"); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static int parse_short_opt(struct optparse_t *p, const struct option *options) |
| 134 | { |
| 135 | for (; options->type != OPTION_END; options++) { |
| 136 | if (options->short_name == *p->opt) { |
| 137 | p->opt = p->opt[1] ? p->opt + 1 : NULL; |
| 138 | return get_value(p, options, OPT_SHORT); |
| 139 | } |
| 140 | } |
| 141 | return error("unknown switch `%c'", *p->opt); |
| 142 | } |
| 143 | |
| 144 | static int parse_long_opt(struct optparse_t *p, const char *arg, |
| 145 | const struct option *options) |
| 146 | { |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 147 | const char *arg_end = strchr(arg, '='); |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 148 | const struct option *abbrev_option = NULL, *ambiguous_option = NULL; |
| 149 | int abbrev_flags = 0, ambiguous_flags = 0; |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 150 | |
| 151 | if (!arg_end) |
| 152 | arg_end = arg + strlen(arg); |
| 153 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 154 | for (; options->type != OPTION_END; options++) { |
| 155 | const char *rest; |
| 156 | int flags = 0; |
| 157 | |
| 158 | if (!options->long_name) |
| 159 | continue; |
| 160 | |
| 161 | rest = skip_prefix(arg, options->long_name); |
| 162 | if (!rest) { |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 163 | /* abbreviated? */ |
| 164 | if (!strncmp(options->long_name, arg, arg_end - arg)) { |
| 165 | is_abbreviated: |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 166 | if (abbrev_option) { |
| 167 | /* |
| 168 | * If this is abbreviated, it is |
| 169 | * ambiguous. So when there is no |
| 170 | * exact match later, we need to |
| 171 | * error out. |
| 172 | */ |
| 173 | ambiguous_option = abbrev_option; |
| 174 | ambiguous_flags = abbrev_flags; |
| 175 | } |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 176 | if (!(flags & OPT_UNSET) && *arg_end) |
| 177 | p->opt = arg_end + 1; |
| 178 | abbrev_option = options; |
| 179 | abbrev_flags = flags; |
| 180 | continue; |
| 181 | } |
| 182 | /* negated and abbreviated very much? */ |
| 183 | if (!prefixcmp("no-", arg)) { |
| 184 | flags |= OPT_UNSET; |
| 185 | goto is_abbreviated; |
| 186 | } |
| 187 | /* negated? */ |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 188 | if (strncmp(arg, "no-", 3)) |
| 189 | continue; |
| 190 | flags |= OPT_UNSET; |
| 191 | rest = skip_prefix(arg + 3, options->long_name); |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 192 | /* abbreviated and negated? */ |
| 193 | if (!rest && !prefixcmp(options->long_name, arg + 3)) |
| 194 | goto is_abbreviated; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 195 | if (!rest) |
| 196 | continue; |
| 197 | } |
| 198 | if (*rest) { |
| 199 | if (*rest != '=') |
| 200 | continue; |
| 201 | p->opt = rest + 1; |
| 202 | } |
| 203 | return get_value(p, options, flags); |
| 204 | } |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 205 | |
| 206 | if (ambiguous_option) |
| 207 | return error("Ambiguous option: %s " |
| 208 | "(could be --%s%s or --%s%s)", |
| 209 | arg, |
| 210 | (ambiguous_flags & OPT_UNSET) ? "no-" : "", |
| 211 | ambiguous_option->long_name, |
| 212 | (abbrev_flags & OPT_UNSET) ? "no-" : "", |
| 213 | abbrev_option->long_name); |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 214 | if (abbrev_option) |
| 215 | return get_value(p, abbrev_option, abbrev_flags); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 216 | return error("unknown option `%s'", arg); |
| 217 | } |
| 218 | |
Pierre Habouzit | 3a9f0f4 | 2008-01-26 12:26:57 +0100 | [diff] [blame] | 219 | void check_typos(const char *arg, const struct option *options) |
| 220 | { |
| 221 | if (strlen(arg) < 3) |
| 222 | return; |
| 223 | |
| 224 | if (!prefixcmp(arg, "no-")) { |
| 225 | error ("did you mean `--%s` (with two dashes ?)", arg); |
| 226 | exit(129); |
| 227 | } |
| 228 | |
| 229 | for (; options->type != OPTION_END; options++) { |
| 230 | if (!options->long_name) |
| 231 | continue; |
| 232 | if (!prefixcmp(options->long_name, arg)) { |
| 233 | error ("did you mean `--%s` (with two dashes ?)", arg); |
| 234 | exit(129); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 239 | static NORETURN void usage_with_options_internal(const char * const *, |
| 240 | const struct option *, int); |
| 241 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 242 | int parse_options(int argc, const char **argv, const struct option *options, |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 243 | const char * const usagestr[], int flags) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 244 | { |
| 245 | struct optparse_t args = { argv + 1, argc - 1, NULL }; |
| 246 | int j = 0; |
| 247 | |
| 248 | for (; args.argc; args.argc--, args.argv++) { |
| 249 | const char *arg = args.argv[0]; |
| 250 | |
| 251 | if (*arg != '-' || !arg[1]) { |
| 252 | argv[j++] = args.argv[0]; |
| 253 | continue; |
| 254 | } |
| 255 | |
| 256 | if (arg[1] != '-') { |
| 257 | args.opt = arg + 1; |
Pierre Habouzit | 3a9f0f4 | 2008-01-26 12:26:57 +0100 | [diff] [blame] | 258 | if (*args.opt == 'h') |
| 259 | usage_with_options(usagestr, options); |
| 260 | if (parse_short_opt(&args, options) < 0) |
| 261 | usage_with_options(usagestr, options); |
| 262 | if (args.opt) |
| 263 | check_typos(arg + 1, options); |
| 264 | while (args.opt) { |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 265 | if (*args.opt == 'h') |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 266 | usage_with_options(usagestr, options); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 267 | if (parse_short_opt(&args, options) < 0) |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 268 | usage_with_options(usagestr, options); |
Pierre Habouzit | 3a9f0f4 | 2008-01-26 12:26:57 +0100 | [diff] [blame] | 269 | } |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 270 | continue; |
| 271 | } |
| 272 | |
| 273 | if (!arg[2]) { /* "--" */ |
| 274 | if (!(flags & PARSE_OPT_KEEP_DASHDASH)) { |
| 275 | args.argc--; |
| 276 | args.argv++; |
| 277 | } |
| 278 | break; |
| 279 | } |
| 280 | |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 281 | if (!strcmp(arg + 2, "help-all")) |
| 282 | usage_with_options_internal(usagestr, options, 1); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 283 | if (!strcmp(arg + 2, "help")) |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 284 | usage_with_options(usagestr, options); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 285 | if (parse_long_opt(&args, arg + 2, options)) |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 286 | usage_with_options(usagestr, options); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | memmove(argv + j, args.argv, args.argc * sizeof(*argv)); |
| 290 | argv[j + args.argc] = NULL; |
| 291 | return j + args.argc; |
| 292 | } |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 293 | |
| 294 | #define USAGE_OPTS_WIDTH 24 |
| 295 | #define USAGE_GAP 2 |
| 296 | |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 297 | void usage_with_options_internal(const char * const *usagestr, |
| 298 | const struct option *opts, int full) |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 299 | { |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 300 | fprintf(stderr, "usage: %s\n", *usagestr++); |
| 301 | while (*usagestr && **usagestr) |
| 302 | fprintf(stderr, " or: %s\n", *usagestr++); |
| 303 | while (*usagestr) |
| 304 | fprintf(stderr, " %s\n", *usagestr++); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 305 | |
| 306 | if (opts->type != OPTION_GROUP) |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 307 | fputc('\n', stderr); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 308 | |
| 309 | for (; opts->type != OPTION_END; opts++) { |
| 310 | size_t pos; |
| 311 | int pad; |
| 312 | |
| 313 | if (opts->type == OPTION_GROUP) { |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 314 | fputc('\n', stderr); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 315 | if (*opts->help) |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 316 | fprintf(stderr, "%s\n", opts->help); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 317 | continue; |
| 318 | } |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 319 | if (!full && (opts->flags & PARSE_OPT_HIDDEN)) |
| 320 | continue; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 321 | |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 322 | pos = fprintf(stderr, " "); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 323 | if (opts->short_name) |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 324 | pos += fprintf(stderr, "-%c", opts->short_name); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 325 | if (opts->long_name && opts->short_name) |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 326 | pos += fprintf(stderr, ", "); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 327 | if (opts->long_name) |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 328 | pos += fprintf(stderr, "--%s", opts->long_name); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 329 | |
| 330 | switch (opts->type) { |
| 331 | case OPTION_INTEGER: |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 332 | if (opts->flags & PARSE_OPT_OPTARG) |
| 333 | pos += fprintf(stderr, " [<n>]"); |
| 334 | else |
| 335 | pos += fprintf(stderr, " <n>"); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 336 | break; |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 337 | case OPTION_CALLBACK: |
Pierre Habouzit | f481e22 | 2007-10-16 00:32:38 +0200 | [diff] [blame] | 338 | if (opts->flags & PARSE_OPT_NOARG) |
| 339 | break; |
| 340 | /* FALLTHROUGH */ |
| 341 | case OPTION_STRING: |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 342 | if (opts->argh) { |
| 343 | if (opts->flags & PARSE_OPT_OPTARG) |
| 344 | pos += fprintf(stderr, " [<%s>]", opts->argh); |
| 345 | else |
| 346 | pos += fprintf(stderr, " <%s>", opts->argh); |
| 347 | } else { |
| 348 | if (opts->flags & PARSE_OPT_OPTARG) |
| 349 | pos += fprintf(stderr, " [...]"); |
| 350 | else |
| 351 | pos += fprintf(stderr, " ..."); |
| 352 | } |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 353 | break; |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 354 | default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */ |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 355 | break; |
| 356 | } |
| 357 | |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 358 | if (pos <= USAGE_OPTS_WIDTH) |
| 359 | pad = USAGE_OPTS_WIDTH - pos; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 360 | else { |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 361 | fputc('\n', stderr); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 362 | pad = USAGE_OPTS_WIDTH; |
| 363 | } |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 364 | fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 365 | } |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 366 | fputc('\n', stderr); |
| 367 | |
| 368 | exit(129); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 369 | } |
Pierre Habouzit | 0ce865b | 2007-10-14 11:05:12 +0200 | [diff] [blame] | 370 | |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 371 | void usage_with_options(const char * const *usagestr, |
| 372 | const struct option *opts) |
| 373 | { |
| 374 | usage_with_options_internal(usagestr, opts, 0); |
| 375 | } |
| 376 | |
Pierre Habouzit | 0ce865b | 2007-10-14 11:05:12 +0200 | [diff] [blame] | 377 | /*----- some often used options -----*/ |
| 378 | #include "cache.h" |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 379 | |
Pierre Habouzit | 0ce865b | 2007-10-14 11:05:12 +0200 | [diff] [blame] | 380 | int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset) |
| 381 | { |
| 382 | int v; |
| 383 | |
| 384 | if (!arg) { |
| 385 | v = unset ? 0 : DEFAULT_ABBREV; |
| 386 | } else { |
| 387 | v = strtol(arg, (char **)&arg, 10); |
| 388 | if (*arg) |
| 389 | return opterror(opt, "expects a numerical value", 0); |
| 390 | if (v && v < MINIMUM_ABBREV) |
| 391 | v = MINIMUM_ABBREV; |
| 392 | else if (v > 40) |
| 393 | v = 40; |
| 394 | } |
| 395 | *(int *)(opt->value) = v; |
| 396 | return 0; |
| 397 | } |