Commits

Anonymous committed 3ea8c74

proper options parsing

Comments (0)

Files changed (3)

 src/tcp_client.d \
 src/tinymq.d 
 
-LIBS := -lboost_thread-xgcc40-mt -lboost_system-xgcc40-mt
-CFLAGS := -I/usr/local/include/boost-1_38 -O0 -g3 -Wall 
+LIBS := -lboost_thread-xgcc40-mt -lboost_system-xgcc40-mt -lboost_program_options-xgcc40-mt
+CFLAGS := -I/usr/local/include/boost-1_38 -O0 -gdwarf-2 -Wall 
 all: tinymq
 
 tinymq: $(OBJS)
-	g++  -o "tinymq" $(OBJS) $(USER_OBJS) $(LIBS)
+	g++ -o "tinymq" $(OBJS) $(USER_OBJS) $(LIBS)
 
 src/%.o: src/%.cpp
 	@echo 'Building file: $<'
         return msg_id
 
 def test():
-    c = tinymq_client("127.0.0.1", 8787)
+    c = tinymq_client("127.0.0.1", 5673)
     c.connect()
     for i in range(0, 1000):
         c.put("test%s" % i)
 
 #include <iostream>
 #include <string>
+#include <boost/program_options.hpp>
 #include "server.hpp"
 
+namespace po = boost::program_options;
+
 int main(int argc, char* argv[]) {
-
-//    try {
-        if (argc != 4) {
-            std::cerr << "Usage: http_server <address> <port> <workdir>\n";
-            std::cerr << "  For IPv4, try:\n";
-            std::cerr << "    receiver 0.0.0.0 80 1 .\n";
-            std::cerr << "  For IPv6, try:\n";
-            std::cerr << "    receiver 0::0 80 1 .\n";
+    std::string ip;
+    std::string port;
+    std::string datadir;
+    po::options_description desc("usage: tinymq [options]");
+    desc.add_options()
+        ("help", "help message")
+        ("ip,i", po::value<std::string>(&ip)->default_value("0.0.0.0"),
+         "ip address to bind to")
+        ("port,p", po::value<std::string>(&port)->default_value("5673"),
+         "port number to listen on")
+        ("dir,d", po::value<std::string>(&datadir)->default_value("."),
+         "data directory");
+    po::variables_map vm;
+    try {
+        po::store(po::parse_command_line(argc, argv, desc), vm);
+        po::notify(vm);
+        if (vm.count("help")) {
+            std::cout << desc << "\n";
             return 1;
         }
-        tinymq::server s(argv[1], argv[2], argv[3]);
+    }
+    catch (po::error& e) {
+        std::cerr << e.what() << "\n\n";
+        std::cerr << desc << "\n"; 
+        return 1;
+    }
+    try {
+        tinymq::server s(ip, port, datadir);
         s.run();
         s.stop();
-//    }
-   // catch (std::exception& e) {
-//        std::cerr << "exception: " << e.what() << "\n";
-//    }
-    return 0;
+        return 0;
+    }
+    catch (std::exception& e) {
+        std::cerr << "exception: " << e.what() << "\n";
+        return 1;
+    }
 }