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