This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit e50832622fe4749441fe9cb5da440e4753508a54
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Nov 8 10:54:43 2026 +0100

    testing/ostest: Test "--" and "-" in getopt().
    
    POSIX: "--" ends the options (getopt() returns -1 and optind is the
    argument after it), and "-" is an operand (getopt() returns -1 and
    optind is the "-").  Add these cases for getopt(), getopt_long() and
    getopt_long_only(), and check optind after each of them.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/ostest/getopt.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)

diff --git a/testing/ostest/getopt.c b/testing/ostest/getopt.c
index 8406f08fd..1c58a5b8c 100644
--- a/testing/ostest/getopt.c
+++ b/testing/ostest/getopt.c
@@ -402,6 +402,15 @@ static int getopt_longonly_test(int noptions, int argc, 
FAR char **argv,
   return OK;
 }
 
+static void getopt_optind_test(int expected)
+{
+  if (optind != expected)
+    {
+      printf("ERROR: optind=%d (expected %d)\n", optind, expected);
+      ASSERT(false);
+    }
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -681,5 +690,78 @@ int getopt_test(void)
 
   getopt_longonly_test(4, 8, argv, g_optstring, long_options, NULL,
                        results);
+
+  printf("getopt():  \"--\" ends the options\n");
+
+  argv[0] = NULL;
+  argv[1] = "-a";
+  argv[2] = "--";
+  argv[3] = "-b";
+  argv[4] = "NoOption";
+  argv[5] = NULL;
+
+  SHORT_RESULT_A(0);
+
+  getopt_short_test(1, 5, argv, g_optstring, results);
+  getopt_optind_test(3);
+
+  printf("getopt():  \"-\" is an operand\n");
+
+  argv[0] = NULL;
+  argv[1] = "-a";
+  argv[2] = "-";
+  argv[3] = "-b";
+  argv[4] = NULL;
+
+  SHORT_RESULT_A(0);
+
+  getopt_short_test(1, 4, argv, g_optstring, results);
+  getopt_optind_test(2);
+
+  printf("getopt_long():  \"--\" ends the options\n");
+
+  argv[0] = NULL;
+  argv[1] = "--OptionA";
+  argv[2] = "--";
+  argv[3] = "--OptionB";
+  argv[4] = NULL;
+
+  LONG_OPTION_A(0);
+  LONG_OPTION_B(1);
+  LONG_OPTION_END(2)
+
+  LONG_RESULT_A(0);
+
+  getopt_long_test(1, 4, argv, g_optstring, long_options, NULL,
+                   results);
+  getopt_optind_test(3);
+
+  printf("getopt_long():  \"-\" is an operand\n");
+
+  argv[2] = "-";
+
+  getopt_long_test(1, 4, argv, g_optstring, long_options, NULL,
+                   results);
+  getopt_optind_test(2);
+
+  printf("getopt_long_only():  \"--\" ends the options\n");
+
+  argv[0] = NULL;
+  argv[1] = "-OptionA";
+  argv[2] = "--";
+  argv[3] = "-b";
+  argv[4] = NULL;
+
+  getopt_longonly_test(1, 4, argv, g_optstring, long_options, NULL,
+                       results);
+  getopt_optind_test(3);
+
+  printf("getopt_long_only():  \"-\" is an operand\n");
+
+  argv[2] = "-";
+
+  getopt_longonly_test(1, 4, argv, g_optstring, long_options, NULL,
+                       results);
+  getopt_optind_test(2);
   return OK;
 }

Reply via email to