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