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