[mlapse-tfl] Implement a command-line argument parser (#6512)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 13 Aug 2019 01:05:28 +0000 (10:05 +0900)
committer이춘석/On-Device Lab(SR)/Staff Engineer/삼성전자 <chunseok.lee@samsung.com>
Tue, 13 Aug 2019 01:05:28 +0000 (10:05 +0900)
This commit implements a command-line argument parse for mlapse-tfl.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
runtimes/contrib/mlapse/tfl/driver.cc

index fc42f05..867a605 100644 (file)
@@ -171,33 +171,56 @@ int entry(const int argc, char **argv)
   int num_thread = -1; // -1 means "auto"
 
   // Read command-line arguments
-  //
-  // The current implementation works only for command-line arguments of the following form:
-  //
-  //  --model <path/to/tflite> --record-count N --thread auto --nnapi --csv-report <path/to/csv>
-  //
-  // TODD Parse command-line arguments
-  auto arg = [argc, argv](uint32_t n) { return std::string{argv[n]}; };
-
-  assert(argc == 10);
-  assert(arg(1) == "--model");
-  {
-    model_path = arg(2);
+  std::map<std::string, std::function<uint32_t(const char *const *)>> opts;
+
+  opts["--model"] = [&model_path, &model_path_initialized](const char *const *tok) {
+    model_path = std::string{tok[0]};
     model_path_initialized = true;
-  }
-  assert(arg(3) == "--record-count");
-  {
-    record_count = std::stoi(arg(4));
-  }
-  assert(arg(5) == "--thread");
-  assert(arg(6) == "auto");
-  assert(arg(7) == "--nnapi");
-  {
+    return 1; // # of arguments
+  };
+
+  opts["--record-count"] = [&record_count](const char *const *tok) {
+    record_count = std::stoi(tok[0]);
+    return 1; // # of arguments
+  };
+
+  opts["--thread"] = [](const char *const *tok) {
+    assert(std::string{tok[0]} == "auto");
+    return 1;
+  };
+
+  opts["--nnapi"] = [&session_type](const char *const *) {
     session_type = SessionType::NNAPI;
-  }
-  assert(arg(8) == "--csv-report");
+    return 0;
+  };
+
+  opts["--csv-report"] = [&observer](const char *const *tok) {
+    observer.append(nnfw::cpp14::make_unique<mlapse::CSVReportGenerator>(tok[0]));
+    return 1;
+  };
+
   {
-    observer.append(nnfw::cpp14::make_unique<mlapse::CSVReportGenerator>(arg(9)));
+    uint32_t offset = 1;
+
+    while (offset < argc)
+    {
+      auto opt = argv[offset];
+
+      auto it = opts.find(opt);
+
+      if (it == opts.end())
+      {
+        std::cout << "INVALID OPTION: " << opt << std::endl;
+        return 255;
+      }
+
+      auto func = it->second;
+
+      auto num_skip = func(argv + offset + 1);
+
+      offset += 1;
+      offset += num_skip;
+    }
   }
 
   // Check arguments