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