aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/all.h2
-rw-r--r--include/option.h10
-rw-r--r--include/util/option_parse.h21
-rw-r--r--src/main.cc33
-rw-r--r--src/option.cc32
-rw-r--r--src/util/option_parse.cc52
6 files changed, 135 insertions, 15 deletions
diff --git a/include/all.h b/include/all.h
index 67759cc..8c0d6f9 100644
--- a/include/all.h
+++ b/include/all.h
@@ -5,6 +5,7 @@
#include "util/prompt.h"
#include "util/string.h"
#include "util/color.h"
+#include "util/option_parse.h"
#include "eval.h"
#include "global.h"
#include "ground.h"
@@ -13,3 +14,4 @@
#include "job.h"
#include "parse.h"
#include "path.h"
+#include "option.h"
diff --git a/include/option.h b/include/option.h
new file mode 100644
index 0000000..b089012
--- /dev/null
+++ b/include/option.h
@@ -0,0 +1,10 @@
+#pragma once
+
+#include <string>
+
+namespace ov4::option
+{
+
+void v(std::string s);
+
+}
diff --git a/include/util/option_parse.h b/include/util/option_parse.h
new file mode 100644
index 0000000..c68c549
--- /dev/null
+++ b/include/util/option_parse.h
@@ -0,0 +1,21 @@
+#pragma once
+
+#include <string>
+#include <unordered_map>
+#include <functional>
+#include <utility>
+
+#include "option.h"
+
+namespace ov4
+{
+
+inline const std::unordered_map<std::string, std::function<void(std::string s)>> option_list =
+{
+ {"-v", ov4::option::v},
+};
+
+void exe_option(char **c_argv);
+std::pair<std::string, std::string> option_split(std::string s);
+
+}
diff --git a/src/main.cc b/src/main.cc
index 2132ccb..ed88912 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -36,21 +36,24 @@ int main(int argc, char **argv)
dup2(1, 2);
/* Parse the command line */
- while ((c = getopt(argc, argv, "hvp")) != EOF) {
- switch (c) {
- case 'h': /* print help message */
- usage();
- break;
- case 'v': /* emit additional diagnostic info */
- verbose = true;
- break;
- case 'p': /* don't print a prompt */
- emit_prompt = 0; /* handy for automatic testing */
- break;
- default:
- usage();
- }
- }
+
+ exe_option(argv);
+
+ // while ((c = getopt(argc, argv, "hvp")) != EOF) {
+ // switch (c) {
+ // case 'h': /* print help message */
+ // usage();
+ // break;
+ // case 'v': /* emit additional diagnostic info */
+ // verbose = true;
+ // break;
+ // case 'p': /* don't print a prompt */
+ // emit_prompt = 0; /* handy for automatic testing */
+ // break;
+ // default:
+ // usage();
+ // }
+ // }
diff --git a/src/option.cc b/src/option.cc
new file mode 100644
index 0000000..552d83f
--- /dev/null
+++ b/src/option.cc
@@ -0,0 +1,32 @@
+#include "option.h"
+
+#include <string>
+#include <iostream>
+
+#include "global.h"
+#include "util/io.h"
+
+using namespace std;
+
+namespace ov4::option
+{
+
+void v(string s)
+{
+ if (s == "" || s == "true" || s == "1")
+ {
+ ov4::verbose = true;
+ LOG << "verbose = true" << endl;
+ return;
+ }
+ if (s == "false" || s == "0")
+ {
+ ov4::verbose = false;
+ LOG << "verbose = flase" << endl;
+ return;
+ }
+ LOG << "-v: invalid value" << endl;
+}
+
+}
+
diff --git a/src/util/option_parse.cc b/src/util/option_parse.cc
new file mode 100644
index 0000000..f9185fb
--- /dev/null
+++ b/src/util/option_parse.cc
@@ -0,0 +1,52 @@
+#include "util/option_parse.h"
+
+#include <iostream>
+#include <string>
+#include <vector>
+#include <unordered_map>
+#include <functional>
+#include <utility>
+
+#include "all.h"
+
+using namespace std;
+
+const char option_key_value_split = '=';
+constexpr char option_prefix = '-';
+
+namespace ov4
+{
+
+void exe_option(char **c_argv)
+{
+ vector<string> argv;
+ argv2string(&argv, c_argv);
+
+ for (auto c : argv)
+ {
+ pair<string, string> key_value = option_split(c);
+ if (key_value.first != "")
+ {
+ if (option_list.find(key_value.first) == option_list.end())
+ {
+ cerr << key_value.first << ": option not fund" << endl;
+ continue;
+ }
+ option_list.at(key_value.first)(key_value.second);
+ }
+ }
+}
+
+pair<string, string> option_split(string s)
+{
+ if (s.size() == 0) return make_pair("", "");
+ if (s[0] != option_prefix) return make_pair("", "");
+ size_t pos = s.find(option_key_value_split);
+ if (pos == string::npos) return make_pair(s, "");
+ if (pos == s.size()+1) return make_pair(s.substr(0, pos), "");
+ string key = s.substr(0, pos);
+ string value = s.substr(pos+1);
+ return make_pair(key, value);
+}
+
+}