Skip to content

Commit

Permalink
use boost::program_options to parse arguments to main()
Browse files Browse the repository at this point in the history
  • Loading branch information
Elias Karakoulakis committed Jan 10, 2012
1 parent adbdb4a commit 8bb9da5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
72 changes: 48 additions & 24 deletions Main.cpp
Expand Up @@ -35,14 +35,9 @@ for more information on the LGPL, see:
#include <string>
#include <sstream>
#include <iostream>

//~ template <class T>
//~ std::string to_string(T t, std::ios_base & (*f)(std::ios_base&))
//~ {
//~ std::ostringstream oss;
//~ oss << f << t;
//~ return oss.str();
//~ }
// we're using Boost's program_options
#include <boost/program_options.hpp>
namespace po = boost::program_options;

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
Expand Down Expand Up @@ -339,18 +334,54 @@ void send_all_values() {
g_criticalSection.unlock();
}

// ------------------------
// THRIFT MAGIC
// ------------------------
// the Thrift-generated (and manually edited) RemoteManager implementation
// for OpenZWave::Manager class
#include "gen-cpp/RemoteManager_server.cpp"
//

int main(int argc, char **argv) {
// STOMP
stomp_client = new STOMP::PocoStomp("localhost", 61613);
stomp_client->connect();

// -----------------------------------------
int main(int argc, char *argv[]) {
// -----------------------------------------
string stomp_host = "localhost";
int stomp_port = 61613;
string ozw_config_dir = "/home/ekarak/ozw/open-zwave-read-only/config/";
string ozw_port = "/dev/ttyUSB0";

try {
// Declare the supported options.
po::options_description desc("OpenZWave orbiter: Allowed options");
desc.add_options()
("help,?", "print this message")
("stomphost,h", po::value(&stomp_host), "STOMP server hostname (default: localhost)")
("stompport,p", po::value(&stomp_port), "STOMP server port num (default: 61613)")
("ozwconf,c", po::value(&ozw_config_dir), "OpenZWave config/ directory")
("ozwport,o", po::value(&ozw_port), "OpenZWave driver port (e.g. /dev/ttyUSB0)")
;

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);

if (vm.count("help")) {
cout << desc << "\n";
return 1;
}
}
catch (exception& e) {
cerr << "Error parsing options: " << e.what() << "\n";
return 2;
}

// ------------------
try {
// STOMP
stomp_client = new STOMP::PocoStomp(stomp_host, stomp_port);
stomp_client->connect();
} catch (exception& e) {
cerr << "Error connecting to STOMP: " << e.what() << "\n";
return 3;
}

// OpenZWave initialization
//initMutex.lock();
Expand All @@ -359,22 +390,15 @@ int main(int argc, char **argv) {
// The first argument is the path to the config files (where the manufacturer_specific.xml file is located
// The second argument is the path for saved Z-Wave network state and the log file. If you leave it NULL
// the log file will appear in the program's working directory.
Options::Create( "/home/ekarak/ozw/open-zwave-read-only/config/", "", "" );
Options::Create(ozw_config_dir, "", "" );
Options::Get()->Lock();

Manager::Create();

// Add a callback handler to the manager. The second argument is a context that
// is passed to the OnNotification method. If the OnNotification is a method of
// a class, the context would usually be a pointer to that class object, to
// avoid the need for the notification handler to be a static.
// Add a callback handler to the manager.
Manager::Get()->AddWatcher( OnNotification, NULL );

// Add a Z-Wave Driver
// Modify this line to set the correct serial port for your PC interface.

string ozw_port = "/dev/ttyUSB0";

Manager::Get()->AddDriver( ozw_port );
//Manager::Get()->AddDriver( "HID Controller", Driver::ControllerInterface_Hid );

Expand Down
5 changes: 2 additions & 3 deletions Makefile
Expand Up @@ -25,7 +25,6 @@ LDFLAGS := $(DEBUG_LDFLAGS)
# change directories if needed
OPENZWAVE := ../open-zwave
THRIFT := /usr/local/include/thrift
# LIBTHRIFT := ../libmicrohttpd/src/daemon/.libs/libmicrohttpd.a
SMC := /opt/smc

INCLUDES := -I $(OPENZWAVE)/cpp/src -I $(OPENZWAVE)/cpp/src/command_classes/ \
Expand All @@ -40,14 +39,14 @@ GNUTLS := -lgnutls
# for Linux uncomment out next two lines
LIBZWAVE := $(wildcard $(OPENZWAVE)/cpp/lib/linux/*.a)
LIBUSB := -ludev
LIBPOCO := -lPocoNet -lboost_thread
LIBPOCO := -lPocoNet -lboost_thread -lboost_program_options
LIBTHRIFT := -lthrift

# for Mac OS X comment out above 2 lines and uncomment next 2 lines
#LIBZWAVE := $(wildcard $(OPENZWAVE)/cpp/lib/mac/*.a)
#LIBUSB := -framework IOKit -framework CoreFoundation

LIBS := $(LIBZWAVE) $(GNUTLS) $(LIBMICROHTTPD) $(LIBTHRIFT) $(LIBUSB) $(LIBPOCO)
LIBS := $(LIBZWAVE) $(GNUTLS) $(LIBTHRIFT) $(LIBUSB) $(LIBPOCO)

%.o : %.cpp
$(CXX) $(CFLAGS) $(INCLUDES) -o $@ $<
Expand Down
Binary file modified main
Binary file not shown.

0 comments on commit 8bb9da5

Please sign in to comment.