From 2aba178484ddce8e0bfcce071862bb60fcb8555d Mon Sep 17 00:00:00 2001 From: "David A. Pimentel" Date: Sat, 7 Feb 2026 12:50:27 -0700 Subject: [PATCH 1/2] Implement improved optional optarg parsing for short option. --- examples/long.c | 7 ++++++- optparse.h | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/examples/long.c b/examples/long.c index 00993fa..4ba63b9 100644 --- a/examples/long.c +++ b/examples/long.c @@ -48,7 +48,12 @@ int main(int argc, char **argv) } } - /* Print remaining arguments. */ + printf("amend : %s\n", amend ? "true" : "false"); + printf("brief : %s\n", brief ? "true" : "false"); + printf("color : %s\n", color); + printf("delay : %d\n", delay); + + printf("Print remaining arguments:\n"); while ((arg = optparse_arg(&options))) printf("%s\n", arg); diff --git a/optparse.h b/optparse.h index 9b7c7f8..9a18399 100644 --- a/optparse.h +++ b/optparse.h @@ -256,6 +256,13 @@ optparse(struct optparse *options, const char *optstring) options->optind++; if (option[1]) options->optarg = option + 1; +#ifdef IMPROVED_OPTPARSE_OPTIONAL + else if (next[0] != '-') + { + options->optarg = next; + options->optind++; + } +#endif else options->optarg = 0; return option[0]; From 2b166d5e7f4137cc10c0dc9ade0f563797a91670 Mon Sep 17 00:00:00 2001 From: "David A. Pimentel" Date: Sat, 7 Feb 2026 17:01:26 -0700 Subject: [PATCH 2/2] Do not assume next is defined --- optparse.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optparse.h b/optparse.h index 9a18399..00bcea1 100644 --- a/optparse.h +++ b/optparse.h @@ -257,7 +257,7 @@ optparse(struct optparse *options, const char *optstring) if (option[1]) options->optarg = option + 1; #ifdef IMPROVED_OPTPARSE_OPTIONAL - else if (next[0] != '-') + else if (next && next[0] != '-') { options->optarg = next; options->optind++;