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 | |
Johannes Schindelin | b02e7d5 | 2019-04-12 02:37:24 -0700 | [diff] [blame] | 9 | static int disallow_abbreviated_options; |
| 10 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 11 | #define OPT_SHORT 1 |
| 12 | #define OPT_UNSET 2 |
| 13 | |
Dmitry Ivankov | 1f275b7 | 2011-08-11 15:15:37 +0600 | [diff] [blame] | 14 | int optbug(const struct option *opt, const char *reason) |
Jonathan Nieder | 1e5ce57 | 2010-12-02 00:01:18 -0600 | [diff] [blame] | 15 | { |
Junio C Hamano | af465af | 2014-09-03 12:42:37 -0700 | [diff] [blame] | 16 | if (opt->long_name) { |
| 17 | if (opt->short_name) |
| 18 | return error("BUG: switch '%c' (--%s) %s", |
| 19 | opt->short_name, opt->long_name, reason); |
Jonathan Nieder | 1e5ce57 | 2010-12-02 00:01:18 -0600 | [diff] [blame] | 20 | return error("BUG: option '%s' %s", opt->long_name, reason); |
Junio C Hamano | af465af | 2014-09-03 12:42:37 -0700 | [diff] [blame] | 21 | } |
Jonathan Nieder | 1e5ce57 | 2010-12-02 00:01:18 -0600 | [diff] [blame] | 22 | return error("BUG: switch '%c' %s", opt->short_name, reason); |
| 23 | } |
| 24 | |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 25 | static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p, |
| 26 | const struct option *opt, |
| 27 | int flags, const char **arg) |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 28 | { |
| 29 | if (p->opt) { |
| 30 | *arg = p->opt; |
| 31 | p->opt = NULL; |
| 32 | } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) { |
| 33 | *arg = (const char *)opt->defval; |
Olivier Marin | d5d745f | 2008-07-21 20:30:36 +0200 | [diff] [blame] | 34 | } else if (p->argc > 1) { |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 35 | p->argc--; |
| 36 | *arg = *++p->argv; |
| 37 | } else |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 38 | return error(_("%s requires a value"), optname(opt, flags)); |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 39 | return 0; |
| 40 | } |
| 41 | |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 42 | static void fix_filename(const char *prefix, const char **file) |
| 43 | { |
| 44 | if (!file || !*file || !prefix || is_absolute_path(*file) |
| 45 | || !strcmp("-", *file)) |
| 46 | return; |
Jeff King | e4da43b | 2017-03-20 21:28:49 -0400 | [diff] [blame] | 47 | *file = prefix_filename(prefix, *file); |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 50 | static enum parse_opt_result opt_command_mode_error( |
| 51 | const struct option *opt, |
| 52 | const struct option *all_opts, |
| 53 | int flags) |
Junio C Hamano | 1158826 | 2013-07-30 12:06:01 -0700 | [diff] [blame] | 54 | { |
| 55 | const struct option *that; |
Junio C Hamano | 1158826 | 2013-07-30 12:06:01 -0700 | [diff] [blame] | 56 | struct strbuf that_name = STRBUF_INIT; |
| 57 | |
| 58 | /* |
| 59 | * Find the other option that was used to set the variable |
| 60 | * already, and report that this is not compatible with it. |
| 61 | */ |
| 62 | for (that = all_opts; that->type != OPTION_END; that++) { |
| 63 | if (that == opt || |
Paolo Bonzini | bc8620b | 2020-02-20 15:15:16 +0100 | [diff] [blame] | 64 | !(that->flags & PARSE_OPT_CMDMODE) || |
Junio C Hamano | 1158826 | 2013-07-30 12:06:01 -0700 | [diff] [blame] | 65 | that->value != opt->value || |
| 66 | that->defval != *(int *)opt->value) |
| 67 | continue; |
| 68 | |
| 69 | if (that->long_name) |
| 70 | strbuf_addf(&that_name, "--%s", that->long_name); |
| 71 | else |
| 72 | strbuf_addf(&that_name, "-%c", that->short_name); |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 73 | error(_("%s is incompatible with %s"), |
| 74 | optname(opt, flags), that_name.buf); |
Junio C Hamano | 1158826 | 2013-07-30 12:06:01 -0700 | [diff] [blame] | 75 | strbuf_release(&that_name); |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 76 | return PARSE_OPT_ERROR; |
Junio C Hamano | 1158826 | 2013-07-30 12:06:01 -0700 | [diff] [blame] | 77 | } |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 78 | return error(_("%s : incompatible with something else"), |
| 79 | optname(opt, flags)); |
Junio C Hamano | 1158826 | 2013-07-30 12:06:01 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 82 | static enum parse_opt_result get_value(struct parse_opt_ctx_t *p, |
| 83 | const struct option *opt, |
| 84 | const struct option *all_opts, |
| 85 | int flags) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 86 | { |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 87 | const char *s, *arg; |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 88 | const int unset = flags & OPT_UNSET; |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 89 | int err; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 90 | |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 91 | if (unset && p->opt) |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 92 | return error(_("%s takes no value"), optname(opt, flags)); |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 93 | if (unset && (opt->flags & PARSE_OPT_NONEG)) |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 94 | return error(_("%s isn't available"), optname(opt, flags)); |
Stephen Boyd | c1f4ec9 | 2010-12-01 17:30:40 -0600 | [diff] [blame] | 95 | if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG)) |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 96 | return error(_("%s takes no value"), optname(opt, flags)); |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 97 | |
Paolo Bonzini | bc8620b | 2020-02-20 15:15:16 +0100 | [diff] [blame] | 98 | /* |
| 99 | * Giving the same mode option twice, although unnecessary, |
| 100 | * is not a grave error, so let it pass. |
| 101 | */ |
| 102 | if ((opt->flags & PARSE_OPT_CMDMODE) && |
| 103 | *(int *)opt->value && *(int *)opt->value != opt->defval) |
| 104 | return opt_command_mode_error(opt, all_opts, flags); |
| 105 | |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 106 | switch (opt->type) { |
Jonathan Nieder | b0b3a8b | 2010-12-01 17:32:16 -0600 | [diff] [blame] | 107 | case OPTION_LOWLEVEL_CALLBACK: |
Nguyễn Thái Ngọc Duy | 3ebbe28 | 2019-01-27 07:35:28 +0700 | [diff] [blame] | 108 | return opt->ll_callback(p, opt, NULL, unset); |
Jonathan Nieder | b0b3a8b | 2010-12-01 17:32:16 -0600 | [diff] [blame] | 109 | |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 110 | case OPTION_BIT: |
| 111 | if (unset) |
| 112 | *(int *)opt->value &= ~opt->defval; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 113 | else |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 114 | *(int *)opt->value |= opt->defval; |
| 115 | return 0; |
| 116 | |
René Scharfe | 2f4b97f | 2009-05-07 21:44:17 +0200 | [diff] [blame] | 117 | case OPTION_NEGBIT: |
| 118 | if (unset) |
| 119 | *(int *)opt->value |= opt->defval; |
| 120 | else |
| 121 | *(int *)opt->value &= ~opt->defval; |
| 122 | return 0; |
| 123 | |
Nguyễn Thái Ngọc Duy | f62470c | 2019-01-27 07:35:25 +0700 | [diff] [blame] | 124 | case OPTION_BITOP: |
| 125 | if (unset) |
| 126 | BUG("BITOP can't have unset form"); |
| 127 | *(int *)opt->value &= ~opt->extra; |
| 128 | *(int *)opt->value |= opt->defval; |
| 129 | return 0; |
| 130 | |
Junio C Hamano | b04ba2b | 2011-09-27 16:56:49 -0700 | [diff] [blame] | 131 | case OPTION_COUNTUP: |
Pranit Bauva | e0070e8 | 2016-05-05 15:20:00 +0530 | [diff] [blame] | 132 | if (*(int *)opt->value < 0) |
| 133 | *(int *)opt->value = 0; |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 134 | *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1; |
| 135 | return 0; |
| 136 | |
| 137 | case OPTION_SET_INT: |
| 138 | *(int *)opt->value = unset ? 0 : opt->defval; |
| 139 | return 0; |
| 140 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 141 | case OPTION_STRING: |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 142 | if (unset) |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 143 | *(const char **)opt->value = NULL; |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 144 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 145 | *(const char **)opt->value = (const char *)opt->defval; |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 146 | else |
| 147 | return get_arg(p, opt, flags, (const char **)opt->value); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 148 | return 0; |
| 149 | |
Stephen Boyd | df217ed | 2009-05-23 11:53:13 -0700 | [diff] [blame] | 150 | case OPTION_FILENAME: |
| 151 | err = 0; |
| 152 | if (unset) |
| 153 | *(const char **)opt->value = NULL; |
| 154 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
| 155 | *(const char **)opt->value = (const char *)opt->defval; |
| 156 | else |
| 157 | err = get_arg(p, opt, flags, (const char **)opt->value); |
| 158 | |
| 159 | if (!err) |
| 160 | fix_filename(p->prefix, (const char **)opt->value); |
| 161 | return err; |
| 162 | |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 163 | case OPTION_CALLBACK: |
Nguyễn Thái Ngọc Duy | 3ebbe28 | 2019-01-27 07:35:28 +0700 | [diff] [blame] | 164 | { |
| 165 | const char *p_arg = NULL; |
| 166 | int p_unset; |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 167 | |
Nguyễn Thái Ngọc Duy | 3ebbe28 | 2019-01-27 07:35:28 +0700 | [diff] [blame] | 168 | if (unset) |
| 169 | p_unset = 1; |
| 170 | else if (opt->flags & PARSE_OPT_NOARG) |
| 171 | p_unset = 0; |
| 172 | else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) |
| 173 | p_unset = 0; |
| 174 | else if (get_arg(p, opt, flags, &arg)) |
| 175 | return -1; |
| 176 | else { |
| 177 | p_unset = 0; |
| 178 | p_arg = arg; |
| 179 | } |
| 180 | if (opt->callback) |
| 181 | return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0; |
| 182 | else |
| 183 | return (*opt->ll_callback)(p, opt, p_arg, p_unset); |
| 184 | } |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 185 | case OPTION_INTEGER: |
Pierre Habouzit | db7244b | 2007-11-07 11:20:27 +0100 | [diff] [blame] | 186 | if (unset) { |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 187 | *(int *)opt->value = 0; |
| 188 | return 0; |
| 189 | } |
Pierre Habouzit | c43a248 | 2007-12-21 11:41:41 +0100 | [diff] [blame] | 190 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { |
Pierre Habouzit | ffe659f | 2007-10-15 01:45:45 +0200 | [diff] [blame] | 191 | *(int *)opt->value = opt->defval; |
| 192 | return 0; |
| 193 | } |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 194 | if (get_arg(p, opt, flags, &arg)) |
| 195 | return -1; |
Nguyễn Thái Ngọc Duy | f7e68a0 | 2019-05-29 16:11:16 +0700 | [diff] [blame] | 196 | if (!*arg) |
| 197 | return error(_("%s expects a numerical value"), |
| 198 | optname(opt, flags)); |
Pierre Habouzit | 1cc6985 | 2008-07-08 12:34:08 +0200 | [diff] [blame] | 199 | *(int *)opt->value = strtol(arg, (char **)&s, 10); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 200 | if (*s) |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 201 | return error(_("%s expects a numerical value"), |
| 202 | optname(opt, flags)); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 203 | return 0; |
| 204 | |
Charles Bailey | 2a514ed | 2015-06-21 19:25:44 +0100 | [diff] [blame] | 205 | case OPTION_MAGNITUDE: |
| 206 | if (unset) { |
| 207 | *(unsigned long *)opt->value = 0; |
| 208 | return 0; |
| 209 | } |
| 210 | if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { |
| 211 | *(unsigned long *)opt->value = opt->defval; |
| 212 | return 0; |
| 213 | } |
| 214 | if (get_arg(p, opt, flags, &arg)) |
| 215 | return -1; |
| 216 | if (!git_parse_ulong(arg, opt->value)) |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 217 | return error(_("%s expects a non-negative integer value" |
| 218 | " with an optional k/m/g suffix"), |
| 219 | optname(opt, flags)); |
Charles Bailey | 2a514ed | 2015-06-21 19:25:44 +0100 | [diff] [blame] | 220 | return 0; |
| 221 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 222 | default: |
Nguyễn Thái Ngọc Duy | 48a5499 | 2018-11-10 06:16:12 +0100 | [diff] [blame] | 223 | BUG("opt->type %d should not happen", opt->type); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 227 | static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p, |
| 228 | const struct option *options) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 229 | { |
Junio C Hamano | 1158826 | 2013-07-30 12:06:01 -0700 | [diff] [blame] | 230 | const struct option *all_opts = options; |
René Scharfe | e0319ff | 2009-05-07 21:45:08 +0200 | [diff] [blame] | 231 | const struct option *numopt = NULL; |
| 232 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 233 | for (; options->type != OPTION_END; options++) { |
| 234 | if (options->short_name == *p->opt) { |
| 235 | p->opt = p->opt[1] ? p->opt + 1 : NULL; |
Junio C Hamano | 1158826 | 2013-07-30 12:06:01 -0700 | [diff] [blame] | 236 | return get_value(p, options, all_opts, OPT_SHORT); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 237 | } |
René Scharfe | e0319ff | 2009-05-07 21:45:08 +0200 | [diff] [blame] | 238 | |
| 239 | /* |
| 240 | * Handle the numerical option later, explicit one-digit |
| 241 | * options take precedence over it. |
| 242 | */ |
| 243 | if (options->type == OPTION_NUMBER) |
| 244 | numopt = options; |
| 245 | } |
| 246 | if (numopt && isdigit(*p->opt)) { |
| 247 | size_t len = 1; |
| 248 | char *arg; |
| 249 | int rc; |
| 250 | |
| 251 | while (isdigit(p->opt[len])) |
| 252 | len++; |
| 253 | arg = xmemdupz(p->opt, len); |
| 254 | p->opt = p->opt[len] ? p->opt + len : NULL; |
Nguyễn Thái Ngọc Duy | 3ebbe28 | 2019-01-27 07:35:28 +0700 | [diff] [blame] | 255 | if (numopt->callback) |
| 256 | rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0; |
| 257 | else |
| 258 | rc = (*numopt->ll_callback)(p, numopt, arg, 0); |
René Scharfe | e0319ff | 2009-05-07 21:45:08 +0200 | [diff] [blame] | 259 | free(arg); |
| 260 | return rc; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 261 | } |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 262 | return PARSE_OPT_UNKNOWN; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 265 | static int has_string(const char *it, const char **array) |
| 266 | { |
| 267 | while (*array) |
| 268 | if (!strcmp(it, *(array++))) |
| 269 | return 1; |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static int is_alias(struct parse_opt_ctx_t *ctx, |
| 274 | const struct option *one_opt, |
| 275 | const struct option *another_opt) |
| 276 | { |
| 277 | const char **group; |
| 278 | |
| 279 | if (!ctx->alias_groups) |
| 280 | return 0; |
| 281 | |
| 282 | if (!one_opt->long_name || !another_opt->long_name) |
| 283 | return 0; |
| 284 | |
| 285 | for (group = ctx->alias_groups; *group; group += 3) { |
| 286 | /* it and other are from the same family? */ |
| 287 | if (has_string(one_opt->long_name, group) && |
| 288 | has_string(another_opt->long_name, group)) |
| 289 | return 1; |
| 290 | } |
| 291 | return 0; |
| 292 | } |
| 293 | |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 294 | static enum parse_opt_result parse_long_opt( |
| 295 | struct parse_opt_ctx_t *p, const char *arg, |
| 296 | const struct option *options) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 297 | { |
Junio C Hamano | 1158826 | 2013-07-30 12:06:01 -0700 | [diff] [blame] | 298 | const struct option *all_opts = options; |
Rohit Mani | 2c5495f | 2014-03-07 22:48:31 -0800 | [diff] [blame] | 299 | const char *arg_end = strchrnul(arg, '='); |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 300 | const struct option *abbrev_option = NULL, *ambiguous_option = NULL; |
| 301 | int abbrev_flags = 0, ambiguous_flags = 0; |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 302 | |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 303 | for (; options->type != OPTION_END; options++) { |
René Scharfe | 0f1930c5 | 2012-02-25 20:14:54 +0100 | [diff] [blame] | 304 | const char *rest, *long_name = options->long_name; |
| 305 | int flags = 0, opt_flags = 0; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 306 | |
René Scharfe | 0f1930c5 | 2012-02-25 20:14:54 +0100 | [diff] [blame] | 307 | if (!long_name) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 308 | continue; |
| 309 | |
René Scharfe | 0f1930c5 | 2012-02-25 20:14:54 +0100 | [diff] [blame] | 310 | again: |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 311 | if (!skip_prefix(arg, long_name, &rest)) |
| 312 | rest = NULL; |
Pierre Habouzit | 580d5bf | 2008-03-02 11:35:56 +0100 | [diff] [blame] | 313 | if (options->type == OPTION_ARGUMENT) { |
| 314 | if (!rest) |
| 315 | continue; |
| 316 | if (*rest == '=') |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 317 | return error(_("%s takes no value"), |
| 318 | optname(options, flags)); |
Pierre Habouzit | 580d5bf | 2008-03-02 11:35:56 +0100 | [diff] [blame] | 319 | if (*rest) |
| 320 | continue; |
Johannes Schindelin | 1a85b49 | 2019-03-14 04:25:04 -0700 | [diff] [blame] | 321 | if (options->value) |
| 322 | *(int *)options->value = options->defval; |
Pierre Habouzit | 580d5bf | 2008-03-02 11:35:56 +0100 | [diff] [blame] | 323 | p->out[p->cpidx++] = arg - 2; |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 324 | return PARSE_OPT_DONE; |
Pierre Habouzit | 580d5bf | 2008-03-02 11:35:56 +0100 | [diff] [blame] | 325 | } |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 326 | if (!rest) { |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 327 | /* abbreviated? */ |
Nguyễn Thái Ngọc Duy | baa4adc | 2019-01-27 07:35:24 +0700 | [diff] [blame] | 328 | if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) && |
| 329 | !strncmp(long_name, arg, arg_end - arg)) { |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 330 | is_abbreviated: |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 331 | if (abbrev_option && |
| 332 | !is_alias(p, abbrev_option, options)) { |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 333 | /* |
| 334 | * If this is abbreviated, it is |
| 335 | * ambiguous. So when there is no |
| 336 | * exact match later, we need to |
| 337 | * error out. |
| 338 | */ |
| 339 | ambiguous_option = abbrev_option; |
| 340 | ambiguous_flags = abbrev_flags; |
| 341 | } |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 342 | if (!(flags & OPT_UNSET) && *arg_end) |
| 343 | p->opt = arg_end + 1; |
| 344 | abbrev_option = options; |
René Scharfe | 0f1930c5 | 2012-02-25 20:14:54 +0100 | [diff] [blame] | 345 | abbrev_flags = flags ^ opt_flags; |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 346 | continue; |
| 347 | } |
Andreas Schwab | 6bbfd1f | 2009-09-25 20:44:44 +0200 | [diff] [blame] | 348 | /* negation allowed? */ |
| 349 | if (options->flags & PARSE_OPT_NONEG) |
| 350 | continue; |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 351 | /* negated and abbreviated very much? */ |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 352 | if (starts_with("no-", arg)) { |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 353 | flags |= OPT_UNSET; |
| 354 | goto is_abbreviated; |
| 355 | } |
| 356 | /* negated? */ |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 357 | if (!starts_with(arg, "no-")) { |
Junio C Hamano | 145136a | 2020-01-30 11:35:46 -0800 | [diff] [blame] | 358 | if (skip_prefix(long_name, "no-", &long_name)) { |
René Scharfe | 0f1930c5 | 2012-02-25 20:14:54 +0100 | [diff] [blame] | 359 | opt_flags |= OPT_UNSET; |
| 360 | goto again; |
| 361 | } |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 362 | continue; |
René Scharfe | 0f1930c5 | 2012-02-25 20:14:54 +0100 | [diff] [blame] | 363 | } |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 364 | flags |= OPT_UNSET; |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 365 | if (!skip_prefix(arg + 3, long_name, &rest)) { |
| 366 | /* abbreviated and negated? */ |
| 367 | if (starts_with(long_name, arg + 3)) |
| 368 | goto is_abbreviated; |
| 369 | else |
| 370 | continue; |
| 371 | } |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 372 | } |
| 373 | if (*rest) { |
| 374 | if (*rest != '=') |
| 375 | continue; |
| 376 | p->opt = rest + 1; |
| 377 | } |
Junio C Hamano | 1158826 | 2013-07-30 12:06:01 -0700 | [diff] [blame] | 378 | return get_value(p, options, all_opts, flags ^ opt_flags); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 379 | } |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 380 | |
Johannes Schindelin | b02e7d5 | 2019-04-12 02:37:24 -0700 | [diff] [blame] | 381 | if (disallow_abbreviated_options && (ambiguous_option || abbrev_option)) |
| 382 | die("disallowed abbreviated or ambiguous option '%.*s'", |
| 383 | (int)(arg_end - arg), arg); |
| 384 | |
Paul-Sebastian Ungureanu | 3bb0923 | 2018-03-22 20:43:51 +0200 | [diff] [blame] | 385 | if (ambiguous_option) { |
Nguyễn Thái Ngọc Duy | 8900342 | 2018-11-10 06:16:13 +0100 | [diff] [blame] | 386 | error(_("ambiguous option: %s " |
| 387 | "(could be --%s%s or --%s%s)"), |
Johannes Schindelin | 243e061 | 2007-11-05 13:15:21 +0000 | [diff] [blame] | 388 | arg, |
| 389 | (ambiguous_flags & OPT_UNSET) ? "no-" : "", |
| 390 | ambiguous_option->long_name, |
| 391 | (abbrev_flags & OPT_UNSET) ? "no-" : "", |
| 392 | abbrev_option->long_name); |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 393 | return PARSE_OPT_HELP; |
Paul-Sebastian Ungureanu | 3bb0923 | 2018-03-22 20:43:51 +0200 | [diff] [blame] | 394 | } |
Johannes Schindelin | 7f275b9 | 2007-10-14 17:54:06 +0100 | [diff] [blame] | 395 | if (abbrev_option) |
Junio C Hamano | 1158826 | 2013-07-30 12:06:01 -0700 | [diff] [blame] | 396 | return get_value(p, abbrev_option, all_opts, abbrev_flags); |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 397 | return PARSE_OPT_UNKNOWN; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 398 | } |
| 399 | |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 400 | static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg, |
| 401 | const struct option *options) |
| 402 | { |
Junio C Hamano | 1158826 | 2013-07-30 12:06:01 -0700 | [diff] [blame] | 403 | const struct option *all_opts = options; |
| 404 | |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 405 | for (; options->type != OPTION_END; options++) { |
| 406 | if (!(options->flags & PARSE_OPT_NODASH)) |
| 407 | continue; |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 408 | if (options->short_name == arg[0] && arg[1] == '\0') |
Junio C Hamano | 1158826 | 2013-07-30 12:06:01 -0700 | [diff] [blame] | 409 | return get_value(p, options, all_opts, OPT_SHORT); |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 410 | } |
| 411 | return -2; |
| 412 | } |
| 413 | |
Nanako Shiraishi | 1121a87 | 2008-07-16 19:42:18 +0900 | [diff] [blame] | 414 | static void check_typos(const char *arg, const struct option *options) |
Pierre Habouzit | 3a9f0f4 | 2008-01-26 12:26:57 +0100 | [diff] [blame] | 415 | { |
| 416 | if (strlen(arg) < 3) |
| 417 | return; |
| 418 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 419 | if (starts_with(arg, "no-")) { |
Jacques Bodin-Hullin | 395518c | 2020-02-05 13:07:23 +0000 | [diff] [blame] | 420 | error(_("did you mean `--%s` (with two dashes)?"), arg); |
Pierre Habouzit | 3a9f0f4 | 2008-01-26 12:26:57 +0100 | [diff] [blame] | 421 | exit(129); |
| 422 | } |
| 423 | |
| 424 | for (; options->type != OPTION_END; options++) { |
| 425 | if (!options->long_name) |
| 426 | continue; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 427 | if (starts_with(options->long_name, arg)) { |
Jacques Bodin-Hullin | 395518c | 2020-02-05 13:07:23 +0000 | [diff] [blame] | 428 | error(_("did you mean `--%s` (with two dashes)?"), arg); |
Pierre Habouzit | 3a9f0f4 | 2008-01-26 12:26:57 +0100 | [diff] [blame] | 429 | exit(129); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
Pierre Habouzit | cb9d398 | 2009-06-09 10:23:44 +0200 | [diff] [blame] | 434 | static void parse_options_check(const struct option *opts) |
| 435 | { |
| 436 | int err = 0; |
Junio C Hamano | af465af | 2014-09-03 12:42:37 -0700 | [diff] [blame] | 437 | char short_opts[128]; |
Pierre Habouzit | cb9d398 | 2009-06-09 10:23:44 +0200 | [diff] [blame] | 438 | |
Junio C Hamano | af465af | 2014-09-03 12:42:37 -0700 | [diff] [blame] | 439 | memset(short_opts, '\0', sizeof(short_opts)); |
Pierre Habouzit | cb9d398 | 2009-06-09 10:23:44 +0200 | [diff] [blame] | 440 | for (; opts->type != OPTION_END; opts++) { |
| 441 | if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) && |
Jonathan Nieder | 1e5ce57 | 2010-12-02 00:01:18 -0600 | [diff] [blame] | 442 | (opts->flags & PARSE_OPT_OPTARG)) |
| 443 | err |= optbug(opts, "uses incompatible flags " |
| 444 | "LASTARG_DEFAULT and OPTARG"); |
Junio C Hamano | af465af | 2014-09-03 12:42:37 -0700 | [diff] [blame] | 445 | if (opts->short_name) { |
| 446 | if (0x7F <= opts->short_name) |
| 447 | err |= optbug(opts, "invalid short name"); |
| 448 | else if (short_opts[opts->short_name]++) |
| 449 | err |= optbug(opts, "short name already used"); |
| 450 | } |
Jonathan Nieder | a02dd4f | 2010-12-02 00:05:05 -0600 | [diff] [blame] | 451 | if (opts->flags & PARSE_OPT_NODASH && |
| 452 | ((opts->flags & PARSE_OPT_OPTARG) || |
| 453 | !(opts->flags & PARSE_OPT_NOARG) || |
| 454 | !(opts->flags & PARSE_OPT_NONEG) || |
| 455 | opts->long_name)) |
| 456 | err |= optbug(opts, "uses feature " |
| 457 | "not supported for dashless options"); |
Jonathan Nieder | 5c400ed | 2010-12-02 00:08:57 -0600 | [diff] [blame] | 458 | switch (opts->type) { |
Junio C Hamano | b04ba2b | 2011-09-27 16:56:49 -0700 | [diff] [blame] | 459 | case OPTION_COUNTUP: |
Jonathan Nieder | 5c400ed | 2010-12-02 00:08:57 -0600 | [diff] [blame] | 460 | case OPTION_BIT: |
| 461 | case OPTION_NEGBIT: |
| 462 | case OPTION_SET_INT: |
Jonathan Nieder | 5c400ed | 2010-12-02 00:08:57 -0600 | [diff] [blame] | 463 | case OPTION_NUMBER: |
| 464 | if ((opts->flags & PARSE_OPT_OPTARG) || |
| 465 | !(opts->flags & PARSE_OPT_NOARG)) |
| 466 | err |= optbug(opts, "should not accept an argument"); |
Nguyễn Thái Ngọc Duy | bf3ff33 | 2019-01-27 07:35:26 +0700 | [diff] [blame] | 467 | break; |
| 468 | case OPTION_CALLBACK: |
Nguyễn Thái Ngọc Duy | 3ebbe28 | 2019-01-27 07:35:28 +0700 | [diff] [blame] | 469 | if (!opts->callback && !opts->ll_callback) |
| 470 | BUG("OPTION_CALLBACK needs one callback"); |
| 471 | if (opts->callback && opts->ll_callback) |
| 472 | BUG("OPTION_CALLBACK can't have two callbacks"); |
Nguyễn Thái Ngọc Duy | bf3ff33 | 2019-01-27 07:35:26 +0700 | [diff] [blame] | 473 | break; |
| 474 | case OPTION_LOWLEVEL_CALLBACK: |
| 475 | if (!opts->ll_callback) |
| 476 | BUG("OPTION_LOWLEVEL_CALLBACK needs a callback"); |
| 477 | if (opts->callback) |
| 478 | BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback"); |
| 479 | break; |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 480 | case OPTION_ALIAS: |
| 481 | BUG("OPT_ALIAS() should not remain at this point. " |
| 482 | "Are you using parse_options_step() directly?\n" |
| 483 | "That case is not supported yet."); |
Jonathan Nieder | 5c400ed | 2010-12-02 00:08:57 -0600 | [diff] [blame] | 484 | default: |
| 485 | ; /* ok. (usually accepts an argument) */ |
| 486 | } |
Junio C Hamano | b6c2a0d | 2014-03-23 16:04:36 -0700 | [diff] [blame] | 487 | if (opts->argh && |
| 488 | strcspn(opts->argh, " _") != strlen(opts->argh)) |
| 489 | err |= optbug(opts, "multi-word argh should use dash to separate words"); |
Pierre Habouzit | cb9d398 | 2009-06-09 10:23:44 +0200 | [diff] [blame] | 490 | } |
Pierre Habouzit | cb9d398 | 2009-06-09 10:23:44 +0200 | [diff] [blame] | 491 | if (err) |
Jonathan Nieder | 1e5ce57 | 2010-12-02 00:01:18 -0600 | [diff] [blame] | 492 | exit(128); |
Pierre Habouzit | cb9d398 | 2009-06-09 10:23:44 +0200 | [diff] [blame] | 493 | } |
| 494 | |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 495 | static void parse_options_start_1(struct parse_opt_ctx_t *ctx, |
| 496 | int argc, const char **argv, const char *prefix, |
| 497 | const struct option *options, int flags) |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 498 | { |
Nguyễn Thái Ngọc Duy | 202fbb3 | 2019-01-27 07:35:23 +0700 | [diff] [blame] | 499 | ctx->argc = argc; |
| 500 | ctx->argv = argv; |
| 501 | if (!(flags & PARSE_OPT_ONE_SHOT)) { |
| 502 | ctx->argc--; |
| 503 | ctx->argv++; |
| 504 | } |
| 505 | ctx->total = ctx->argc; |
| 506 | ctx->out = argv; |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 507 | ctx->prefix = prefix; |
Pierre Habouzit | a32a4ea | 2008-06-24 00:31:31 +0200 | [diff] [blame] | 508 | ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0); |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 509 | ctx->flags = flags; |
René Scharfe | 0d260f9 | 2009-03-09 21:57:38 +0100 | [diff] [blame] | 510 | if ((flags & PARSE_OPT_KEEP_UNKNOWN) && |
Nguyễn Thái Ngọc Duy | 202fbb3 | 2019-01-27 07:35:23 +0700 | [diff] [blame] | 511 | (flags & PARSE_OPT_STOP_AT_NON_OPTION) && |
| 512 | !(flags & PARSE_OPT_ONE_SHOT)) |
Nguyễn Thái Ngọc Duy | 48a5499 | 2018-11-10 06:16:12 +0100 | [diff] [blame] | 513 | BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together"); |
Nguyễn Thái Ngọc Duy | 202fbb3 | 2019-01-27 07:35:23 +0700 | [diff] [blame] | 514 | if ((flags & PARSE_OPT_ONE_SHOT) && |
| 515 | (flags & PARSE_OPT_KEEP_ARGV0)) |
| 516 | BUG("Can't keep argv0 if you don't have it"); |
Stephen Boyd | 9ca1169 | 2010-12-05 23:57:42 -0800 | [diff] [blame] | 517 | parse_options_check(options); |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 518 | } |
| 519 | |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 520 | void parse_options_start(struct parse_opt_ctx_t *ctx, |
| 521 | int argc, const char **argv, const char *prefix, |
| 522 | const struct option *options, int flags) |
| 523 | { |
| 524 | memset(ctx, 0, sizeof(*ctx)); |
| 525 | parse_options_start_1(ctx, argc, argv, prefix, options, flags); |
| 526 | } |
| 527 | |
Ryan Zoeller | a0abe5e | 2020-08-19 23:06:08 +0000 | [diff] [blame] | 528 | static void show_negated_gitcomp(const struct option *opts, int show_all, |
| 529 | int nr_noopts) |
Nguyễn Thái Ngọc Duy | b221b5a | 2018-06-06 11:41:39 +0200 | [diff] [blame] | 530 | { |
| 531 | int printed_dashdash = 0; |
| 532 | |
| 533 | for (; opts->type != OPTION_END; opts++) { |
| 534 | int has_unset_form = 0; |
| 535 | const char *name; |
| 536 | |
| 537 | if (!opts->long_name) |
| 538 | continue; |
Ryan Zoeller | a0abe5e | 2020-08-19 23:06:08 +0000 | [diff] [blame] | 539 | if (!show_all && |
| 540 | (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))) |
Nguyễn Thái Ngọc Duy | b221b5a | 2018-06-06 11:41:39 +0200 | [diff] [blame] | 541 | continue; |
| 542 | if (opts->flags & PARSE_OPT_NONEG) |
| 543 | continue; |
| 544 | |
| 545 | switch (opts->type) { |
| 546 | case OPTION_STRING: |
| 547 | case OPTION_FILENAME: |
| 548 | case OPTION_INTEGER: |
| 549 | case OPTION_MAGNITUDE: |
| 550 | case OPTION_CALLBACK: |
| 551 | case OPTION_BIT: |
| 552 | case OPTION_NEGBIT: |
| 553 | case OPTION_COUNTUP: |
| 554 | case OPTION_SET_INT: |
| 555 | has_unset_form = 1; |
| 556 | break; |
| 557 | default: |
| 558 | break; |
| 559 | } |
| 560 | if (!has_unset_form) |
| 561 | continue; |
| 562 | |
| 563 | if (skip_prefix(opts->long_name, "no-", &name)) { |
| 564 | if (nr_noopts < 0) |
| 565 | printf(" --%s", name); |
| 566 | } else if (nr_noopts >= 0) { |
| 567 | if (nr_noopts && !printed_dashdash) { |
| 568 | printf(" --"); |
| 569 | printed_dashdash = 1; |
| 570 | } |
| 571 | printf(" --no-%s", opts->long_name); |
| 572 | nr_noopts++; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
Ryan Zoeller | a0abe5e | 2020-08-19 23:06:08 +0000 | [diff] [blame] | 577 | static int show_gitcomp(const struct option *opts, int show_all) |
Nguyễn Thái Ngọc Duy | b9d7f4b | 2018-02-09 18:01:40 +0700 | [diff] [blame] | 578 | { |
Nguyễn Thái Ngọc Duy | b221b5a | 2018-06-06 11:41:39 +0200 | [diff] [blame] | 579 | const struct option *original_opts = opts; |
| 580 | int nr_noopts = 0; |
| 581 | |
Nguyễn Thái Ngọc Duy | b9d7f4b | 2018-02-09 18:01:40 +0700 | [diff] [blame] | 582 | for (; opts->type != OPTION_END; opts++) { |
| 583 | const char *suffix = ""; |
| 584 | |
| 585 | if (!opts->long_name) |
| 586 | continue; |
Ryan Zoeller | a0abe5e | 2020-08-19 23:06:08 +0000 | [diff] [blame] | 587 | if (!show_all && |
| 588 | (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))) |
Nguyễn Thái Ngọc Duy | b9d7f4b | 2018-02-09 18:01:40 +0700 | [diff] [blame] | 589 | continue; |
| 590 | |
| 591 | switch (opts->type) { |
| 592 | case OPTION_GROUP: |
| 593 | continue; |
| 594 | case OPTION_STRING: |
| 595 | case OPTION_FILENAME: |
| 596 | case OPTION_INTEGER: |
| 597 | case OPTION_MAGNITUDE: |
| 598 | case OPTION_CALLBACK: |
| 599 | if (opts->flags & PARSE_OPT_NOARG) |
| 600 | break; |
| 601 | if (opts->flags & PARSE_OPT_OPTARG) |
| 602 | break; |
| 603 | if (opts->flags & PARSE_OPT_LASTARG_DEFAULT) |
| 604 | break; |
| 605 | suffix = "="; |
| 606 | break; |
| 607 | default: |
| 608 | break; |
| 609 | } |
Nguyễn Thái Ngọc Duy | ebc4a04 | 2018-02-09 18:02:12 +0700 | [diff] [blame] | 610 | if (opts->flags & PARSE_OPT_COMP_ARG) |
| 611 | suffix = "="; |
Nguyễn Thái Ngọc Duy | b221b5a | 2018-06-06 11:41:39 +0200 | [diff] [blame] | 612 | if (starts_with(opts->long_name, "no-")) |
| 613 | nr_noopts++; |
Nguyễn Thái Ngọc Duy | b9d7f4b | 2018-02-09 18:01:40 +0700 | [diff] [blame] | 614 | printf(" --%s%s", opts->long_name, suffix); |
| 615 | } |
Ryan Zoeller | a0abe5e | 2020-08-19 23:06:08 +0000 | [diff] [blame] | 616 | show_negated_gitcomp(original_opts, show_all, -1); |
| 617 | show_negated_gitcomp(original_opts, show_all, nr_noopts); |
Nguyễn Thái Ngọc Duy | b9d7f4b | 2018-02-09 18:01:40 +0700 | [diff] [blame] | 618 | fputc('\n', stdout); |
Nguyễn Thái Ngọc Duy | a92ec7e | 2018-12-11 16:35:01 +0100 | [diff] [blame] | 619 | return PARSE_OPT_COMPLETE; |
Nguyễn Thái Ngọc Duy | b9d7f4b | 2018-02-09 18:01:40 +0700 | [diff] [blame] | 620 | } |
| 621 | |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 622 | /* |
| 623 | * Scan and may produce a new option[] array, which should be used |
| 624 | * instead of the original 'options'. |
| 625 | * |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 626 | * Right now this is only used to preprocess and substitute |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 627 | * OPTION_ALIAS. |
Andrzej Hunt | 64cc539 | 2021-03-21 16:58:36 +0000 | [diff] [blame] | 628 | * |
| 629 | * The returned options should be freed using free_preprocessed_options. |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 630 | */ |
| 631 | static struct option *preprocess_options(struct parse_opt_ctx_t *ctx, |
| 632 | const struct option *options) |
| 633 | { |
| 634 | struct option *newopt; |
| 635 | int i, nr, alias; |
| 636 | int nr_aliases = 0; |
| 637 | |
| 638 | for (nr = 0; options[nr].type != OPTION_END; nr++) { |
| 639 | if (options[nr].type == OPTION_ALIAS) |
| 640 | nr_aliases++; |
| 641 | } |
| 642 | |
| 643 | if (!nr_aliases) |
| 644 | return NULL; |
| 645 | |
| 646 | ALLOC_ARRAY(newopt, nr + 1); |
| 647 | COPY_ARRAY(newopt, options, nr + 1); |
| 648 | |
| 649 | /* each alias has two string pointers and NULL */ |
| 650 | CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1)); |
| 651 | |
| 652 | for (alias = 0, i = 0; i < nr; i++) { |
| 653 | int short_name; |
| 654 | const char *long_name; |
| 655 | const char *source; |
Junio C Hamano | 7c28058 | 2020-03-16 13:22:54 -0700 | [diff] [blame] | 656 | struct strbuf help = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 657 | int j; |
| 658 | |
| 659 | if (newopt[i].type != OPTION_ALIAS) |
| 660 | continue; |
| 661 | |
| 662 | short_name = newopt[i].short_name; |
| 663 | long_name = newopt[i].long_name; |
| 664 | source = newopt[i].value; |
| 665 | |
| 666 | if (!long_name) |
| 667 | BUG("An alias must have long option name"); |
Junio C Hamano | 7c28058 | 2020-03-16 13:22:54 -0700 | [diff] [blame] | 668 | strbuf_addf(&help, _("alias of --%s"), source); |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 669 | |
| 670 | for (j = 0; j < nr; j++) { |
| 671 | const char *name = options[j].long_name; |
| 672 | |
| 673 | if (!name || strcmp(name, source)) |
| 674 | continue; |
| 675 | |
| 676 | if (options[j].type == OPTION_ALIAS) |
| 677 | BUG("No please. Nested aliases are not supported."); |
| 678 | |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 679 | memcpy(newopt + i, options + j, sizeof(*newopt)); |
| 680 | newopt[i].short_name = short_name; |
| 681 | newopt[i].long_name = long_name; |
Junio C Hamano | 7c28058 | 2020-03-16 13:22:54 -0700 | [diff] [blame] | 682 | newopt[i].help = strbuf_detach(&help, NULL); |
Andrzej Hunt | 64cc539 | 2021-03-21 16:58:36 +0000 | [diff] [blame] | 683 | newopt[i].flags |= PARSE_OPT_FROM_ALIAS; |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 684 | break; |
| 685 | } |
| 686 | |
| 687 | if (j == nr) |
| 688 | BUG("could not find source option '%s' of alias '%s'", |
| 689 | source, newopt[i].long_name); |
| 690 | ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name; |
| 691 | ctx->alias_groups[alias * 3 + 1] = options[j].long_name; |
| 692 | ctx->alias_groups[alias * 3 + 2] = NULL; |
| 693 | alias++; |
| 694 | } |
| 695 | |
| 696 | return newopt; |
| 697 | } |
| 698 | |
Andrzej Hunt | 64cc539 | 2021-03-21 16:58:36 +0000 | [diff] [blame] | 699 | static void free_preprocessed_options(struct option *options) |
| 700 | { |
| 701 | int i; |
| 702 | |
| 703 | if (!options) |
| 704 | return; |
| 705 | |
| 706 | for (i = 0; options[i].type != OPTION_END; i++) { |
| 707 | if (options[i].flags & PARSE_OPT_FROM_ALIAS) |
| 708 | free((void *)options[i].help); |
| 709 | } |
| 710 | free(options); |
| 711 | } |
| 712 | |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 713 | static int usage_with_options_internal(struct parse_opt_ctx_t *, |
| 714 | const char * const *, |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 715 | const struct option *, int, int); |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 716 | |
| 717 | int parse_options_step(struct parse_opt_ctx_t *ctx, |
| 718 | const struct option *options, |
| 719 | const char * const usagestr[]) |
| 720 | { |
René Scharfe | b92891f | 2009-03-08 19:15:08 +0100 | [diff] [blame] | 721 | int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP); |
| 722 | |
Pierre Habouzit | 26141b5 | 2008-06-23 22:55:11 +0200 | [diff] [blame] | 723 | /* we must reset ->opt, unknown short option leave it dangling */ |
| 724 | ctx->opt = NULL; |
| 725 | |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 726 | for (; ctx->argc; ctx->argc--, ctx->argv++) { |
| 727 | const char *arg = ctx->argv[0]; |
| 728 | |
Nguyễn Thái Ngọc Duy | 202fbb3 | 2019-01-27 07:35:23 +0700 | [diff] [blame] | 729 | if (ctx->flags & PARSE_OPT_ONE_SHOT && |
| 730 | ctx->argc != ctx->total) |
| 731 | break; |
| 732 | |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 733 | if (*arg != '-' || !arg[1]) { |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 734 | if (parse_nodash_opt(ctx, arg, options) == 0) |
| 735 | continue; |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 736 | if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION) |
Jonathan Nieder | 979240f | 2010-12-01 17:32:55 -0600 | [diff] [blame] | 737 | return PARSE_OPT_NON_OPTION; |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 738 | ctx->out[ctx->cpidx++] = ctx->argv[0]; |
| 739 | continue; |
| 740 | } |
| 741 | |
René Scharfe | 5ad0d3d | 2015-11-17 11:25:38 +0100 | [diff] [blame] | 742 | /* lone -h asks for help */ |
| 743 | if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h")) |
| 744 | goto show_usage; |
| 745 | |
Ryan Zoeller | a0abe5e | 2020-08-19 23:06:08 +0000 | [diff] [blame] | 746 | /* |
| 747 | * lone --git-completion-helper and --git-completion-helper-all |
| 748 | * are asked by git-completion.bash |
| 749 | */ |
| 750 | if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper")) |
| 751 | return show_gitcomp(options, 0); |
| 752 | if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all")) |
| 753 | return show_gitcomp(options, 1); |
Nguyễn Thái Ngọc Duy | b9d7f4b | 2018-02-09 18:01:40 +0700 | [diff] [blame] | 754 | |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 755 | if (arg[1] != '-') { |
| 756 | ctx->opt = arg + 1; |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 757 | switch (parse_short_opt(ctx, options)) { |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 758 | case PARSE_OPT_ERROR: |
Paul-Sebastian Ungureanu | 3bb0923 | 2018-03-22 20:43:51 +0200 | [diff] [blame] | 759 | return PARSE_OPT_ERROR; |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 760 | case PARSE_OPT_UNKNOWN: |
René Scharfe | 38916c5 | 2012-03-03 12:00:29 +0100 | [diff] [blame] | 761 | if (ctx->opt) |
| 762 | check_typos(arg + 1, options); |
René Scharfe | 5ad0d3d | 2015-11-17 11:25:38 +0100 | [diff] [blame] | 763 | if (internal_help && *ctx->opt == 'h') |
| 764 | goto show_usage; |
René Scharfe | b5ce3a5 | 2009-03-08 19:12:47 +0100 | [diff] [blame] | 765 | goto unknown; |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 766 | case PARSE_OPT_NON_OPTION: |
| 767 | case PARSE_OPT_HELP: |
| 768 | case PARSE_OPT_COMPLETE: |
| 769 | BUG("parse_short_opt() cannot return these"); |
| 770 | case PARSE_OPT_DONE: |
| 771 | break; |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 772 | } |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 773 | if (ctx->opt) |
| 774 | check_typos(arg + 1, options); |
| 775 | while (ctx->opt) { |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 776 | switch (parse_short_opt(ctx, options)) { |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 777 | case PARSE_OPT_ERROR: |
Paul-Sebastian Ungureanu | 3bb0923 | 2018-03-22 20:43:51 +0200 | [diff] [blame] | 778 | return PARSE_OPT_ERROR; |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 779 | case PARSE_OPT_UNKNOWN: |
René Scharfe | 5ad0d3d | 2015-11-17 11:25:38 +0100 | [diff] [blame] | 780 | if (internal_help && *ctx->opt == 'h') |
| 781 | goto show_usage; |
| 782 | |
Pierre Habouzit | 26141b5 | 2008-06-23 22:55:11 +0200 | [diff] [blame] | 783 | /* fake a short option thing to hide the fact that we may have |
| 784 | * started to parse aggregated stuff |
| 785 | * |
| 786 | * This is leaky, too bad. |
| 787 | */ |
| 788 | ctx->argv[0] = xstrdup(ctx->opt - 1); |
| 789 | *(char *)ctx->argv[0] = '-'; |
René Scharfe | b5ce3a5 | 2009-03-08 19:12:47 +0100 | [diff] [blame] | 790 | goto unknown; |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 791 | case PARSE_OPT_NON_OPTION: |
| 792 | case PARSE_OPT_COMPLETE: |
| 793 | case PARSE_OPT_HELP: |
| 794 | BUG("parse_short_opt() cannot return these"); |
| 795 | case PARSE_OPT_DONE: |
| 796 | break; |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 797 | } |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 798 | } |
| 799 | continue; |
| 800 | } |
| 801 | |
Jeff King | 51b4594 | 2019-08-06 10:40:16 -0400 | [diff] [blame] | 802 | if (!arg[2] /* "--" */ || |
| 803 | !strcmp(arg + 2, "end-of-options")) { |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 804 | if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) { |
| 805 | ctx->argc--; |
| 806 | ctx->argv++; |
| 807 | } |
| 808 | break; |
| 809 | } |
| 810 | |
René Scharfe | b92891f | 2009-03-08 19:15:08 +0100 | [diff] [blame] | 811 | if (internal_help && !strcmp(arg + 2, "help-all")) |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 812 | return usage_with_options_internal(ctx, usagestr, options, 1, 0); |
René Scharfe | b92891f | 2009-03-08 19:15:08 +0100 | [diff] [blame] | 813 | if (internal_help && !strcmp(arg + 2, "help")) |
René Scharfe | ac20ff6 | 2015-11-17 11:25:14 +0100 | [diff] [blame] | 814 | goto show_usage; |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 815 | switch (parse_long_opt(ctx, arg + 2, options)) { |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 816 | case PARSE_OPT_ERROR: |
Paul-Sebastian Ungureanu | 3bb0923 | 2018-03-22 20:43:51 +0200 | [diff] [blame] | 817 | return PARSE_OPT_ERROR; |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 818 | case PARSE_OPT_UNKNOWN: |
René Scharfe | b5ce3a5 | 2009-03-08 19:12:47 +0100 | [diff] [blame] | 819 | goto unknown; |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 820 | case PARSE_OPT_HELP: |
Paul-Sebastian Ungureanu | 3bb0923 | 2018-03-22 20:43:51 +0200 | [diff] [blame] | 821 | goto show_usage; |
Nguyễn Thái Ngọc Duy | f41179f | 2019-01-27 07:35:27 +0700 | [diff] [blame] | 822 | case PARSE_OPT_NON_OPTION: |
| 823 | case PARSE_OPT_COMPLETE: |
| 824 | BUG("parse_long_opt() cannot return these"); |
| 825 | case PARSE_OPT_DONE: |
| 826 | break; |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 827 | } |
René Scharfe | b5ce3a5 | 2009-03-08 19:12:47 +0100 | [diff] [blame] | 828 | continue; |
| 829 | unknown: |
Nguyễn Thái Ngọc Duy | 202fbb3 | 2019-01-27 07:35:23 +0700 | [diff] [blame] | 830 | if (ctx->flags & PARSE_OPT_ONE_SHOT) |
| 831 | break; |
René Scharfe | b5ce3a5 | 2009-03-08 19:12:47 +0100 | [diff] [blame] | 832 | if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN)) |
| 833 | return PARSE_OPT_UNKNOWN; |
| 834 | ctx->out[ctx->cpidx++] = ctx->argv[0]; |
| 835 | ctx->opt = NULL; |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 836 | } |
| 837 | return PARSE_OPT_DONE; |
René Scharfe | ac20ff6 | 2015-11-17 11:25:14 +0100 | [diff] [blame] | 838 | |
René Scharfe | ac20ff6 | 2015-11-17 11:25:14 +0100 | [diff] [blame] | 839 | show_usage: |
Paul-Sebastian Ungureanu | 3bb0923 | 2018-03-22 20:43:51 +0200 | [diff] [blame] | 840 | return usage_with_options_internal(ctx, usagestr, options, 0, 0); |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 841 | } |
| 842 | |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 843 | int parse_options_end(struct parse_opt_ctx_t *ctx) |
| 844 | { |
Nguyễn Thái Ngọc Duy | 202fbb3 | 2019-01-27 07:35:23 +0700 | [diff] [blame] | 845 | if (ctx->flags & PARSE_OPT_ONE_SHOT) |
| 846 | return ctx->total - ctx->argc; |
| 847 | |
SZEDER Gábor | f919ffe | 2018-01-22 18:50:09 +0100 | [diff] [blame] | 848 | MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc); |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 849 | ctx->out[ctx->cpidx + ctx->argc] = NULL; |
| 850 | return ctx->cpidx + ctx->argc; |
| 851 | } |
| 852 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 853 | int parse_options(int argc, const char **argv, const char *prefix, |
| 854 | const struct option *options, const char * const usagestr[], |
| 855 | int flags) |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 856 | { |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 857 | struct parse_opt_ctx_t ctx; |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 858 | struct option *real_options; |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 859 | |
Johannes Schindelin | b02e7d5 | 2019-04-12 02:37:24 -0700 | [diff] [blame] | 860 | disallow_abbreviated_options = |
| 861 | git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0); |
| 862 | |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 863 | memset(&ctx, 0, sizeof(ctx)); |
| 864 | real_options = preprocess_options(&ctx, options); |
| 865 | if (real_options) |
| 866 | options = real_options; |
| 867 | parse_options_start_1(&ctx, argc, argv, prefix, options, flags); |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 868 | switch (parse_options_step(&ctx, options, usagestr)) { |
| 869 | case PARSE_OPT_HELP: |
Paul-Sebastian Ungureanu | 3bb0923 | 2018-03-22 20:43:51 +0200 | [diff] [blame] | 870 | case PARSE_OPT_ERROR: |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 871 | exit(129); |
Nguyễn Thái Ngọc Duy | a92ec7e | 2018-12-11 16:35:01 +0100 | [diff] [blame] | 872 | case PARSE_OPT_COMPLETE: |
| 873 | exit(0); |
Jonathan Nieder | 979240f | 2010-12-01 17:32:55 -0600 | [diff] [blame] | 874 | case PARSE_OPT_NON_OPTION: |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 875 | case PARSE_OPT_DONE: |
| 876 | break; |
| 877 | default: /* PARSE_OPT_UNKNOWN */ |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 878 | if (ctx.argv[0][1] == '-') { |
Nguyễn Thái Ngọc Duy | 8900342 | 2018-11-10 06:16:13 +0100 | [diff] [blame] | 879 | error(_("unknown option `%s'"), ctx.argv[0] + 2); |
Erik Faye-Lund | b141a47 | 2013-02-12 00:13:48 +0100 | [diff] [blame] | 880 | } else if (isascii(*ctx.opt)) { |
Nguyễn Thái Ngọc Duy | 8900342 | 2018-11-10 06:16:13 +0100 | [diff] [blame] | 881 | error(_("unknown switch `%c'"), *ctx.opt); |
Erik Faye-Lund | b141a47 | 2013-02-12 00:13:48 +0100 | [diff] [blame] | 882 | } else { |
Nguyễn Thái Ngọc Duy | 8900342 | 2018-11-10 06:16:13 +0100 | [diff] [blame] | 883 | error(_("unknown non-ascii option in string: `%s'"), |
Erik Faye-Lund | b141a47 | 2013-02-12 00:13:48 +0100 | [diff] [blame] | 884 | ctx.argv[0]); |
Pierre Habouzit | 07fe54d | 2008-06-23 22:46:36 +0200 | [diff] [blame] | 885 | } |
| 886 | usage_with_options(usagestr, options); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 887 | } |
| 888 | |
Torsten Bögershausen | 5c32750 | 2021-02-03 17:28:23 +0100 | [diff] [blame] | 889 | precompose_argv_prefix(argc, argv, NULL); |
Andrzej Hunt | 64cc539 | 2021-03-21 16:58:36 +0000 | [diff] [blame] | 890 | free_preprocessed_options(real_options); |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 891 | free(ctx.alias_groups); |
Pierre Habouzit | 7e7bbcb | 2008-06-23 21:59:37 +0200 | [diff] [blame] | 892 | return parse_options_end(&ctx); |
Pierre Habouzit | 4a59fd1 | 2007-10-15 01:35:37 +0200 | [diff] [blame] | 893 | } |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 894 | |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 895 | static int usage_argh(const struct option *opts, FILE *outfile) |
Stephen Boyd | 29f25d4 | 2009-05-21 00:33:17 -0700 | [diff] [blame] | 896 | { |
| 897 | const char *s; |
René Scharfe | 5f0df44 | 2018-08-02 21:18:14 +0200 | [diff] [blame] | 898 | int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || |
| 899 | !opts->argh || !!strpbrk(opts->argh, "()<>[]|"); |
Stephen Boyd | 29f25d4 | 2009-05-21 00:33:17 -0700 | [diff] [blame] | 900 | if (opts->flags & PARSE_OPT_OPTARG) |
| 901 | if (opts->long_name) |
| 902 | s = literal ? "[=%s]" : "[=<%s>]"; |
| 903 | else |
| 904 | s = literal ? "[%s]" : "[<%s>]"; |
| 905 | else |
| 906 | s = literal ? " %s" : " <%s>"; |
Jiang Xin | c082196 | 2013-02-09 14:31:09 +0800 | [diff] [blame] | 907 | return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("...")); |
Stephen Boyd | 29f25d4 | 2009-05-21 00:33:17 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 910 | #define USAGE_OPTS_WIDTH 24 |
| 911 | #define USAGE_GAP 2 |
| 912 | |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 913 | static int usage_with_options_internal(struct parse_opt_ctx_t *ctx, |
| 914 | const char * const *usagestr, |
| 915 | const struct option *opts, int full, int err) |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 916 | { |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 917 | FILE *outfile = err ? stderr : stdout; |
Brandon Casey | a6304fa | 2017-09-24 21:08:05 -0700 | [diff] [blame] | 918 | int need_newline; |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 919 | |
René Scharfe | 49b6180 | 2009-03-08 19:16:58 +0100 | [diff] [blame] | 920 | if (!usagestr) |
| 921 | return PARSE_OPT_HELP; |
| 922 | |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 923 | if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) |
| 924 | fprintf(outfile, "cat <<\\EOF\n"); |
| 925 | |
Nguyễn Thái Ngọc Duy | 54e6dc7 | 2012-05-06 21:23:51 +0700 | [diff] [blame] | 926 | fprintf_ln(outfile, _("usage: %s"), _(*usagestr++)); |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 927 | while (*usagestr && **usagestr) |
Ævar Arnfjörð Bjarmason | 66f5f6d | 2017-05-11 21:20:12 +0000 | [diff] [blame] | 928 | /* |
| 929 | * TRANSLATORS: the colon here should align with the |
| 930 | * one in "usage: %s" translation. |
| 931 | */ |
Nguyễn Thái Ngọc Duy | 54e6dc7 | 2012-05-06 21:23:51 +0700 | [diff] [blame] | 932 | fprintf_ln(outfile, _(" or: %s"), _(*usagestr++)); |
Jeff King | 44d86e9 | 2008-06-14 03:27:21 -0400 | [diff] [blame] | 933 | while (*usagestr) { |
Nguyễn Thái Ngọc Duy | 54e6dc7 | 2012-05-06 21:23:51 +0700 | [diff] [blame] | 934 | if (**usagestr) |
| 935 | fprintf_ln(outfile, _(" %s"), _(*usagestr)); |
| 936 | else |
Brandon Casey | 1a9bf1e | 2017-09-24 21:08:04 -0700 | [diff] [blame] | 937 | fputc('\n', outfile); |
Jeff King | 44d86e9 | 2008-06-14 03:27:21 -0400 | [diff] [blame] | 938 | usagestr++; |
| 939 | } |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 940 | |
Brandon Casey | a6304fa | 2017-09-24 21:08:05 -0700 | [diff] [blame] | 941 | need_newline = 1; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 942 | |
| 943 | for (; opts->type != OPTION_END; opts++) { |
| 944 | size_t pos; |
| 945 | int pad; |
| 946 | |
| 947 | if (opts->type == OPTION_GROUP) { |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 948 | fputc('\n', outfile); |
Brandon Casey | a6304fa | 2017-09-24 21:08:05 -0700 | [diff] [blame] | 949 | need_newline = 0; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 950 | if (*opts->help) |
Nguyễn Thái Ngọc Duy | 54e6dc7 | 2012-05-06 21:23:51 +0700 | [diff] [blame] | 951 | fprintf(outfile, "%s\n", _(opts->help)); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 952 | continue; |
| 953 | } |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 954 | if (!full && (opts->flags & PARSE_OPT_HIDDEN)) |
| 955 | continue; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 956 | |
Brandon Casey | a6304fa | 2017-09-24 21:08:05 -0700 | [diff] [blame] | 957 | if (need_newline) { |
| 958 | fputc('\n', outfile); |
| 959 | need_newline = 0; |
| 960 | } |
| 961 | |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 962 | pos = fprintf(outfile, " "); |
René Scharfe | cbb08c2 | 2012-02-28 20:06:09 +0100 | [diff] [blame] | 963 | if (opts->short_name) { |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 964 | if (opts->flags & PARSE_OPT_NODASH) |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 965 | pos += fprintf(outfile, "%c", opts->short_name); |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 966 | else |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 967 | pos += fprintf(outfile, "-%c", opts->short_name); |
René Scharfe | 51a9949 | 2009-05-07 21:45:42 +0200 | [diff] [blame] | 968 | } |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 969 | if (opts->long_name && opts->short_name) |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 970 | pos += fprintf(outfile, ", "); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 971 | if (opts->long_name) |
René Scharfe | cbb08c2 | 2012-02-28 20:06:09 +0100 | [diff] [blame] | 972 | pos += fprintf(outfile, "--%s", opts->long_name); |
René Scharfe | e0319ff | 2009-05-07 21:45:08 +0200 | [diff] [blame] | 973 | if (opts->type == OPTION_NUMBER) |
Jiang Xin | c082196 | 2013-02-09 14:31:09 +0800 | [diff] [blame] | 974 | pos += utf8_fprintf(outfile, _("-NUM")); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 975 | |
Jonathan Nieder | b57c68a | 2010-12-01 17:31:36 -0600 | [diff] [blame] | 976 | if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) || |
| 977 | !(opts->flags & PARSE_OPT_NOARG)) |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 978 | pos += usage_argh(opts, outfile); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 979 | |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 980 | if (pos <= USAGE_OPTS_WIDTH) |
| 981 | pad = USAGE_OPTS_WIDTH - pos; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 982 | else { |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 983 | fputc('\n', outfile); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 984 | pad = USAGE_OPTS_WIDTH; |
| 985 | } |
Nguyễn Thái Ngọc Duy | 5c38742 | 2019-04-29 17:05:25 +0700 | [diff] [blame] | 986 | if (opts->type == OPTION_ALIAS) { |
| 987 | fprintf(outfile, "%*s", pad + USAGE_GAP, ""); |
| 988 | fprintf_ln(outfile, _("alias of --%s"), |
| 989 | (const char *)opts->value); |
| 990 | continue; |
| 991 | } |
Nguyễn Thái Ngọc Duy | 54e6dc7 | 2012-05-06 21:23:51 +0700 | [diff] [blame] | 992 | fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help)); |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 993 | } |
Giuseppe Scrivano | 9c7304e | 2010-05-17 17:34:41 +0200 | [diff] [blame] | 994 | fputc('\n', outfile); |
Alex Riesen | f389c80 | 2007-10-14 00:10:51 +0200 | [diff] [blame] | 995 | |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 996 | if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL) |
| 997 | fputs("EOF\n", outfile); |
| 998 | |
Pierre Habouzit | ee68b87 | 2008-06-23 22:28:04 +0200 | [diff] [blame] | 999 | return PARSE_OPT_HELP; |
Pierre Habouzit | d7a38c5 | 2007-10-15 01:38:30 +0200 | [diff] [blame] | 1000 | } |
Pierre Habouzit | 0ce865b | 2007-10-14 11:05:12 +0200 | [diff] [blame] | 1001 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 1002 | void NORETURN usage_with_options(const char * const *usagestr, |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 1003 | const struct option *opts) |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 1004 | { |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 1005 | usage_with_options_internal(NULL, usagestr, opts, 0, 1); |
Pierre Habouzit | ff43ec3 | 2008-06-23 22:38:58 +0200 | [diff] [blame] | 1006 | exit(129); |
Pierre Habouzit | dd3bf0f | 2007-11-19 10:21:44 +0100 | [diff] [blame] | 1007 | } |
| 1008 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 1009 | void NORETURN usage_msg_opt(const char *msg, |
Christian Couder | 451bb21 | 2009-02-02 06:12:58 +0100 | [diff] [blame] | 1010 | const char * const *usagestr, |
| 1011 | const struct option *options) |
| 1012 | { |
Jeff King | 8743326 | 2016-12-14 10:10:10 -0500 | [diff] [blame] | 1013 | fprintf(stderr, "fatal: %s\n\n", msg); |
Christian Couder | 451bb21 | 2009-02-02 06:12:58 +0100 | [diff] [blame] | 1014 | usage_with_options(usagestr, options); |
| 1015 | } |
| 1016 | |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 1017 | const char *optname(const struct option *opt, int flags) |
Jeff King | a469a10 | 2012-12-15 12:42:10 -0500 | [diff] [blame] | 1018 | { |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 1019 | static struct strbuf sb = STRBUF_INIT; |
| 1020 | |
| 1021 | strbuf_reset(&sb); |
Jeff King | a469a10 | 2012-12-15 12:42:10 -0500 | [diff] [blame] | 1022 | if (flags & OPT_SHORT) |
Nguyễn Thái Ngọc Duy | 9440b83 | 2018-11-10 06:16:11 +0100 | [diff] [blame] | 1023 | strbuf_addf(&sb, "switch `%c'", opt->short_name); |
| 1024 | else if (flags & OPT_UNSET) |
| 1025 | strbuf_addf(&sb, "option `no-%s'", opt->long_name); |
| 1026 | else |
| 1027 | strbuf_addf(&sb, "option `%s'", opt->long_name); |
| 1028 | |
| 1029 | return sb.buf; |
Jeff King | a469a10 | 2012-12-15 12:42:10 -0500 | [diff] [blame] | 1030 | } |