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" |
Jake Goulding | 269defd | 2009-01-26 09:13:23 -0500 | [diff] [blame] | 4 | #include "commit.h" |
Mark Lodato | 73e9da0 | 2010-02-16 23:55:58 -0500 | [diff] [blame] | 5 | #include "color.h" |
Jeff King | c8ba163 | 2011-06-09 11:55:23 -0400 | [diff] [blame] | 6 | #include "string-list.h" |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 7 | |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 8 | static int parse_options_usage(struct parse_opt_ctx_t *ctx, |
| 9 | const char * const *usagestr, |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 10 | const struct option *opts, int err); |
Junio C Hamano | 41064eb | 2010-01-11 22:28:45 -0800 | [diff] [blame] | 11 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 12 | #define OPT_SHORT 1 |
| 13 | #define OPT_UNSET 2 |
| 14 | |
Jonathan Nieder | 1e5ce57 | 2010-12-02 00:01:18 -0600 | [diff] [blame] | 15 | static int optbug(const struct option *opt, const char *reason) |
| 16 | { |
| 17 | if (opt->long_name) |
| 18 | return error("BUG: option '%s' %s", opt->long_name, reason); |
| 19 | return error("BUG: switch '%c' %s", opt->short_name, reason); |
| 20 | } |
| 21 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 22 | static int opterror(const struct option *opt, const char *reason, int flags) |
| 23 | { |
| 24 | if (flags & OPT_SHORT) |
| 25 | return error("switch `%c' %s", opt->short_name, reason); |
| 26 | if (flags & OPT_UNSET) |
| 27 | return error("option `no-%s' %s", opt->long_name, reason); |
| 28 | return error("option `%s' %s", opt->long_name, reason); |
| 29 | } |
| 30 | |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 31 | static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt, |
| 32 | int flags, const char **arg) |
| 33 | { |
| 34 | if (p->opt) { |
| 35 | *arg = p->opt; |
| 36 | p->opt = NULL; |
| 37 | } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) { |
| 38 | *arg = (const char *)opt->defval; |
Olivier Marin | d5d745f | 2008-07-21 20:30:36 +0200 | [diff] [blame] | 39 | } else if (p->argc > 1) { |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 40 | p->argc--; |
| 41 | *arg = *++p->argv; |
| 42 | } else |
| 43 | return opterror(opt, "requires a value", flags); |
| 44 | return 0; |
| 45 | } |
| 46 | |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 47 | static void fix_filename(const char *prefix, const char **file) |
| 48 | { |
| 49 | if (!file || !*file || !prefix || is_absolute_path(*file) |
| 50 | || !strcmp("-", *file)) |
| 51 | return; |
| 52 | *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file)); |
| 53 | } |
| 54 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 55 | static int get_value(struct parse_opt_ctx_t *p, |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 56 | const struct option *opt, int flags) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 57 | { |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 58 | const char *s, *arg; |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 59 | const int unset = flags & OPT_UNSET; |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 60 | int err; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 61 | |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 62 | if (unset && p->opt) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 63 | return opterror(opt, "takes no value", flags); |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 64 | if (unset && (opt->flags & PARSE_OPT_NONEG)) |
| 65 | return opterror(opt, "isn't available", flags); |
Stephen Boyd | c1f4ec9 | 2010-12-01 17:30:40 -0600 | [diff] [blame] | 66 | if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG)) |
| 67 | return opterror(opt, "takes no value", flags); |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 68 | |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 69 | switch (opt->type) { |
Jonathan Nieder | b0b3a8b | 2010-12-01 17:32:16 -0600 | [diff] [blame] | 70 | case OPTION_LOWLEVEL_CALLBACK: |
| 71 | return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset); |
| 72 | |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 73 | case OPTION_BIT: |
| 74 | if (unset) |
| 75 | *(int *)opt->value &= ~opt->defval; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 76 | else |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 77 | *(int *)opt->value |= opt->defval; |
| 78 | return 0; |
| 79 | |
René Scharfe | 2f4b97f | 2009-05-07 21:44:17 +0200 | [diff] [blame] | 80 | case OPTION_NEGBIT: |
| 81 | if (unset) |
| 82 | *(int *)opt->value |= opt->defval; |
| 83 | else |
| 84 | *(int *)opt->value &= ~opt->defval; |
| 85 | return 0; |
| 86 | |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 87 | case OPTION_BOOLEAN: |
| 88 | *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1; |
| 89 | return 0; |
| 90 | |
| 91 | case OPTION_SET_INT: |
| 92 | *(int *)opt->value = unset ? 0 : opt->defval; |
| 93 | return 0; |
| 94 | |
| 95 | case OPTION_SET_PTR: |
| 96 | *(void **)opt->value = unset ? NULL : (void *)opt->defval; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 97 | return 0; |
| 98 | |
| 99 | case OPTION_STRING: |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 100 | if (unset) |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 101 | *(const char **)opt->value = NULL; |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 102 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 103 | *(const char **)opt->value = (const char *)opt->defval; |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 104 | else |
| 105 | return get_arg(p, opt, flags, (const char **)opt->value); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 106 | return 0; |
| 107 | |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 108 | case OPTION_FILENAME: |
| 109 | err = 0; |
| 110 | if (unset) |
| 111 | *(const char **)opt->value = NULL; |
| 112 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
| 113 | *(const char **)opt->value = (const char *)opt->defval; |
| 114 | else |
| 115 | err = get_arg(p, opt, flags, (const char **)opt->value); |
| 116 | |
| 117 | if (!err) |
| 118 | fix_filename(p->prefix, (const char **)opt->value); |
| 119 | return err; |
| 120 | |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 121 | case OPTION_CALLBACK: |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 122 | if (unset) |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 123 | return (*opt->callback)(opt, NULL, 1) ? (-1) : 0; |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 124 | if (opt->flags & PARSE_OPT_NOARG) |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 125 | return (*opt->callback)(opt, NULL, 0) ? (-1) : 0; |
Pierre Habouzit | c43a248 | 2007-12-21 11:41:41 +0100 | [diff] [blame] | 126 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 127 | return (*opt->callback)(opt, NULL, 0) ? (-1) : 0; |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 128 | if (get_arg(p, opt, flags, &arg)) |
| 129 | return -1; |
| 130 | return (*opt->callback)(opt, arg, 0) ? (-1) : 0; |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 131 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 132 | case OPTION_INTEGER: |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 133 | if (unset) { |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 134 | *(int *)opt->value = 0; |
| 135 | return 0; |
| 136 | } |
Pierre Habouzit | c43a248 | 2007-12-21 11:41:41 +0100 | [diff] [blame] | 137 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 138 | *(int *)opt->value = opt->defval; |
| 139 | return 0; |
| 140 | } |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 141 | if (get_arg(p, opt, flags, &arg)) |
| 142 | return -1; |
| 143 | *(int *)opt->value = strtol(arg, (char **)&s, 10); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 144 | if (*s) |
| 145 | return opterror(opt, "expects a numerical value", flags); |
| 146 | return 0; |
| 147 | |
| 148 | default: |
| 149 | die("should not happen, someone must be hit on the forehead"); |
| 150 | } |
| 151 | } |
| 152 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 153 | 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] | 154 | { |
René Scharfe | e0319ff | 2009-05-07 21:45:08 +0200 | [diff] [blame] | 155 | const struct option *numopt = NULL; |
| 156 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 157 | for (; options->type != OPTION_END; options++) { |
| 158 | if (options->short_name == *p->opt) { |
| 159 | p->opt = p->opt[1] ? p->opt + 1 : NULL; |
| 160 | return get_value(p, options, OPT_SHORT); |
| 161 | } |
René Scharfe | e0319ff | 2009-05-07 21:45:08 +0200 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * Handle the numerical option later, explicit one-digit |
| 165 | * options take precedence over it. |
| 166 | */ |
| 167 | if (options->type == OPTION_NUMBER) |
| 168 | numopt = options; |
| 169 | } |
| 170 | if (numopt && isdigit(*p->opt)) { |
| 171 | size_t len = 1; |
| 172 | char *arg; |
| 173 | int rc; |
| 174 | |
| 175 | while (isdigit(p->opt[len])) |
| 176 | len++; |
| 177 | arg = xmemdupz(p->opt, len); |
| 178 | p->opt = p->opt[len] ? p->opt + len : NULL; |
| 179 | rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0; |
| 180 | free(arg); |
| 181 | return rc; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 182 | } |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 183 | return -2; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 186 | 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] | 187 | const struct option *options) |
| 188 | { |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 189 | const char *arg_end = strchr(arg, '='); |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 190 | const struct option *abbrev_option = NULL, *ambiguous_option = NULL; |
| 191 | int abbrev_flags = 0, ambiguous_flags = 0; |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 192 | |
| 193 | if (!arg_end) |
| 194 | arg_end = arg + strlen(arg); |
| 195 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 196 | for (; options->type != OPTION_END; options++) { |
| 197 | const char *rest; |
| 198 | int flags = 0; |
| 199 | |
| 200 | if (!options->long_name) |
| 201 | continue; |
| 202 | |
| 203 | rest = skip_prefix(arg, options->long_name); |
Pierre Habouzit | 580d5bf | 2008-03-02 11:35:56 +0100 | [diff] [blame] | 204 | if (options->type == OPTION_ARGUMENT) { |
| 205 | if (!rest) |
| 206 | continue; |
| 207 | if (*rest == '=') |
| 208 | return opterror(options, "takes no value", flags); |
| 209 | if (*rest) |
| 210 | continue; |
| 211 | p->out[p->cpidx++] = arg - 2; |
| 212 | return 0; |
| 213 | } |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 214 | if (!rest) { |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 215 | /* abbreviated? */ |
| 216 | if (!strncmp(options->long_name, arg, arg_end - arg)) { |
| 217 | is_abbreviated: |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 218 | if (abbrev_option) { |
| 219 | /* |
| 220 | * If this is abbreviated, it is |
| 221 | * ambiguous. So when there is no |
| 222 | * exact match later, we need to |
| 223 | * error out. |
| 224 | */ |
| 225 | ambiguous_option = abbrev_option; |
| 226 | ambiguous_flags = abbrev_flags; |
| 227 | } |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 228 | if (!(flags & OPT_UNSET) && *arg_end) |
| 229 | p->opt = arg_end + 1; |
| 230 | abbrev_option = options; |
| 231 | abbrev_flags = flags; |
| 232 | continue; |
| 233 | } |
Andreas Schwab | 6bbfd1f | 2009-09-25 20:44:44 +0200 | [diff] [blame] | 234 | /* negation allowed? */ |
| 235 | if (options->flags & PARSE_OPT_NONEG) |
| 236 | continue; |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 237 | /* negated and abbreviated very much? */ |
| 238 | if (!prefixcmp("no-", arg)) { |
| 239 | flags |= OPT_UNSET; |
| 240 | goto is_abbreviated; |
| 241 | } |
| 242 | /* negated? */ |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 243 | if (strncmp(arg, "no-", 3)) |
| 244 | continue; |
| 245 | flags |= OPT_UNSET; |
| 246 | rest = skip_prefix(arg + 3, options->long_name); |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 247 | /* abbreviated and negated? */ |
| 248 | if (!rest && !prefixcmp(options->long_name, arg + 3)) |
| 249 | goto is_abbreviated; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 250 | if (!rest) |
| 251 | continue; |
| 252 | } |
| 253 | if (*rest) { |
| 254 | if (*rest != '=') |
| 255 | continue; |
| 256 | p->opt = rest + 1; |
| 257 | } |
| 258 | return get_value(p, options, flags); |
| 259 | } |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 260 | |
| 261 | if (ambiguous_option) |
| 262 | return error("Ambiguous option: %s " |
| 263 | "(could be --%s%s or --%s%s)", |
| 264 | arg, |
| 265 | (ambiguous_flags & OPT_UNSET) ? "no-" : "", |
| 266 | ambiguous_option->long_name, |
| 267 | (abbrev_flags & OPT_UNSET) ? "no-" : "", |
| 268 | abbrev_option->long_name); |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 269 | if (abbrev_option) |
| 270 | return get_value(p, abbrev_option, abbrev_flags); |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 271 | return -2; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 272 | } |
| 273 | |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 274 | static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg, |
| 275 | const struct option *options) |
| 276 | { |
| 277 | for (; options->type != OPTION_END; options++) { |
| 278 | if (!(options->flags & PARSE_OPT_NODASH)) |
| 279 | continue; |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 280 | if (options->short_name == arg[0] && arg[1] == '\0') |
| 281 | return get_value(p, options, OPT_SHORT); |
| 282 | } |
| 283 | return -2; |
| 284 | } |
| 285 | |
Nanako Shiraishi | 1121a87 | 2008-07-16 19:42:18 +0900 | [diff] [blame] | 286 | static void check_typos(const char *arg, const struct option *options) |
Pierre Habouzit | 3a9f0f4 | 2008-01-26 12:26:57 +0100 | [diff] [blame] | 287 | { |
| 288 | if (strlen(arg) < 3) |
| 289 | return; |
| 290 | |
| 291 | if (!prefixcmp(arg, "no-")) { |
| 292 | error ("did you mean `--%s` (with two dashes ?)", arg); |
| 293 | exit(129); |
| 294 | } |
| 295 | |
| 296 | for (; options->type != OPTION_END; options++) { |
| 297 | if (!options->long_name) |
| 298 | continue; |
| 299 | if (!prefixcmp(options->long_name, arg)) { |
| 300 | error ("did you mean `--%s` (with two dashes ?)", arg); |
| 301 | exit(129); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
Pierre Habouzit | cb9d398 | 2009-06-09 10:23:44 +0200 | [diff] [blame] | 306 | static void parse_options_check(const struct option *opts) |
| 307 | { |
| 308 | int err = 0; |
| 309 | |
| 310 | for (; opts->type != OPTION_END; opts++) { |
| 311 | if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) && |
Jonathan Nieder | 1e5ce57 | 2010-12-02 00:01:18 -0600 | [diff] [blame] | 312 | (opts->flags & PARSE_OPT_OPTARG)) |
| 313 | err |= optbug(opts, "uses incompatible flags " |
| 314 | "LASTARG_DEFAULT and OPTARG"); |
Jonathan Nieder | a02dd4f | 2010-12-02 00:05:05 -0600 | [diff] [blame] | 315 | if (opts->flags & PARSE_OPT_NODASH && |
| 316 | ((opts->flags & PARSE_OPT_OPTARG) || |
| 317 | !(opts->flags & PARSE_OPT_NOARG) || |
| 318 | !(opts->flags & PARSE_OPT_NONEG) || |
| 319 | opts->long_name)) |
| 320 | err |= optbug(opts, "uses feature " |
| 321 | "not supported for dashless options"); |
Jonathan Nieder | 5c400ed | 2010-12-02 00:08:57 -0600 | [diff] [blame] | 322 | switch (opts->type) { |
| 323 | case OPTION_BOOLEAN: |
| 324 | case OPTION_BIT: |
| 325 | case OPTION_NEGBIT: |
| 326 | case OPTION_SET_INT: |
| 327 | case OPTION_SET_PTR: |
| 328 | case OPTION_NUMBER: |
| 329 | if ((opts->flags & PARSE_OPT_OPTARG) || |
| 330 | !(opts->flags & PARSE_OPT_NOARG)) |
| 331 | err |= optbug(opts, "should not accept an argument"); |
| 332 | default: |
| 333 | ; /* ok. (usually accepts an argument) */ |
| 334 | } |
Pierre Habouzit | cb9d398 | 2009-06-09 10:23:44 +0200 | [diff] [blame] | 335 | } |
Pierre Habouzit | cb9d398 | 2009-06-09 10:23:44 +0200 | [diff] [blame] | 336 | if (err) |
Jonathan Nieder | 1e5ce57 | 2010-12-02 00:01:18 -0600 | [diff] [blame] | 337 | exit(128); |
Pierre Habouzit | cb9d398 | 2009-06-09 10:23:44 +0200 | [diff] [blame] | 338 | } |
| 339 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 340 | void parse_options_start(struct parse_opt_ctx_t *ctx, |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 341 | int argc, const char **argv, const char *prefix, |
Stephen Boyd | 9ca1169 | 2010-12-05 23:57:42 -0800 | [diff] [blame] | 342 | const struct option *options, int flags) |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 343 | { |
| 344 | memset(ctx, 0, sizeof(*ctx)); |
| 345 | ctx->argc = argc - 1; |
| 346 | ctx->argv = argv + 1; |
| 347 | ctx->out = argv; |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 348 | ctx->prefix = prefix; |
Pierre Habouzit | a32a4ea | 2008-06-24 00:31:31 +0200 | [diff] [blame] | 349 | ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0); |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 350 | ctx->flags = flags; |
René Scharfe | 0d260f9 | 2009-03-09 21:57:38 +0100 | [diff] [blame] | 351 | if ((flags & PARSE_OPT_KEEP_UNKNOWN) && |
| 352 | (flags & PARSE_OPT_STOP_AT_NON_OPTION)) |
| 353 | die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together"); |
Stephen Boyd | 9ca1169 | 2010-12-05 23:57:42 -0800 | [diff] [blame] | 354 | parse_options_check(options); |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 355 | } |
| 356 | |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 357 | static int usage_with_options_internal(struct parse_opt_ctx_t *, |
| 358 | const char * const *, |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 359 | const struct option *, int, int); |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 360 | |
| 361 | int parse_options_step(struct parse_opt_ctx_t *ctx, |
| 362 | const struct option *options, |
| 363 | const char * const usagestr[]) |
| 364 | { |
René Scharfe | b92891f | 2009-03-08 19:15:08 +0100 | [diff] [blame] | 365 | int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP); |
| 366 | |
Pierre Habouzit | 26141b5 | 2008-06-23 22:55:11 +0200 | [diff] [blame] | 367 | /* we must reset ->opt, unknown short option leave it dangling */ |
| 368 | ctx->opt = NULL; |
| 369 | |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 370 | for (; ctx->argc; ctx->argc--, ctx->argv++) { |
| 371 | const char *arg = ctx->argv[0]; |
| 372 | |
| 373 | if (*arg != '-' || !arg[1]) { |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 374 | if (parse_nodash_opt(ctx, arg, options) == 0) |
| 375 | continue; |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 376 | if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION) |
Jonathan Nieder | 979240f | 2010-12-01 17:32:55 -0600 | [diff] [blame] | 377 | return PARSE_OPT_NON_OPTION; |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 378 | ctx->out[ctx->cpidx++] = ctx->argv[0]; |
| 379 | continue; |
| 380 | } |
| 381 | |
| 382 | if (arg[1] != '-') { |
| 383 | ctx->opt = arg + 1; |
René Scharfe | b92891f | 2009-03-08 19:15:08 +0100 | [diff] [blame] | 384 | if (internal_help && *ctx->opt == 'h') |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 385 | return parse_options_usage(ctx, usagestr, options, 0); |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 386 | switch (parse_short_opt(ctx, options)) { |
| 387 | case -1: |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 388 | return parse_options_usage(ctx, usagestr, options, 1); |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 389 | case -2: |
René Scharfe | b5ce3a5 | 2009-03-08 19:12:47 +0100 | [diff] [blame] | 390 | goto unknown; |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 391 | } |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 392 | if (ctx->opt) |
| 393 | check_typos(arg + 1, options); |
| 394 | while (ctx->opt) { |
René Scharfe | b92891f | 2009-03-08 19:15:08 +0100 | [diff] [blame] | 395 | if (internal_help && *ctx->opt == 'h') |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 396 | return parse_options_usage(ctx, usagestr, options, 0); |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 397 | switch (parse_short_opt(ctx, options)) { |
| 398 | case -1: |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 399 | return parse_options_usage(ctx, usagestr, options, 1); |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 400 | case -2: |
Pierre Habouzit | 26141b5 | 2008-06-23 22:55:11 +0200 | [diff] [blame] | 401 | /* fake a short option thing to hide the fact that we may have |
| 402 | * started to parse aggregated stuff |
| 403 | * |
| 404 | * This is leaky, too bad. |
| 405 | */ |
| 406 | ctx->argv[0] = xstrdup(ctx->opt - 1); |
| 407 | *(char *)ctx->argv[0] = '-'; |
René Scharfe | b5ce3a5 | 2009-03-08 19:12:47 +0100 | [diff] [blame] | 408 | goto unknown; |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 409 | } |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 410 | } |
| 411 | continue; |
| 412 | } |
| 413 | |
| 414 | if (!arg[2]) { /* "--" */ |
| 415 | if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) { |
| 416 | ctx->argc--; |
| 417 | ctx->argv++; |
| 418 | } |
| 419 | break; |
| 420 | } |
| 421 | |
René Scharfe | b92891f | 2009-03-08 19:15:08 +0100 | [diff] [blame] | 422 | if (internal_help && !strcmp(arg + 2, "help-all")) |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 423 | return usage_with_options_internal(ctx, usagestr, options, 1, 0); |
René Scharfe | b92891f | 2009-03-08 19:15:08 +0100 | [diff] [blame] | 424 | if (internal_help && !strcmp(arg + 2, "help")) |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 425 | return parse_options_usage(ctx, usagestr, options, 0); |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 426 | switch (parse_long_opt(ctx, arg + 2, options)) { |
| 427 | case -1: |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 428 | return parse_options_usage(ctx, usagestr, options, 1); |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 429 | case -2: |
René Scharfe | b5ce3a5 | 2009-03-08 19:12:47 +0100 | [diff] [blame] | 430 | goto unknown; |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 431 | } |
René Scharfe | b5ce3a5 | 2009-03-08 19:12:47 +0100 | [diff] [blame] | 432 | continue; |
| 433 | unknown: |
| 434 | if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN)) |
| 435 | return PARSE_OPT_UNKNOWN; |
| 436 | ctx->out[ctx->cpidx++] = ctx->argv[0]; |
| 437 | ctx->opt = NULL; |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 438 | } |
| 439 | return PARSE_OPT_DONE; |
| 440 | } |
| 441 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 442 | int parse_options_end(struct parse_opt_ctx_t *ctx) |
| 443 | { |
| 444 | memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out)); |
| 445 | ctx->out[ctx->cpidx + ctx->argc] = NULL; |
| 446 | return ctx->cpidx + ctx->argc; |
| 447 | } |
| 448 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 449 | int parse_options(int argc, const char **argv, const char *prefix, |
| 450 | const struct option *options, const char * const usagestr[], |
| 451 | int flags) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 452 | { |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 453 | struct parse_opt_ctx_t ctx; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 454 | |
Stephen Boyd | 9ca1169 | 2010-12-05 23:57:42 -0800 | [diff] [blame] | 455 | parse_options_start(&ctx, argc, argv, prefix, options, flags); |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 456 | switch (parse_options_step(&ctx, options, usagestr)) { |
| 457 | case PARSE_OPT_HELP: |
| 458 | exit(129); |
Jonathan Nieder | 979240f | 2010-12-01 17:32:55 -0600 | [diff] [blame] | 459 | case PARSE_OPT_NON_OPTION: |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 460 | case PARSE_OPT_DONE: |
| 461 | break; |
| 462 | default: /* PARSE_OPT_UNKNOWN */ |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 463 | if (ctx.argv[0][1] == '-') { |
| 464 | error("unknown option `%s'", ctx.argv[0] + 2); |
| 465 | } else { |
| 466 | error("unknown switch `%c'", *ctx.opt); |
| 467 | } |
| 468 | usage_with_options(usagestr, options); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 471 | return parse_options_end(&ctx); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 472 | } |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 473 | |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 474 | static int usage_argh(const struct option *opts, FILE *outfile) |
Stephen Boyd | 29f25d4 | 2009-05-21 00:33:17 -0700 | [diff] [blame] | 475 | { |
| 476 | const char *s; |
Stephen Boyd | 34aec9f | 2009-06-04 16:43:57 -0700 | [diff] [blame] | 477 | int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh; |
Stephen Boyd | 29f25d4 | 2009-05-21 00:33:17 -0700 | [diff] [blame] | 478 | if (opts->flags & PARSE_OPT_OPTARG) |
| 479 | if (opts->long_name) |
| 480 | s = literal ? "[=%s]" : "[=<%s>]"; |
| 481 | else |
| 482 | s = literal ? "[%s]" : "[<%s>]"; |
| 483 | else |
| 484 | s = literal ? " %s" : " <%s>"; |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 485 | return fprintf(outfile, s, opts->argh ? opts->argh : "..."); |
Stephen Boyd | 29f25d4 | 2009-05-21 00:33:17 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 488 | #define USAGE_OPTS_WIDTH 24 |
| 489 | #define USAGE_GAP 2 |
| 490 | |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 491 | static int usage_with_options_internal(struct parse_opt_ctx_t *ctx, |
| 492 | const char * const *usagestr, |
| 493 | const struct option *opts, int full, int err) |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 494 | { |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 495 | FILE *outfile = err ? stderr : stdout; |
| 496 | |
René Scharfe | 49b6180 | 2009-03-08 19:16:58 +0100 | [diff] [blame] | 497 | if (!usagestr) |
| 498 | return PARSE_OPT_HELP; |
| 499 | |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 500 | if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) |
| 501 | fprintf(outfile, "cat <<\\EOF\n"); |
| 502 | |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 503 | fprintf(outfile, "usage: %s\n", *usagestr++); |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 504 | while (*usagestr && **usagestr) |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 505 | fprintf(outfile, " or: %s\n", *usagestr++); |
Jeff King | 44d86e9 | 2008-06-14 03:27:21 -0400 | [diff] [blame] | 506 | while (*usagestr) { |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 507 | fprintf(outfile, "%s%s\n", |
Jeff King | 44d86e9 | 2008-06-14 03:27:21 -0400 | [diff] [blame] | 508 | **usagestr ? " " : "", |
| 509 | *usagestr); |
| 510 | usagestr++; |
| 511 | } |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 512 | |
| 513 | if (opts->type != OPTION_GROUP) |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 514 | fputc('\n', outfile); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 515 | |
| 516 | for (; opts->type != OPTION_END; opts++) { |
| 517 | size_t pos; |
| 518 | int pad; |
| 519 | |
| 520 | if (opts->type == OPTION_GROUP) { |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 521 | fputc('\n', outfile); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 522 | if (*opts->help) |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 523 | fprintf(outfile, "%s\n", opts->help); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 524 | continue; |
| 525 | } |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 526 | if (!full && (opts->flags & PARSE_OPT_HIDDEN)) |
| 527 | continue; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 528 | |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 529 | pos = fprintf(outfile, " "); |
Johannes Schindelin | 86b5efb | 2009-07-27 20:49:56 +0200 | [diff] [blame] | 530 | if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) { |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 531 | if (opts->flags & PARSE_OPT_NODASH) |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 532 | pos += fprintf(outfile, "%c", opts->short_name); |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 533 | else |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 534 | pos += fprintf(outfile, "-%c", opts->short_name); |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 535 | } |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 536 | if (opts->long_name && opts->short_name) |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 537 | pos += fprintf(outfile, ", "); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 538 | if (opts->long_name) |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 539 | pos += fprintf(outfile, "--%s%s", |
Johannes Schindelin | 86b5efb | 2009-07-27 20:49:56 +0200 | [diff] [blame] | 540 | (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "", |
| 541 | opts->long_name); |
René Scharfe | e0319ff | 2009-05-07 21:45:08 +0200 | [diff] [blame] | 542 | if (opts->type == OPTION_NUMBER) |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 543 | pos += fprintf(outfile, "-NUM"); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 544 | |
Jonathan Nieder | b57c68a | 2010-12-01 17:31:36 -0600 | [diff] [blame] | 545 | if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) || |
| 546 | !(opts->flags & PARSE_OPT_NOARG)) |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 547 | pos += usage_argh(opts, outfile); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 548 | |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 549 | if (pos <= USAGE_OPTS_WIDTH) |
| 550 | pad = USAGE_OPTS_WIDTH - pos; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 551 | else { |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 552 | fputc('\n', outfile); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 553 | pad = USAGE_OPTS_WIDTH; |
| 554 | } |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 555 | fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 556 | } |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 557 | fputc('\n', outfile); |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 558 | |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 559 | if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) |
| 560 | fputs("EOF\n", outfile); |
| 561 | |
Pierre Habouzit | ee68b87 | 2008-06-23 22:28:04 +0200 | [diff] [blame] | 562 | return PARSE_OPT_HELP; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 563 | } |
Pierre Habouzit | 0ce865b | 2007-10-14 11:05:12 +0200 | [diff] [blame] | 564 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 565 | void NORETURN usage_with_options(const char * const *usagestr, |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 566 | const struct option *opts) |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 567 | { |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 568 | usage_with_options_internal(NULL, usagestr, opts, 0, 1); |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 569 | exit(129); |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 572 | void NORETURN usage_msg_opt(const char *msg, |
Christian Couder | 451bb21 | 2009-02-02 06:12:58 +0100 | [diff] [blame] | 573 | const char * const *usagestr, |
| 574 | const struct option *options) |
| 575 | { |
| 576 | fprintf(stderr, "%s\n\n", msg); |
| 577 | usage_with_options(usagestr, options); |
| 578 | } |
| 579 | |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 580 | static int parse_options_usage(struct parse_opt_ctx_t *ctx, |
| 581 | const char * const *usagestr, |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 582 | const struct option *opts, int err) |
Pierre Habouzit | ee68b87 | 2008-06-23 22:28:04 +0200 | [diff] [blame] | 583 | { |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 584 | return usage_with_options_internal(ctx, usagestr, opts, 0, err); |
Pierre Habouzit | ee68b87 | 2008-06-23 22:28:04 +0200 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | |
Pierre Habouzit | 0ce865b | 2007-10-14 11:05:12 +0200 | [diff] [blame] | 588 | /*----- some often used options -----*/ |
| 589 | #include "cache.h" |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 590 | |
Pierre Habouzit | 0ce865b | 2007-10-14 11:05:12 +0200 | [diff] [blame] | 591 | int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset) |
| 592 | { |
| 593 | int v; |
| 594 | |
| 595 | if (!arg) { |
| 596 | v = unset ? 0 : DEFAULT_ABBREV; |
| 597 | } else { |
| 598 | v = strtol(arg, (char **)&arg, 10); |
| 599 | if (*arg) |
| 600 | return opterror(opt, "expects a numerical value", 0); |
| 601 | if (v && v < MINIMUM_ABBREV) |
| 602 | v = MINIMUM_ABBREV; |
| 603 | else if (v > 40) |
| 604 | v = 40; |
| 605 | } |
| 606 | *(int *)(opt->value) = v; |
| 607 | return 0; |
| 608 | } |
Michele Ballabio | 1f4a711 | 2008-03-24 15:02:21 +0100 | [diff] [blame] | 609 | |
| 610 | int parse_opt_approxidate_cb(const struct option *opt, const char *arg, |
| 611 | int unset) |
| 612 | { |
| 613 | *(unsigned long *)(opt->value) = approxidate(arg); |
| 614 | return 0; |
| 615 | } |
Junio C Hamano | dbd0f5c | 2008-08-06 11:43:47 -0700 | [diff] [blame] | 616 | |
Mark Lodato | 73e9da0 | 2010-02-16 23:55:58 -0500 | [diff] [blame] | 617 | int parse_opt_color_flag_cb(const struct option *opt, const char *arg, |
| 618 | int unset) |
| 619 | { |
| 620 | int value; |
| 621 | |
| 622 | if (!arg) |
| 623 | arg = unset ? "never" : (const char *)opt->defval; |
| 624 | value = git_config_colorbool(NULL, arg, -1); |
| 625 | if (value < 0) |
| 626 | return opterror(opt, |
| 627 | "expects \"always\", \"auto\", or \"never\"", 0); |
| 628 | *(int *)opt->value = value; |
| 629 | return 0; |
| 630 | } |
| 631 | |
Tuncer Ayaz | 7f87aff | 2008-11-15 01:14:24 +0100 | [diff] [blame] | 632 | int parse_opt_verbosity_cb(const struct option *opt, const char *arg, |
| 633 | int unset) |
| 634 | { |
| 635 | int *target = opt->value; |
| 636 | |
| 637 | if (unset) |
| 638 | /* --no-quiet, --no-verbose */ |
| 639 | *target = 0; |
| 640 | else if (opt->short_name == 'v') { |
| 641 | if (*target >= 0) |
| 642 | (*target)++; |
| 643 | else |
| 644 | *target = 1; |
| 645 | } else { |
| 646 | if (*target <= 0) |
| 647 | (*target)--; |
| 648 | else |
| 649 | *target = -1; |
| 650 | } |
| 651 | return 0; |
| 652 | } |
| 653 | |
Jake Goulding | 269defd | 2009-01-26 09:13:23 -0500 | [diff] [blame] | 654 | int parse_opt_with_commit(const struct option *opt, const char *arg, int unset) |
| 655 | { |
| 656 | unsigned char sha1[20]; |
| 657 | struct commit *commit; |
| 658 | |
| 659 | if (!arg) |
| 660 | return -1; |
| 661 | if (get_sha1(arg, sha1)) |
| 662 | return error("malformed object name %s", arg); |
| 663 | commit = lookup_commit_reference(sha1); |
| 664 | if (!commit) |
| 665 | return error("no such commit %s", arg); |
| 666 | commit_list_insert(commit, opt->value); |
| 667 | return 0; |
| 668 | } |
Junio C Hamano | cb6020b | 2009-12-04 00:20:48 -0800 | [diff] [blame] | 669 | |
| 670 | int parse_opt_tertiary(const struct option *opt, const char *arg, int unset) |
| 671 | { |
| 672 | int *target = opt->value; |
| 673 | *target = unset ? 2 : 1; |
| 674 | return 0; |
| 675 | } |
Junio C Hamano | 8b74d75 | 2010-03-06 21:34:40 +0100 | [diff] [blame] | 676 | |
| 677 | int parse_options_concat(struct option *dst, size_t dst_size, struct option *src) |
| 678 | { |
| 679 | int i, j; |
| 680 | |
| 681 | for (i = 0; i < dst_size; i++) |
| 682 | if (dst[i].type == OPTION_END) |
| 683 | break; |
| 684 | for (j = 0; i < dst_size; i++, j++) { |
| 685 | dst[i] = src[j]; |
| 686 | if (src[j].type == OPTION_END) |
| 687 | return 0; |
| 688 | } |
| 689 | return -1; |
| 690 | } |
Jeff King | c8ba163 | 2011-06-09 11:55:23 -0400 | [diff] [blame] | 691 | |
| 692 | int parse_opt_string_list(const struct option *opt, const char *arg, int unset) |
| 693 | { |
| 694 | struct string_list *v = opt->value; |
| 695 | |
| 696 | if (unset) { |
| 697 | string_list_clear(v, 0); |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | if (!arg) |
| 702 | return -1; |
| 703 | |
| 704 | string_list_append(v, xstrdup(arg)); |
| 705 | return 0; |
| 706 | } |