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