Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stop parsing the command line when encountering '--'
authorMartin Quinson <martin.quinson@loria.fr>
Fri, 13 Jul 2018 18:20:07 +0000 (20:20 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Fri, 13 Jul 2018 18:53:15 +0000 (20:53 +0200)
And also, add a test for this feature (fix #287)

ChangeLog
src/simgrid/sg_config.cpp
src/xbt/log.c
teshsuite/xbt/CMakeLists.txt
teshsuite/xbt/cmdline/cmdline.c [new file with mode: 0644]
teshsuite/xbt/cmdline/cmdline.tesh [new file with mode: 0644]

index 988d5f2..96abe8a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -11,6 +11,7 @@ Fixed bugs:
  - #282: TIT convertor erase traces when using absolute path in the trace list file
  - #285: segfault when a s4u actor kills itself with msg process tracing activated
  - #286: Pajé traces are not exposing the number of cores
+ - #287: Command-line parsing should stop when encountering '--'
 
 ----------------------------------------------------------------------------
 
index 415a9c7..2367331 100644 (file)
@@ -41,18 +41,22 @@ static void sg_config_cmd_line(int *argc, char **argv)
   int shall_exit = 0;
   int i;
   int j;
+  bool parse_args = true; // Stop parsing the parameters once we found '--'
 
   for (j = i = 1; i < *argc; i++) {
-    if (not strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
+    if (not strcmp("--", argv[i])) {
+      parse_args = false;
+      // Remove that '--' from the arguments
+    } else if (parse_args && not strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
       char *opt = strchr(argv[i], '=');
       opt++;
 
       simgrid::config::set_parse(opt);
       XBT_DEBUG("Did apply '%s' as config setting", opt);
-    } else if (not strcmp(argv[i], "--version")) {
+    } else if (parse_args && not strcmp(argv[i], "--version")) {
       printf("%s\n", SIMGRID_VERSION_STRING);
       shall_exit = 1;
-    } else if (not strcmp(argv[i], "--cfg-help") || not strcmp(argv[i], "--help")) {
+    } else if (parse_args && (not strcmp(argv[i], "--cfg-help") || not strcmp(argv[i], "--help"))) {
       printf("Description of the configuration accepted by this simulator:\n");
       simgrid::config::help();
       printf(
@@ -70,12 +74,12 @@ static void sg_config_cmd_line(int *argc, char **argv)
           "\n"
         );
       shall_exit = 1;
-    } else if (not strcmp(argv[i], "--help-aliases")) {
+    } else if (parse_args && not strcmp(argv[i], "--help-aliases")) {
       printf("Here is a list of all deprecated option names, with their replacement.\n");
       simgrid::config::show_aliases();
       printf("Please consider using the recent names\n");
       shall_exit = 1;
-    } else if (not strcmp(argv[i], "--help-models")) {
+    } else if (parse_args && not strcmp(argv[i], "--help-models")) {
       model_help("host", surf_host_model_description);
       printf("\n");
       model_help("CPU", surf_cpu_model_description);
@@ -88,7 +92,7 @@ static void sg_config_cmd_line(int *argc, char **argv)
                surf_optimization_mode_description[k].description);
       printf("Both network and CPU models have 'Lazy' as default optimization level\n\n");
       shall_exit = 1;
-    } else if (not strcmp(argv[i], "--help-tracing")) {
+    } else if (parse_args && not strcmp(argv[i], "--help-tracing")) {
       TRACE_help();
       shall_exit = 1;
     } else {
index 3b665d9..fe6d5d8 100644 (file)
@@ -109,17 +109,21 @@ void xbt_log_init(int *argc, char **argv)
 {
   unsigned help_requested = 0;  /* 1: logs; 2: categories */
   int j                   = 1;
+  int parse_args          = 1; // Stop parsing the parameters once we found '--'
 
   /* Set logs and init log submodule */
   for (int i = 1; i < *argc; i++) {
-    if (!strncmp(argv[i], "--log=", strlen("--log="))) {
+    if (!strcmp("--", argv[i])) {
+      parse_args = 0;
+      argv[j++]  = argv[i]; // Keep the '--' for sg_config
+    } else if (parse_args && !strncmp(argv[i], "--log=", strlen("--log="))) {
       char* opt = strchr(argv[i], '=');
       opt++;
       xbt_log_control_set(opt);
       XBT_DEBUG("Did apply '%s' as log setting", opt);
-    } else if (!strcmp(argv[i], "--help-logs")) {
+    } else if (parse_args && !strcmp(argv[i], "--help-logs")) {
       help_requested |= 1U;
-    } else if (!strcmp(argv[i], "--help-log-categories")) {
+    } else if (parse_args && !strcmp(argv[i], "--help-log-categories")) {
       help_requested |= 2U;
     } else {
       argv[j++] = argv[i];
index bd505c8..e59f345 100644 (file)
@@ -1,4 +1,4 @@
-foreach(x log_large log_usage mallocator parallel_log_crashtest)
+foreach(x cmdline log_large log_usage mallocator parallel_log_crashtest)
   add_executable       (${x}  ${x}/${x}.c)
   target_link_libraries(${x}  simgrid)
   set_target_properties(${x}  PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${x})
@@ -29,7 +29,7 @@ set(tesh_files    ${tesh_files}     ${CMAKE_CURRENT_SOURCE_DIR}/log_usage/log_us
                                     ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc/mmalloc_32.tesh          PARENT_SCOPE)
 set(teshsuite_src ${teshsuite_src}  ${CMAKE_CURRENT_SOURCE_DIR}/mmalloc/mmalloc_test.cpp           PARENT_SCOPE)
 
-foreach(x log_large parallel_log_crashtest parmap_test) #mallocator parmap_bench
+foreach(x cmdline log_large parallel_log_crashtest parmap_test) #mallocator parmap_bench
   ADD_TESH(tesh-xbt-${x} --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/xbt/${x} --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/${x} ${x}.tesh)
 endforeach()
 
diff --git a/teshsuite/xbt/cmdline/cmdline.c b/teshsuite/xbt/cmdline/cmdline.c
new file mode 100644 (file)
index 0000000..bda54bc
--- /dev/null
@@ -0,0 +1,19 @@
+/* Copyright (c) 2018. The SimGrid Team. All rights reserved.               */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#include "simgrid/engine.h"
+#include <xbt.h>
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to this test");
+
+int main(int argc, char** argv)
+{
+  simgrid_init(&argc, argv);
+
+  for (int i = 1; i < argc; i++)
+    XBT_INFO("argv[%d]=%s", i, argv[i]);
+
+  return 0;
+}
diff --git a/teshsuite/xbt/cmdline/cmdline.tesh b/teshsuite/xbt/cmdline/cmdline.tesh
new file mode 100644 (file)
index 0000000..4145b93
--- /dev/null
@@ -0,0 +1,5 @@
+
+p Test that -- correctly stops the command line parsing
+
+$ ${bindir:=.}/cmdline -- --help
+> [0.000000] [test/INFO] argv[1]=--help