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 | 26141b5 | 2008-06-23 22:55:11 +0200 | [diff] [blame] | 3 | #include "cache.h" |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 4 | |
| 5 | #define OPT_SHORT 1 |
| 6 | #define OPT_UNSET 2 |
| 7 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 8 | static int opterror(const struct option *opt, const char *reason, int flags) |
| 9 | { |
| 10 | if (flags & OPT_SHORT) |
| 11 | return error("switch `%c' %s", opt->short_name, reason); |
| 12 | if (flags & OPT_UNSET) |
| 13 | return error("option `no-%s' %s", opt->long_name, reason); |
| 14 | return error("option `%s' %s", opt->long_name, reason); |
| 15 | } |
| 16 | |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 17 | static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt, |
| 18 | int flags, const char **arg) |
| 19 | { |
| 20 | if (p->opt) { |
| 21 | *arg = p->opt; |
| 22 | p->opt = NULL; |
| 23 | } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) { |
| 24 | *arg = (const char *)opt->defval; |
Olivier Marin | d5d745f | 2008-07-21 20:30:36 +0200 | [diff] [blame] | 25 | } else if (p->argc > 1) { |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 26 | p->argc--; |
| 27 | *arg = *++p->argv; |
| 28 | } else |
| 29 | return opterror(opt, "requires a value", flags); |
| 30 | return 0; |
| 31 | } |
| 32 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 33 | static int get_value(struct parse_opt_ctx_t *p, |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 34 | const struct option *opt, int flags) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 35 | { |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 36 | const char *s, *arg; |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 37 | const int unset = flags & OPT_UNSET; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 38 | |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 39 | if (unset && p->opt) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 40 | return opterror(opt, "takes no value", flags); |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 41 | if (unset && (opt->flags & PARSE_OPT_NONEG)) |
| 42 | return opterror(opt, "isn't available", flags); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 43 | |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 44 | if (!(flags & OPT_SHORT) && p->opt) { |
| 45 | switch (opt->type) { |
| 46 | case OPTION_CALLBACK: |
| 47 | if (!(opt->flags & PARSE_OPT_NOARG)) |
| 48 | break; |
| 49 | /* FALLTHROUGH */ |
| 50 | case OPTION_BOOLEAN: |
| 51 | case OPTION_BIT: |
| 52 | case OPTION_SET_INT: |
| 53 | case OPTION_SET_PTR: |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 54 | return opterror(opt, "takes no value", flags); |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 55 | default: |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 60 | switch (opt->type) { |
| 61 | case OPTION_BIT: |
| 62 | if (unset) |
| 63 | *(int *)opt->value &= ~opt->defval; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 64 | else |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 65 | *(int *)opt->value |= opt->defval; |
| 66 | return 0; |
| 67 | |
| 68 | case OPTION_BOOLEAN: |
| 69 | *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1; |
| 70 | return 0; |
| 71 | |
| 72 | case OPTION_SET_INT: |
| 73 | *(int *)opt->value = unset ? 0 : opt->defval; |
| 74 | return 0; |
| 75 | |
| 76 | case OPTION_SET_PTR: |
| 77 | *(void **)opt->value = unset ? NULL : (void *)opt->defval; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 78 | return 0; |
| 79 | |
| 80 | case OPTION_STRING: |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 81 | if (unset) |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 82 | *(const char **)opt->value = NULL; |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 83 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 84 | *(const char **)opt->value = (const char *)opt->defval; |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 85 | else |
| 86 | return get_arg(p, opt, flags, (const char **)opt->value); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 87 | return 0; |
| 88 | |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 89 | case OPTION_CALLBACK: |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 90 | if (unset) |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 91 | return (*opt->callback)(opt, NULL, 1) ? (-1) : 0; |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 92 | if (opt->flags & PARSE_OPT_NOARG) |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 93 | return (*opt->callback)(opt, NULL, 0) ? (-1) : 0; |
Pierre Habouzit | c43a248 | 2007-12-21 11:41:41 +0100 | [diff] [blame] | 94 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 95 | return (*opt->callback)(opt, NULL, 0) ? (-1) : 0; |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 96 | if (get_arg(p, opt, flags, &arg)) |
| 97 | return -1; |
| 98 | return (*opt->callback)(opt, arg, 0) ? (-1) : 0; |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 99 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 100 | case OPTION_INTEGER: |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 101 | if (unset) { |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 102 | *(int *)opt->value = 0; |
| 103 | return 0; |
| 104 | } |
Pierre Habouzit | c43a248 | 2007-12-21 11:41:41 +0100 | [diff] [blame] | 105 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 106 | *(int *)opt->value = opt->defval; |
| 107 | return 0; |
| 108 | } |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 109 | if (get_arg(p, opt, flags, &arg)) |
| 110 | return -1; |
| 111 | *(int *)opt->value = strtol(arg, (char **)&s, 10); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 112 | if (*s) |
| 113 | return opterror(opt, "expects a numerical value", flags); |
| 114 | return 0; |
| 115 | |
| 116 | default: |
| 117 | die("should not happen, someone must be hit on the forehead"); |
| 118 | } |
| 119 | } |
| 120 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 121 | static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 122 | { |
| 123 | for (; options->type != OPTION_END; options++) { |
| 124 | if (options->short_name == *p->opt) { |
| 125 | p->opt = p->opt[1] ? p->opt + 1 : NULL; |
| 126 | return get_value(p, options, OPT_SHORT); |
| 127 | } |
| 128 | } |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 129 | return -2; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 132 | static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg, |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 133 | const struct option *options) |
| 134 | { |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 135 | const char *arg_end = strchr(arg, '='); |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 136 | const struct option *abbrev_option = NULL, *ambiguous_option = NULL; |
| 137 | int abbrev_flags = 0, ambiguous_flags = 0; |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 138 | |
| 139 | if (!arg_end) |
| 140 | arg_end = arg + strlen(arg); |
| 141 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 142 | for (; options->type != OPTION_END; options++) { |
| 143 | const char *rest; |
| 144 | int flags = 0; |
| 145 | |
| 146 | if (!options->long_name) |
| 147 | continue; |
| 148 | |
| 149 | rest = skip_prefix(arg, options->long_name); |
Pierre Habouzit | 580d5bf | 2008-03-02 11:35:56 +0100 | [diff] [blame] | 150 | if (options->type == OPTION_ARGUMENT) { |
| 151 | if (!rest) |
| 152 | continue; |
| 153 | if (*rest == '=') |
| 154 | return opterror(options, "takes no value", flags); |
| 155 | if (*rest) |
| 156 | continue; |
| 157 | p->out[p->cpidx++] = arg - 2; |
| 158 | return 0; |
| 159 | } |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 160 | if (!rest) { |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 161 | /* abbreviated? */ |
| 162 | if (!strncmp(options->long_name, arg, arg_end - arg)) { |
| 163 | is_abbreviated: |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 164 | if (abbrev_option) { |
| 165 | /* |
| 166 | * If this is abbreviated, it is |
| 167 | * ambiguous. So when there is no |
| 168 | * exact match later, we need to |
| 169 | * error out. |
| 170 | */ |
| 171 | ambiguous_option = abbrev_option; |
| 172 | ambiguous_flags = abbrev_flags; |
| 173 | } |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 174 | if (!(flags & OPT_UNSET) && *arg_end) |
| 175 | p->opt = arg_end + 1; |
| 176 | abbrev_option = options; |
| 177 | abbrev_flags = flags; |
| 178 | continue; |
| 179 | } |
| 180 | /* negated and abbreviated very much? */ |
| 181 | if (!prefixcmp("no-", arg)) { |
| 182 | flags |= OPT_UNSET; |
| 183 | goto is_abbreviated; |
| 184 | } |
| 185 | /* negated? */ |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 186 | if (strncmp(arg, "no-", 3)) |
| 187 | continue; |
| 188 | flags |= OPT_UNSET; |
| 189 | rest = skip_prefix(arg + 3, options->long_name); |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 190 | /* abbreviated and negated? */ |
| 191 | if (!rest && !prefixcmp(options->long_name, arg + 3)) |
| 192 | goto is_abbreviated; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 193 | if (!rest) |
| 194 | continue; |
| 195 | } |
| 196 | if (*rest) { |
| 197 | if (*rest != '=') |
| 198 | continue; |
| 199 | p->opt = rest + 1; |
| 200 | } |
| 201 | return get_value(p, options, flags); |
| 202 | } |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 203 | |
| 204 | if (ambiguous_option) |
| 205 | return error("Ambiguous option: %s " |
| 206 | "(could be --%s%s or --%s%s)", |
| 207 | arg, |
| 208 | (ambiguous_flags & OPT_UNSET) ? "no-" : "", |
| 209 | ambiguous_option->long_name, |
| 210 | (abbrev_flags & OPT_UNSET) ? "no-" : "", |
| 211 | abbrev_option->long_name); |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 212 | if (abbrev_option) |
| 213 | return get_value(p, abbrev_option, abbrev_flags); |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 214 | return -2; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Nanako Shiraishi | 1121a87 | 2008-07-16 19:42:18 +0900 | [diff] [blame] | 217 | static void check_typos(const char *arg, const struct option *options) |
Pierre Habouzit | 3a9f0f4 | 2008-01-26 12:26:57 +0100 | [diff] [blame] | 218 | { |
| 219 | if (strlen(arg) < 3) |
| 220 | return; |
| 221 | |
| 222 | if (!prefixcmp(arg, "no-")) { |
| 223 | error ("did you mean `--%s` (with two dashes ?)", arg); |
| 224 | exit(129); |
| 225 | } |
| 226 | |
| 227 | for (; options->type != OPTION_END; options++) { |
| 228 | if (!options->long_name) |
| 229 | continue; |
| 230 | if (!prefixcmp(options->long_name, arg)) { |
| 231 | error ("did you mean `--%s` (with two dashes ?)", arg); |
| 232 | exit(129); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 237 | void parse_options_start(struct parse_opt_ctx_t *ctx, |
| 238 | int argc, const char **argv, int flags) |
| 239 | { |
| 240 | memset(ctx, 0, sizeof(*ctx)); |
| 241 | ctx->argc = argc - 1; |
| 242 | ctx->argv = argv + 1; |
| 243 | ctx->out = argv; |
Pierre Habouzit | a32a4ea | 2008-06-24 00:31:31 +0200 | [diff] [blame] | 244 | ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0); |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 245 | ctx->flags = flags; |
| 246 | } |
| 247 | |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 248 | static int usage_with_options_internal(const char * const *, |
| 249 | const struct option *, int); |
| 250 | |
| 251 | int parse_options_step(struct parse_opt_ctx_t *ctx, |
| 252 | const struct option *options, |
| 253 | const char * const usagestr[]) |
| 254 | { |
Pierre Habouzit | 26141b5 | 2008-06-23 22:55:11 +0200 | [diff] [blame] | 255 | /* we must reset ->opt, unknown short option leave it dangling */ |
| 256 | ctx->opt = NULL; |
| 257 | |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 258 | for (; ctx->argc; ctx->argc--, ctx->argv++) { |
| 259 | const char *arg = ctx->argv[0]; |
| 260 | |
| 261 | if (*arg != '-' || !arg[1]) { |
| 262 | if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION) |
| 263 | break; |
| 264 | ctx->out[ctx->cpidx++] = ctx->argv[0]; |
| 265 | continue; |
| 266 | } |
| 267 | |
| 268 | if (arg[1] != '-') { |
| 269 | ctx->opt = arg + 1; |
| 270 | if (*ctx->opt == 'h') |
| 271 | return parse_options_usage(usagestr, options); |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 272 | switch (parse_short_opt(ctx, options)) { |
| 273 | case -1: |
| 274 | return parse_options_usage(usagestr, options); |
| 275 | case -2: |
| 276 | return PARSE_OPT_UNKNOWN; |
| 277 | } |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 278 | if (ctx->opt) |
| 279 | check_typos(arg + 1, options); |
| 280 | while (ctx->opt) { |
| 281 | if (*ctx->opt == 'h') |
| 282 | return parse_options_usage(usagestr, options); |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 283 | switch (parse_short_opt(ctx, options)) { |
| 284 | case -1: |
| 285 | return parse_options_usage(usagestr, options); |
| 286 | case -2: |
Pierre Habouzit | 26141b5 | 2008-06-23 22:55:11 +0200 | [diff] [blame] | 287 | /* fake a short option thing to hide the fact that we may have |
| 288 | * started to parse aggregated stuff |
| 289 | * |
| 290 | * This is leaky, too bad. |
| 291 | */ |
| 292 | ctx->argv[0] = xstrdup(ctx->opt - 1); |
| 293 | *(char *)ctx->argv[0] = '-'; |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 294 | return PARSE_OPT_UNKNOWN; |
| 295 | } |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 296 | } |
| 297 | continue; |
| 298 | } |
| 299 | |
| 300 | if (!arg[2]) { /* "--" */ |
| 301 | if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) { |
| 302 | ctx->argc--; |
| 303 | ctx->argv++; |
| 304 | } |
| 305 | break; |
| 306 | } |
| 307 | |
| 308 | if (!strcmp(arg + 2, "help-all")) |
| 309 | return usage_with_options_internal(usagestr, options, 1); |
| 310 | if (!strcmp(arg + 2, "help")) |
| 311 | return parse_options_usage(usagestr, options); |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 312 | switch (parse_long_opt(ctx, arg + 2, options)) { |
| 313 | case -1: |
| 314 | return parse_options_usage(usagestr, options); |
| 315 | case -2: |
| 316 | return PARSE_OPT_UNKNOWN; |
| 317 | } |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 318 | } |
| 319 | return PARSE_OPT_DONE; |
| 320 | } |
| 321 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 322 | int parse_options_end(struct parse_opt_ctx_t *ctx) |
| 323 | { |
| 324 | memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out)); |
| 325 | ctx->out[ctx->cpidx + ctx->argc] = NULL; |
| 326 | return ctx->cpidx + ctx->argc; |
| 327 | } |
| 328 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 329 | int parse_options(int argc, const char **argv, const struct option *options, |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 330 | const char * const usagestr[], int flags) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 331 | { |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 332 | struct parse_opt_ctx_t ctx; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 333 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 334 | parse_options_start(&ctx, argc, argv, flags); |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 335 | switch (parse_options_step(&ctx, options, usagestr)) { |
| 336 | case PARSE_OPT_HELP: |
| 337 | exit(129); |
| 338 | case PARSE_OPT_DONE: |
| 339 | break; |
| 340 | default: /* PARSE_OPT_UNKNOWN */ |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 341 | if (ctx.argv[0][1] == '-') { |
| 342 | error("unknown option `%s'", ctx.argv[0] + 2); |
| 343 | } else { |
| 344 | error("unknown switch `%c'", *ctx.opt); |
| 345 | } |
| 346 | usage_with_options(usagestr, options); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 347 | } |
| 348 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 349 | return parse_options_end(&ctx); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 350 | } |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 351 | |
| 352 | #define USAGE_OPTS_WIDTH 24 |
| 353 | #define USAGE_GAP 2 |
| 354 | |
Pierre Habouzit | ee68b87 | 2008-06-23 22:28:04 +0200 | [diff] [blame] | 355 | int usage_with_options_internal(const char * const *usagestr, |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 356 | const struct option *opts, int full) |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 357 | { |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 358 | fprintf(stderr, "usage: %s\n", *usagestr++); |
| 359 | while (*usagestr && **usagestr) |
| 360 | fprintf(stderr, " or: %s\n", *usagestr++); |
Jeff King | 44d86e9 | 2008-06-14 03:27:21 -0400 | [diff] [blame] | 361 | while (*usagestr) { |
| 362 | fprintf(stderr, "%s%s\n", |
| 363 | **usagestr ? " " : "", |
| 364 | *usagestr); |
| 365 | usagestr++; |
| 366 | } |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 367 | |
| 368 | if (opts->type != OPTION_GROUP) |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 369 | fputc('\n', stderr); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 370 | |
| 371 | for (; opts->type != OPTION_END; opts++) { |
| 372 | size_t pos; |
| 373 | int pad; |
| 374 | |
| 375 | if (opts->type == OPTION_GROUP) { |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 376 | fputc('\n', stderr); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 377 | if (*opts->help) |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 378 | fprintf(stderr, "%s\n", opts->help); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 379 | continue; |
| 380 | } |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 381 | if (!full && (opts->flags & PARSE_OPT_HIDDEN)) |
| 382 | continue; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 383 | |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 384 | pos = fprintf(stderr, " "); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 385 | if (opts->short_name) |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 386 | pos += fprintf(stderr, "-%c", opts->short_name); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 387 | if (opts->long_name && opts->short_name) |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 388 | pos += fprintf(stderr, ", "); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 389 | if (opts->long_name) |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 390 | pos += fprintf(stderr, "--%s", opts->long_name); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 391 | |
| 392 | switch (opts->type) { |
Pierre Habouzit | 580d5bf | 2008-03-02 11:35:56 +0100 | [diff] [blame] | 393 | case OPTION_ARGUMENT: |
| 394 | break; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 395 | case OPTION_INTEGER: |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 396 | if (opts->flags & PARSE_OPT_OPTARG) |
Michele Ballabio | 6422f63 | 2008-06-22 16:39:04 +0200 | [diff] [blame] | 397 | if (opts->long_name) |
| 398 | pos += fprintf(stderr, "[=<n>]"); |
| 399 | else |
| 400 | pos += fprintf(stderr, "[<n>]"); |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 401 | else |
| 402 | pos += fprintf(stderr, " <n>"); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 403 | break; |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 404 | case OPTION_CALLBACK: |
Pierre Habouzit | f481e22 | 2007-10-16 00:32:38 +0200 | [diff] [blame] | 405 | if (opts->flags & PARSE_OPT_NOARG) |
| 406 | break; |
| 407 | /* FALLTHROUGH */ |
| 408 | case OPTION_STRING: |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 409 | if (opts->argh) { |
| 410 | if (opts->flags & PARSE_OPT_OPTARG) |
Michele Ballabio | 6422f63 | 2008-06-22 16:39:04 +0200 | [diff] [blame] | 411 | if (opts->long_name) |
| 412 | pos += fprintf(stderr, "[=<%s>]", opts->argh); |
| 413 | else |
| 414 | pos += fprintf(stderr, "[<%s>]", opts->argh); |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 415 | else |
| 416 | pos += fprintf(stderr, " <%s>", opts->argh); |
| 417 | } else { |
| 418 | if (opts->flags & PARSE_OPT_OPTARG) |
Michele Ballabio | 6422f63 | 2008-06-22 16:39:04 +0200 | [diff] [blame] | 419 | if (opts->long_name) |
| 420 | pos += fprintf(stderr, "[=...]"); |
| 421 | else |
| 422 | pos += fprintf(stderr, "[...]"); |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 423 | else |
| 424 | pos += fprintf(stderr, " ..."); |
| 425 | } |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 426 | break; |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 427 | default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */ |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 428 | break; |
| 429 | } |
| 430 | |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 431 | if (pos <= USAGE_OPTS_WIDTH) |
| 432 | pad = USAGE_OPTS_WIDTH - pos; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 433 | else { |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 434 | fputc('\n', stderr); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 435 | pad = USAGE_OPTS_WIDTH; |
| 436 | } |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 437 | fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 438 | } |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 439 | fputc('\n', stderr); |
| 440 | |
Pierre Habouzit | ee68b87 | 2008-06-23 22:28:04 +0200 | [diff] [blame] | 441 | return PARSE_OPT_HELP; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 442 | } |
Pierre Habouzit | 0ce865b | 2007-10-14 11:05:12 +0200 | [diff] [blame] | 443 | |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 444 | void usage_with_options(const char * const *usagestr, |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 445 | const struct option *opts) |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 446 | { |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 447 | usage_with_options_internal(usagestr, opts, 0); |
| 448 | exit(129); |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 449 | } |
| 450 | |
Pierre Habouzit | ee68b87 | 2008-06-23 22:28:04 +0200 | [diff] [blame] | 451 | int parse_options_usage(const char * const *usagestr, |
| 452 | const struct option *opts) |
| 453 | { |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 454 | return usage_with_options_internal(usagestr, opts, 0); |
Pierre Habouzit | ee68b87 | 2008-06-23 22:28:04 +0200 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | |
Pierre Habouzit | 0ce865b | 2007-10-14 11:05:12 +0200 | [diff] [blame] | 458 | /*----- some often used options -----*/ |
| 459 | #include "cache.h" |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 460 | |
Pierre Habouzit | 0ce865b | 2007-10-14 11:05:12 +0200 | [diff] [blame] | 461 | int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset) |
| 462 | { |
| 463 | int v; |
| 464 | |
| 465 | if (!arg) { |
| 466 | v = unset ? 0 : DEFAULT_ABBREV; |
| 467 | } else { |
| 468 | v = strtol(arg, (char **)&arg, 10); |
| 469 | if (*arg) |
| 470 | return opterror(opt, "expects a numerical value", 0); |
| 471 | if (v && v < MINIMUM_ABBREV) |
| 472 | v = MINIMUM_ABBREV; |
| 473 | else if (v > 40) |
| 474 | v = 40; |
| 475 | } |
| 476 | *(int *)(opt->value) = v; |
| 477 | return 0; |
| 478 | } |
Michele Ballabio | 1f4a711 | 2008-03-24 15:02:21 +0100 | [diff] [blame] | 479 | |
| 480 | int parse_opt_approxidate_cb(const struct option *opt, const char *arg, |
| 481 | int unset) |
| 482 | { |
| 483 | *(unsigned long *)(opt->value) = approxidate(arg); |
| 484 | return 0; |
| 485 | } |
Junio C Hamano | dbd0f5c | 2008-08-06 11:43:47 -0700 | [diff] [blame] | 486 | |
| 487 | /* |
| 488 | * This should really be OPTION_FILENAME type as a part of |
| 489 | * parse_options that take prefix to do this while parsing. |
| 490 | */ |
| 491 | extern const char *parse_options_fix_filename(const char *prefix, const char *file) |
| 492 | { |
| 493 | if (!file || !prefix || is_absolute_path(file) || !strcmp("-", file)) |
| 494 | return file; |
| 495 | return prefix_filename(prefix, strlen(prefix), file); |
| 496 | } |
| 497 | |