[03/17] support: Add optstring support

Message ID 1496956411-25594-4-git-send-email-adhemerval.zanella@linaro.org
State New
Headers show
Series
  • posix: glob fixes and refactor
Related show

Commit Message

Adhemerval Zanella June 8, 2017, 9:13 p.m.
This patch adds an option to test to add small command line option
through CMDLINE_OPTSTRING define.  For instance:

  #define CMDLINE_OPTSTRING "vd"

  static void
  cmdline_process_function (int c)
  {
    switch (c):
      'v':
        /* process '-v' option.  */
      break;
      'd':
        /* process '-d' option.  */
      break;
  }
  #define CMDLINE_PROCESS cmdline_process_function

It will add both '-v' and '-d' along with already default long options.

	* support/support_test_main.c (support_test_main):  Use optstring
	member for option string in getopt_long.
	* support/test-driver.c: Add comment about CMDLINE_OPTSTRING.
	(CMDLINE_OPTSTRING): New define.
	* support/test-driver.h (test_config): Add optstring member.
---
 support/support_test_main.c | 3 ++-
 support/test-driver.c       | 9 +++++++++
 support/test-driver.h       | 1 +
 3 files changed, 12 insertions(+), 1 deletion(-)

-- 
2.7.4

Comments

Florian Weimer June 13, 2017, 9:22 a.m. | #1
On 06/08/2017 11:13 PM, Adhemerval Zanella wrote:
> +#ifdef CMDLINE_OPTSTRING

> +  test_config.optstring = CMDLINE_OPTSTRING;

> +#else

> +  test_config.optstring = "+";


Why not "+" CMDLINE_OPTSTRING, for consistency?

Otherwise, this looks good to me.

Thanks,
Florian
Adhemerval Zanella June 13, 2017, 11:55 a.m. | #2
On 13/06/2017 06:22, Florian Weimer wrote:
> On 06/08/2017 11:13 PM, Adhemerval Zanella wrote:

>> +#ifdef CMDLINE_OPTSTRING

>> +  test_config.optstring = CMDLINE_OPTSTRING;

>> +#else

>> +  test_config.optstring = "+";

> 

> Why not "+" CMDLINE_OPTSTRING, for consistency?

> 

> Otherwise, this looks good to me.

> 

> Thanks,

> Florian

> 


I will add this change.

Patch

diff --git a/support/support_test_main.c b/support/support_test_main.c
index 914d64f..3c411a4 100644
--- a/support/support_test_main.c
+++ b/support/support_test_main.c
@@ -211,7 +211,8 @@  support_test_main (int argc, char **argv, const struct test_config *config)
         mallopt (M_PERTURB, 42);
     }
 
-  while ((opt = getopt_long (argc, argv, "+", options, NULL)) != -1)
+  while ((opt = getopt_long (argc, argv, config->optstring, options, NULL))
+	 != -1)
     switch (opt)
       {
       case '?':
diff --git a/support/test-driver.c b/support/test-driver.c
index 482066d..f5a2388 100644
--- a/support/test-driver.c
+++ b/support/test-driver.c
@@ -93,6 +93,10 @@ 
    has this type:
 
      void CMDLINE_PROCESS (int);
+
+   If the program also to process custom default short command line
+   argument (similar to getopt) it must define CMDLINE_OPTSTRING
+   with the expected options (for instance "vb").
 */
 
 #include <support/test-driver.h>
@@ -151,6 +155,11 @@  main (int argc, char **argv)
 #ifdef CMDLINE_PROCESS
   test_config.cmdline_function = CMDLINE_PROCESS;
 #endif
+#ifdef CMDLINE_OPTSTRING
+  test_config.optstring = CMDLINE_OPTSTRING;
+#else
+  test_config.optstring = "+";
+#endif
 
   return support_test_main (argc, argv, &test_config);
 }
diff --git a/support/test-driver.h b/support/test-driver.h
index af1971a..a8fe9c3 100644
--- a/support/test-driver.h
+++ b/support/test-driver.h
@@ -35,6 +35,7 @@  struct test_config
   int expected_status;   /* Expected exit status.  */
   int expected_signal;   /* If non-zero, expect termination by signal.  */
   char no_mallopt;       /* Boolean flag to disable mallopt.  */
+  const char *optstring; /* Short command line options.  */
 };
 
 enum