{"thread":{"id":"66820","subject":"[RFC PATCH 0/2] parse-options: improve early option scanning","startedAt":"2026-11-10T16:13:11Z","lastAt":"2026-11-10T16:13:13Z","messageCount":3,"participants":["Azuama Odinaka Maximus"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"558100","messageId":"20261110161314.35213-1-azuama69@gmail.com","threadId":"66820","inReplyTo":null,"subject":"[RFC PATCH 0/2] parse-options: improve early option scanning","fromName":"Azuama Odinaka Maximus","fromEmail":"azuama69@gmail.com","sentAt":"2026-11-10T16:13:11Z","receivedAt":"2026-11-10T16:13:11Z","isPatch":true,"sender":{"key":"azuama69@gmail.com","avatar":null},"body":"This RFC series shares my progress on improving early option\nscanning in rev-list and exploring a reusable scanning API.\n\nThe first patch updates rev-list's early scan to account for\noptions that consume arguments.\n\nThe second patch introduces a generic early option-scanning API\nwith tests, as a step towards a more reusable solution.\n\nThe rev-list patch still uses an initial hardcoded option list,\nwhich I recognise is fragile based on the review feedback.\nI am investigating how to avoid this limitation and how best\nto integrate revision-option argument parsing without triggering\nunwanted parsing side effects.\n\nI would appreciate feedback on the direction of this work,\nparticularly the API design and its integration with revision\noption parsing.\n\nSigned-off-by: Azuama Odinaka Maximus <azuama69@gmail.com>\n\nAzuama Odinaka Maximus (2):\n  rev-list: handle arguments in early option scan\n  parse-options: add early option scanning\n\n builtin/rev-list.c            |  55 +++++++-\n parse-options.c               | 249 ++++++++++++++++++++++++++++++----\n parse-options.h               |  22 +++\n t/helper/test-parse-options.c | 236 ++++++++++++++++++++++++++++++++\n t/t0040-parse-options.sh      |   6 +\n t/t6022-rev-list-missing.sh   |  26 ++++\n 6 files changed, 562 insertions(+), 32 deletions(-)\n\n-- \n2.53.0\n\n\n"},{"id":"558101","messageId":"20261110161314.35213-2-azuama69@gmail.com","threadId":"66820","inReplyTo":"20261110161314.35213-1-azuama69@gmail.com","subject":"[RFC PATCH 1/2] rev-list: handle arguments in early option scan","fromName":"Azuama Odinaka Maximus","fromEmail":"azuama69@gmail.com","sentAt":"2026-11-10T16:13:12Z","receivedAt":"2026-11-10T16:13:12Z","isPatch":true,"sender":{"key":"azuama69@gmail.com","avatar":null},"body":"The early option scan in cmd_rev_list() looks for options that\naffect missing-object handling before setup_revisions() parses the\ncommand line. It must not interpret an option's separate argument\nas another option.\n\nUse parse_long_opt() to recognize revision options that take\nseparate arguments, and skip those arguments. Handle -n and\n--default as well. Stop scanning at --, where subsequent arguments\nare pathspecs.\n\nAdd tests to ensure that --grep and --author arguments beginning\nwith -- are not mistaken for options during the early scan.\n\nSigned-off-by: Azuama Odinaka Maximus <azuama69@gmail.com>\n---\n builtin/rev-list.c          | 55 +++++++++++++++++++++++++++++++++++--\n t/t6022-rev-list-missing.sh | 26 ++++++++++++++++++\n 2 files changed, 78 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/rev-list.c b/builtin/rev-list.c\nindex 071a0e3cb5..4dd646050a 100644\n--- a/builtin/rev-list.c\n+++ b/builtin/rev-list.c\n@@ -744,6 +744,47 @@ static void prepare_maximal_independent(struct rev_info *revs)\n \trevs->no_walk = 1;\n }\n \n+static int rev_list_option_takes_argument(const char **argv)\n+{\n+\tstatic const char * const options[] = {\n+\t\t\"max-count\",\n+\t\t\"max-count-oldest\",\n+\t\t\"skip\",\n+\t\t\"max-age\",\n+\t\t\"since\",\n+\t\t\"since-as-filter\",\n+\t\t\"after\",\n+\t\t\"min-age\",\n+\t\t\"before\",\n+\t\t\"until\",\n+\t\t\"date\",\n+\t\t\"author\",\n+\t\t\"committer\",\n+\t\t\"grep-reflog\",\n+\t\t\"grep\",\n+\t\t\"encoding\",\n+\t\t\"glob\",\n+\t\t\"exclude\",\n+\t\t\"exclude-hidden\",\n+\t};\n+\tconst char *optarg;\n+\tsize_t i;\n+\n+\tif (!argv[1])\n+\t\treturn 0;\n+\n+\tif (!strcmp(argv[0], \"-n\") ||\n+\t    !strcmp(argv[0], \"--default\"))\n+\t\treturn 1;\n+\n+\tfor (i = 0; i < ARRAY_SIZE(options); i++) {\n+\t\tif (parse_long_opt(options[i], argv, &optarg) == 2)\n+\t\t\treturn 1;\n+\t}\n+\n+\treturn 0;\n+}\n+\n int cmd_rev_list(int argc,\n \t\t const char **argv,\n \t\t const char *prefix,\n@@ -789,9 +830,8 @@ int cmd_rev_list(int argc,\n \t * NEEDSWORK: The next loop is utterly broken.  It tries to\n \t * notice an option is used, but without understanding if each\n \t * option takes an argument, which fundamentally would not\n-\t * work.  It would not know \"--grep\n-\t * --exclude-promisor-objects\" is not triggering\n-\t * \"--exclude-promisor-objects\" option, for example.\n+\t * work.  It would not know that \"--author --missing-only\" is not\n+\t * triggering \"--missing-only\" option, for example.\n \t *\n \t * We really need setup_revisions() to have a mechanism to\n \t * allow and disallow some sets of options for different\n@@ -802,6 +842,15 @@ int cmd_rev_list(int argc,\n \t */\n \tfor (i = 1; i < argc; i++) {\n \t\tconst char *arg = argv[i];\n+\n+\t\tif (!strcmp(arg, \"--\"))\n+\t\t\tbreak;\n+\n+\t\tif (rev_list_option_takes_argument(&argv[i])) {\n+\t\t\ti++;\n+\t\t\tcontinue;\n+\t\t}\n+\n \t\tif (!strcmp(arg, \"--exclude-promisor-objects\")) {\n \t\t\trepo->fetch_if_missing = 0;\n \t\t\trevs.exclude_promisor_objects = 1;\ndiff --git a/t/t6022-rev-list-missing.sh b/t/t6022-rev-list-missing.sh\nindex 1bd2c3bc4f..9f995968fd 100755\n--- a/t/t6022-rev-list-missing.sh\n+++ b/t/t6022-rev-list-missing.sh\n@@ -215,6 +215,32 @@ do\n \t'\n done\n \n+test_expect_success \"--grep accepts a pattern beginning with --\" '\n+\ttest_commit --no-tag --missing-only &&\n+\tcommit=$(git rev-parse HEAD) &&\n+\techo \"$commit\" >expect &&\n+\tgit rev-list --grep=--missing-only HEAD >actual &&\n+\ttest_cmp expect actual &&\n+\tgit rev-list --grep --missing-only HEAD >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success \"--grep does not trigger --exclude-promisor-objects in early scan\" '\n+\ttest_commit --no-tag --exclude-promisor-objects &&\n+\tcommit=$(git rev-parse HEAD) &&\n+\techo \"$commit\" >expect &&\n+\tgit rev-list --grep=--exclude-promisor-objects --missing=print HEAD >actual &&\n+\ttest_cmp expect actual &&\n+\tgit rev-list --grep --exclude-promisor-objects --missing=print HEAD >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success \"--author argument is not treated as an option in early scan\" '\n+\ttest_commit --no-tag author-test &&\n+\tgit rev-list --author --missing-only HEAD >actual &&\n+\ttest_must_be_empty actual\n+'\n+\n test_expect_success \"--missing-only requires --missing=print or --missing=print-info\" '\n \ttest_must_fail git rev-list --missing-only --objects HEAD 2>err &&\n \ttest_grep \"requires --missing=print\" err\n-- \n2.53.0\n\n\n"},{"id":"558102","messageId":"20261110161314.35213-3-azuama69@gmail.com","threadId":"66820","inReplyTo":"20261110161314.35213-1-azuama69@gmail.com","subject":"[RFC PATCH 2/2] parse-options: add early option scanning","fromName":"Azuama Odinaka Maximus","fromEmail":"azuama69@gmail.com","sentAt":"2026-11-10T16:13:13Z","receivedAt":"2026-11-10T16:13:13Z","isPatch":true,"sender":{"key":"azuama69@gmail.com","avatar":null},"body":"Signed-off-by: Azuama Odinaka Maximus <azuama69@gmail.com>\n---\n parse-options.c               | 249 ++++++++++++++++++++++++++++++----\n parse-options.h               |  22 +++\n t/helper/test-parse-options.c | 236 ++++++++++++++++++++++++++++++++\n t/t0040-parse-options.sh      |   6 +\n 4 files changed, 484 insertions(+), 29 deletions(-)\n\ndiff --git a/parse-options.c b/parse-options.c\nindex 283a0f1dc2..dbf2dbb04a 100644\n--- a/parse-options.c\n+++ b/parse-options.c\n@@ -423,6 +423,25 @@ static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,\n \treturn -1;\n }\n \n+int parse_options_takes_argument(const struct option *opt)\n+{\n+\tswitch (opt->type) {\n+\tcase OPTION_STRING:\n+\tcase OPTION_FILENAME:\n+\tcase OPTION_INTEGER:\n+\tcase OPTION_UNSIGNED:\n+\tcase OPTION_CALLBACK:\n+\t\tbreak;\n+\tdefault:\n+\t\treturn 0;\n+\t}\n+\n+\tif (opt->flags & (PARSE_OPT_NOARG | PARSE_OPT_OPTARG))\n+\t\treturn 0;\n+\n+\treturn 1;\n+}\n+\n static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,\n \t\t\t\t\t     const struct option *options)\n {\n@@ -515,17 +534,36 @@ static void register_abbrev(struct parse_opt_ctx_t *p,\n \tabbrev->option = option;\n \tabbrev->flags = flags;\n }\n+enum long_opt_lookup {\n+\tLONG_OPT_EXACT,      /* exact match found */\n+\tLONG_OPT_ABBREV,     /* unambiguous abbreviation found */\n+\tLONG_OPT_AMBIGUOUS,  /* more than one candidate abbreviation */\n+\tLONG_OPT_UNKNOWN,    /* nothing matches */\n+};\n \n-static enum parse_opt_result parse_long_opt(\n+/*\n+ * Look up the long option named by `arg` (without its leading \"--\")\n+ * without acting on it. (Neither prints nor dies.)\n+ *\n+ * `found` contains the matching option and flags.\n+ * `other` is meaningful only for LONG_OPT_AMBIGUOUS.\n+ * `stuck` points after '=' when an argument is attached, or is NULL.\n+ */\n+static enum long_opt_lookup lookup_long_opt(\n \tstruct parse_opt_ctx_t *p, const char *arg,\n-\tconst struct option *options)\n+\tconst struct option *options, struct parsed_option *found,\n+\tstruct parsed_option *other, const char **stuck)\n {\n+\tenum opt_parsed flags = OPT_LONG;\n \tconst char *arg_end = strchrnul(arg, '=');\n \tconst char *arg_start = arg;\n-\tenum opt_parsed flags = OPT_LONG;\n \tint arg_starts_with_no_no = 0;\n-\tstruct parsed_option abbrev = { .option = NULL, .flags = OPT_LONG };\n-\tstruct parsed_option ambiguous = { .option = NULL, .flags = OPT_LONG };\n+\n+\tfound->option = NULL;\n+\tfound->flags = OPT_LONG;\n+\tother->option = NULL;\n+\tother->flags = OPT_LONG;\n+\t*stuck = NULL;\n \n \tif (skip_prefix(arg_start, \"no-\", &arg_start)) {\n \t\tif (skip_prefix(arg_start, \"no-\", &arg_start))\n@@ -554,45 +592,75 @@ static enum parse_opt_result parse_long_opt(\n \n \t\tif (skip_prefix(arg_start, long_name, &rest)) {\n \t\t\tif (*rest == '=')\n-\t\t\t\tp->opt = rest + 1;\n+\t\t\t\t*stuck = rest + 1;\n \t\t\telse if (*rest)\n \t\t\t\tcontinue;\n-\t\t\treturn get_value(p, options, flags ^ opt_flags);\n+\t\t\tfound->option = options;\n+\t\t\tfound->flags = flags ^ opt_flags;\n+\t\t\treturn LONG_OPT_EXACT;\n \t\t}\n \n-\t\t/* abbreviated? */\n \t\tif (!strncmp(long_name, arg_start, arg_end - arg_start))\n \t\t\tregister_abbrev(p, options, flags ^ opt_flags,\n-\t\t\t\t\t&abbrev, &ambiguous);\n+\t\t\t\t\tfound, other);\n \n-\t\t/* negated and abbreviated very much? */\n \t\tif (allow_unset && starts_with(\"no-\", arg))\n \t\t\tregister_abbrev(p, options, OPT_UNSET ^ opt_flags,\n-\t\t\t\t\t&abbrev, &ambiguous);\n+\t\t\t\t\tfound, other);\n \t}\n \n-\tif (disallow_abbreviated_options && (ambiguous.option || abbrev.option))\n-\t\tdie(\"disallowed abbreviated or ambiguous option '%.*s'\",\n-\t\t    (int)(arg_end - arg), arg);\n-\n-\tif (ambiguous.option) {\n-\t\terror(_(\"ambiguous option: %s \"\n-\t\t\t\"(could be --%s%s or --%s%s)\"),\n-\t\t\targ,\n-\t\t\t(ambiguous.flags & OPT_UNSET) ?  \"no-\" : \"\",\n-\t\t\tambiguous.option->long_name,\n-\t\t\t(abbrev.flags & OPT_UNSET) ?  \"no-\" : \"\",\n-\t\t\tabbrev.option->long_name);\n-\t\treturn PARSE_OPT_HELP_ERROR;\n-\t}\n-\tif (abbrev.option) {\n+\tif (other->option)\n+\t\treturn LONG_OPT_AMBIGUOUS;\n+\n+\tif (found->option) {\n \t\tif (*arg_end)\n-\t\t\tp->opt = arg_end + 1;\n-\t\treturn get_value(p, abbrev.option, abbrev.flags);\n+\t\t\t*stuck = arg_end + 1;\n+\t\treturn LONG_OPT_ABBREV;\n \t}\n-\treturn PARSE_OPT_UNKNOWN;\n+\n+\treturn LONG_OPT_UNKNOWN;\n+}\n+\n+static enum parse_opt_result parse_long_opt(\n+\tstruct parse_opt_ctx_t *p, const char *arg,\n+\tconst struct option *options)\n+{\n+\tstruct parsed_option found;\n+\tstruct parsed_option other;\n+\tconst char *stuck;\n+\tenum long_opt_lookup result = lookup_long_opt(p, arg, options,\n+\t\t\t\t\t\t      &found, &other, &stuck);\n+\n+\tswitch (result) {\n+\tcase LONG_OPT_UNKNOWN:\n+\t\treturn PARSE_OPT_UNKNOWN;\n+\tcase LONG_OPT_ABBREV:\n+\tcase LONG_OPT_AMBIGUOUS:\n+\t\tif (disallow_abbreviated_options) {\n+\t\t\tconst char *arg_end = strchrnul(arg, '=');\n+\t\t\tdie(\"disallowed abbreviated or ambiguous option '%.*s'\",\n+\t\t\t    (int)(arg_end - arg), arg);\n+\t\t}\n+\t\tif (result == LONG_OPT_AMBIGUOUS) {\n+\t\t\terror(_(\"ambiguous option: %s \"\n+\t\t\t\t\"(could be --%s%s or --%s%s)\"),\n+\t\t\t      arg,\n+\t\t\t      (other.flags & OPT_UNSET) ? \"no-\" : \"\",\n+\t\t\t      other.option->long_name,\n+\t\t\t      (found.flags & OPT_UNSET) ? \"no-\" : \"\",\n+\t\t\t      found.option->long_name);\n+\t\t\treturn PARSE_OPT_HELP_ERROR;\n+\t\t}\n+\t\t/* fallthrough */\n+\tcase LONG_OPT_EXACT:\n+\t\tp->opt = stuck;\n+\t\treturn get_value(p, found.option, found.flags);\n+\t}\n+\n+\tBUG(\"unknown enum long_opt_lookup value '%d'\", result);\n }\n \n+\n static enum parse_opt_result parse_nodash_opt(struct parse_opt_ctx_t *p,\n \t\t\t\t\t      const char *arg,\n \t\t\t\t\t      const struct option *options)\n@@ -1248,6 +1316,129 @@ int parse_options(int argc, const char **argv,\n \treturn parse_options_end(&ctx);\n }\n \n+/*\n+ * Look for `name`, a long option without its leading \"--\", among\n+ * `option`, the way parse_options() would. Return true if it names an\n+ * option exactly, or as an unambiguous abbreviation that is not\n+ * disallowed, with `found` set to that option and its flags, and\n+ * `stuck` to the value stuck to it, if any, or to NULL.\n+ */\n+static bool find_early_scan_option(struct parse_opt_ctx_t *p,\n+\t\t\t\t   const char *name,\n+\t\t\t\t   const struct option *option,\n+\t\t\t\t   bool disallow_abbrev,\n+\t\t\t\t   struct parsed_option *found,\n+\t\t\t\t   const char **stuck)\n+{\n+\tstruct parsed_option other;\n+\tenum long_opt_lookup result = lookup_long_opt(p, name, option,\n+\t\t\t\t\t\t      found, &other, stuck);\n+\n+\tswitch (result) {\n+\tcase LONG_OPT_EXACT:\n+\t\treturn true;\n+\tcase LONG_OPT_ABBREV:\n+\t\treturn !disallow_abbrev;\n+\tcase LONG_OPT_AMBIGUOUS:\n+\tcase LONG_OPT_UNKNOWN:\n+\t\treturn false;\n+\t}\n+\n+\tBUG(\"unknown enum long_opt_lookup value '%d'\", result);\n+}\n+\n+struct early_scan_state {\n+\tstruct parse_opt_ctx_t ctx;\n+\tconst struct option *option;\n+\tenum early_scan_flags flags;\n+\tearly_scan_fn *fn;\n+\tvoid *data;\n+\tbool disallow_abbrev;\n+};\n+\n+/*\n+ * Look at argv[*i]. Return true to go on scanning, with *i on the\n+ * last argument consumed. Return false to stop, with *i set to the\n+ * first argument not consumed.\n+ */\n+static bool early_scan_one_option(struct early_scan_state *s,\n+\t\t\t\t  int argc, const char **argv, int *i)\n+{\n+\tconst char *name;\n+\tconst char *value;\n+\tstruct parsed_option found;\n+\tbool unset;\n+\tint pos = *i;\n+\tconst char *arg = argv[pos];\n+\n+\tif (!strcmp(arg, \"--\") || !strcmp(arg, \"--end-of-options\"))\n+\t\treturn false;\n+\n+\t/* Non-option */\n+\tif (*arg != '-' || !arg[1])\n+\t\treturn !(s->flags & EARLY_SCAN_STOP_AT_NON_OPTION);\n+\n+\t/* Short option */\n+\tif (!skip_prefix(arg, \"--\", &name))\n+\t\treturn false;\n+\n+\tif (!find_early_scan_option(&s->ctx, name, s->option,\n+\t\t\t\t    s->disallow_abbrev, &found, &value))\n+\t\treturn false;\n+\n+\t/* A low-level callback may consume any number of arguments. */\n+\tif (found.option->ll_callback)\n+\t\treturn false;\n+\n+\tunset = !!(found.flags & OPT_UNSET);\n+\n+\tif (value && (unset || (found.option->flags & PARSE_OPT_NOARG)))\n+\t\treturn false;\n+\n+\tif (!value && !unset && parse_options_takes_argument(found.option)) {\n+\t\tif (pos + 1 < argc)\n+\t\t\tvalue = argv[++*i];\n+\t\telse if (found.option->flags & PARSE_OPT_LASTARG_DEFAULT)\n+\t\t\tvalue = (const char *)found.option->defval;\n+\t\telse\n+\t\t\treturn false;\n+\t}\n+\tif ((found.option->flags & PARSE_OPT_EARLY) &&\n+\t    s->fn(found.option, value, unset, pos, s->data)) {\n+\t\t++*i;\n+\t\treturn false;\n+\t}\n+\treturn true;\n+}\n+\n+int early_scan_options(int argc, const char **argv,\n+\t\t       const struct option *option,\n+\t\t       enum early_scan_flags flags,\n+\t\t       early_scan_fn *fn, void *data)\n+{\n+\tstruct early_scan_state s = {\n+\t\t.flags = flags,\n+\t\t.fn = fn,\n+\t\t.data = data,\n+\t\t.disallow_abbrev =\n+\t\t\tgit_env_bool(\"GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS\", 0),\n+\t};\n+\tstruct option *real_option = preprocess_options(&s.ctx, option);\n+\tint i;\n+\n+\ts.option = real_option ? real_option : option;\n+\tparse_options_check(s.option);\n+\n+\tfor (i = 0; i < argc; i++)\n+\t\tif (!early_scan_one_option(&s, argc, argv, &i))\n+\t\t\tbreak;\n+\n+\tfree_preprocessed_options(real_option);\n+\tfree(s.ctx.alias_groups);\n+\treturn i;\n+}\n+\n+\n static int usage_argh(const struct option *opts, FILE *outfile)\n {\n \tconst char *s;\ndiff --git a/parse-options.h b/parse-options.h\nindex f68b51ceca..9c012b87a3 100644\n--- a/parse-options.h\n+++ b/parse-options.h\n@@ -51,6 +51,7 @@ enum parse_opt_option_flags {\n \tPARSE_OPT_NODASH = 1 << 5,\n \tPARSE_OPT_LITERAL_ARGHELP = 1 << 6,\n \tPARSE_OPT_FROM_ALIAS = 1 << 7,\n+\tPARSE_OPT_EARLY = 1 << 8, /* only for early_scan_options() */\n \tPARSE_OPT_NOCOMPLETE = 1 << 9,\n \tPARSE_OPT_COMP_ARG = 1 << 10,\n \tPARSE_OPT_CMDMODE = 1 << 11,\n@@ -423,6 +424,27 @@ int parse_options(int argc, const char **argv, const char *prefix,\n \t\t  const struct option *options,\n \t\t  const char * const usagestr[],\n \t\t  enum parse_opt_flags flags);\n+int parse_options_takes_argument(const struct option *opt);\n+/*\n+ * Called for each argument matching an option marked PARSE_OPT_EARLY.\n+ */\n+typedef int early_scan_fn(const struct option *option, const char *value,\n+\t\t\t  bool unset, int pos, void *data);\n+\n+enum early_scan_flags {\n+\tEARLY_SCAN_STOP_AT_NON_OPTION = 1 << 0,\n+};\n+\n+/*\n+ * Scan argv for options marked PARSE_OPT_EARLY.\n+ * argv is not modified.\n+ *\n+ * Returns the index of the first argument not consumed by the scan.\n+ */\n+int early_scan_options(int argc, const char **argv,\n+\t\t       const struct option *option,\n+\t\t       enum early_scan_flags flags,\n+\t\t       early_scan_fn *fn, void *data);\n \n NORETURN void usage_with_options(const char * const *usagestr,\n \t\t\t\t const struct option *options);\ndiff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c\nindex 950db78673..da66855f28 100644\n--- a/t/helper/test-parse-options.c\n+++ b/t/helper/test-parse-options.c\n@@ -109,8 +109,244 @@ static void show(struct string_list *expect, int *status, const char *fmt, ...)\n \tstrbuf_release(&buf);\n }\n \n+static int test_parse_options_takes_argument(void)\n+{\n+\tint integer = 0;\n+\tint count = 0;\n+\tchar *string = NULL;\n+\tunsigned int unsigned_value = 0;\n+\tchar *filename = NULL;\n+\n+\tstruct option options[] = {\n+\t\tOPT_STRING(0, \"string\", &string, \"value\", \"string option\"),\n+\t\tOPT_INTEGER(0, \"integer\", &integer, \"integer option\"),\n+\t\tOPT_COUNTUP(0, \"count\", &count, \"count option\"),\n+\t\t{\n+\t\t\t.type = OPTION_CALLBACK,\n+\t\t\t.long_name = \"optional\",\n+\t\t\t.flags = PARSE_OPT_OPTARG,\n+\t\t},\n+\t\t{\n+\t\t\t.type = OPTION_CALLBACK,\n+\t\t\t.long_name = \"noarg\",\n+\t\t\t.flags = PARSE_OPT_NOARG,\n+\t\t},\n+\t\t{\n+\t\t\t.type = OPTION_CALLBACK,\n+\t\t\t.long_name = \"callback\",\n+\t\t},\n+\t\tOPT_UNSIGNED(0, \"unsigned\", &unsigned_value, \"unsigned option\"),\n+\t\tOPT_FILENAME(0, \"filename\", &filename, \"filename option\"),\n+\t\tOPT_END(),\n+\t};\n+\n+\tif (!parse_options_takes_argument(&options[0]))\n+\t\treturn error(\"STRING option should take an argument\");\n+\n+\tif (!parse_options_takes_argument(&options[1]))\n+\t\treturn error(\"INTEGER option should take an argument\");\n+\n+\tif (parse_options_takes_argument(&options[2]))\n+\t\treturn error(\"COUNTUP option should not take an argument\");\n+\n+\tif (parse_options_takes_argument(&options[3]))\n+\t\treturn error(\"OPTIONAL argument should not be required\");\n+\n+\tif (parse_options_takes_argument(&options[4]))\n+\t\treturn error(\"NOARG option should not take an argument\");\n+\n+\tif (!parse_options_takes_argument(&options[5]))\n+\t\treturn error(\"required CALLBACK option should take an argument\");\n+\tif (!parse_options_takes_argument(&options[6]))\n+\t\treturn error(\"UNSIGNED option should take an argument\");\n+\n+\tif (!parse_options_takes_argument(&options[7]))\n+\t\treturn error(\"FILENAME option should take an argument\");\n+\n+\treturn 0;\n+}\n+\n+struct early_scan_test_data {\n+\tint calls;\n+\tint pos;\n+\tconst char *value;\n+\tint stop;\n+};\n+\n+static int early_scan_test_callback(const struct option *option UNUSED,\n+\t\t\t\t    const char *value, bool unset UNUSED,\n+\t\t\t\t    int pos, void *data)\n+{\n+\tstruct early_scan_test_data *test_data = data;\n+\n+\ttest_data->calls++;\n+\ttest_data->pos = pos;\n+\ttest_data->value = value;\n+\n+\treturn test_data->stop;\n+}\n+\n+static int test_early_scan_options(void)\n+{\n+\tchar *early_value = NULL;\n+\tchar *ordinary_value = NULL;\n+\tstruct early_scan_test_data data = { 0 };\n+\tstruct option options[] = {\n+\t\tOPT_STRING(0, \"early\", &early_value, \"value\", \"early option\"),\n+\t\tOPT_STRING(0, \"ordinary\", &ordinary_value, \"value\",\n+\t\t\t\"ordinary option\"),\n+\t\tOPT_STRING(0, \"early-default\", &early_value, \"value\",\n+\t\t\t\"early option with default\"),\n+\t\tOPT_END(),\n+\t};\n+\tconst char *args[] = {\n+\t\t\"--ordinary\", \"ignored\",\n+\t\t\"--early=attached\",\n+\t\t\"--early\", \"separate\",\n+\t\t\"--\", \"--early\", \"after-dashdash\",\n+\t};\n+\tconst char *positional_args[] = {\n+\t\t\"positional\", \"--early\", \"value\",\n+\t};\n+\tconst char *end_of_options_args[] = {\n+\t\t\"--end-of-options\", \"--early\", \"ignored\",\n+\t};\n+\tconst char *short_option_args[] = {\n+\t\t\"-e\", \"--early\", \"ignored\",\n+\t};\n+\tconst char *unknown_option_args[] = {\n+\t\t\"--unknown\", \"--early\", \"ignored\",\n+\t};\n+\tconst char *missing_value_args[] = {\n+\t\t\"--early\",\n+\t};\n+\tconst char *lastarg_default_args[] = {\n+\t\t\"--early-default\",\n+\t};\n+\tconst char *callback_stop_args[] = {\n+\t\t\"--early=attached\", \"--early\", \"ignored\",\n+\t};\n+\tconst char *stop_args[] = {\n+\t\t\"--early\", \"separate\", \"--ordinary\",\n+\t};\n+\n+\tint ret;\n+\n+\toptions[0].flags |= PARSE_OPT_EARLY;\n+\toptions[2].flags |= PARSE_OPT_EARLY | PARSE_OPT_LASTARG_DEFAULT;\n+\toptions[2].defval = (intptr_t)\"default\";\n+\n+\tdata.calls = 0;\n+\tdata.stop = 1;\n+\n+\tret = early_scan_options(ARRAY_SIZE(callback_stop_args),\n+\t\tcallback_stop_args, options, 0,\n+\t\tearly_scan_test_callback, &data);\n+\n+\tif (ret != 1)\n+\t\treturn error(\"expected callback to stop scan at argument 1, got %d\",\n+\t\t\tret);\n+\n+\tif (data.calls != 1)\n+\t\treturn error(\"expected callback to run once before stopping\");\n+\tdata.calls = 0;\n+\tdata.stop = 0;\n+\n+\tret = early_scan_options(ARRAY_SIZE(args), args, options, 0,\n+\t\t\t\t early_scan_test_callback, &data);\n+\n+\tif (ret != 5)\n+\t\treturn error(\"expected scan to stop at argument 5, got %d\", ret);\n+\n+\tif (data.calls != 2)\n+\t\treturn error(\"expected 2 early callbacks, got %d\", data.calls);\n+\n+\tif (strcmp(data.value, \"separate\"))\n+\t\treturn error(\"expected separate argument value, got %s\",\n+\t\t\t     data.value);\n+\n+\tif (data.pos != 3)\n+\t\treturn error(\"expected second early option at position 3, got %d\",\n+\t\t\t     data.pos);\n+\n+\tdata.calls = 0;\n+\n+\tret = early_scan_options(ARRAY_SIZE(stop_args), stop_args,\n+\t\toptions, 0,\n+\t\tearly_scan_test_callback, &data);\n+\n+\tif (ret != 2)\n+\t\treturn error(\"expected scan to stop at argument 2, got %d\", ret);\n+\n+\tif (data.calls != 1)\n+\t\treturn error(\"expected 1 early callback before stopping, got %d\",\n+\t\t\t     data.calls);\n+\n+\tif (strcmp(data.value, \"separate\"))\n+\t\treturn error(\"expected separate argument value before stopping\");\n+\n+\tdata.calls = 0;\n+\tdata.stop = 0;\n+\n+\tret = early_scan_options(ARRAY_SIZE(positional_args), positional_args,\n+\t\t\t\t options, EARLY_SCAN_STOP_AT_NON_OPTION,\n+\t\t\t\t early_scan_test_callback, &data);\n+\n+\tif (ret != 0)\n+\t\treturn error(\"expected scan to stop at positional argument\");\n+\n+\tif (data.calls != 0)\n+\t\treturn error(\"early callback should not run after positional argument\");\n+\n+\tdata.calls = 0;\n+\n+\tret = early_scan_options(ARRAY_SIZE(end_of_options_args),\n+\t\t\t\t end_of_options_args, options, 0,\n+\t\t\t\t early_scan_test_callback, &data);\n+\tif (ret != 0 || data.calls)\n+\t\treturn error(\"scanner should stop at --end-of-options\");\n+\n+\tret = early_scan_options(ARRAY_SIZE(short_option_args),\n+\t\t\t\t short_option_args, options, 0,\n+\t\t\t\t early_scan_test_callback, &data);\n+\tif (ret != 0 || data.calls)\n+\t\treturn error(\"scanner should stop at short options\");\n+\n+\tret = early_scan_options(ARRAY_SIZE(unknown_option_args),\n+\t\t\t\t unknown_option_args, options, 0,\n+\t\t\t\t early_scan_test_callback, &data);\n+\tif (ret != 0 || data.calls)\n+\t\treturn error(\"scanner should stop at unknown options\");\n+\n+\tret = early_scan_options(ARRAY_SIZE(missing_value_args),\n+\t\t\t\t missing_value_args, options, 0,\n+\t\t\t\t early_scan_test_callback, &data);\n+\tif (ret != 0 || data.calls)\n+\t\treturn error(\"scanner should stop when a required value is missing\");\n+\n+\tdata.calls = 0;\n+\n+\tret = early_scan_options(ARRAY_SIZE(lastarg_default_args),\n+\t\tlastarg_default_args, options, 0,\n+\t\tearly_scan_test_callback, &data);\n+\n+\tif (ret != ARRAY_SIZE(lastarg_default_args))\n+\t\treturn error(\"expected last-argument default to be consumed\");\n+\n+\tif (data.calls != 1)\n+\t\treturn error(\"expected early callback for default argument\");\n+\n+\tif (strcmp(data.value, \"default\"))\n+\t\treturn error(\"expected default value, got %s\", data.value);\n+\treturn 0;\n+}\n+\n int cmd__parse_options(int argc, const char **argv)\n {\n+\tif (argc == 2 && !strcmp(argv[1], \"--test-takes-argument\"))\n+\t\treturn test_parse_options_takes_argument();\n+\tif (argc == 2 && !strcmp(argv[1], \"--test-early-scan\"))\n+\t\treturn test_early_scan_options();\n \tconst char *prefix = \"prefix/\";\n \tconst char *usage[] = {\n \t\t\"test-tool parse-options <options>\",\ndiff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh\nindex ec55bb1414..1bf802da5b 100755\n--- a/t/t0040-parse-options.sh\n+++ b/t/t0040-parse-options.sh\n@@ -860,5 +860,11 @@ test_expect_success 'u16 limits range' '\n \ttest_must_fail test-tool parse-options --u16 65536 2>err &&\n \ttest_grep \"value 65536 for option .u16. not in range \\[0,65535\\]\" err\n '\n+test_expect_success 'parse_options_takes_argument' '\n+        test-tool parse-options --test-takes-argument\n+'\n+test_expect_success 'early_scan_options' '\n+\ttest-tool parse-options --test-early-scan\n+'\n \n test_done\n-- \n2.53.0\n\n\n"}]}