c++/parameter-lister/CLI.cpp
author František Kučera <franta-hg@frantovo.cz>
Tue, 05 May 2015 22:19:24 +0200
changeset 16 65f51abd5fb8
parent 15 09adb33465e4
child 28 bfef9f34e438
permissions -rw-r--r--
chooseOutputModule
     1 #include <cstdlib>
     2 #include <iostream>
     3 #include <vector>
     4 #include <map>
     5 #include <algorithm>
     6 #include <boost/optional.hpp>
     7 #include <memory>
     8 
     9 #include "info/globalcode/parameterLister/OutputModule.h"
    10 #include "info/globalcode/parameterLister/TerminalOutputModule.h"
    11 
    12 using namespace std;
    13 using namespace info::globalcode::parameterLister;
    14 
    15 namespace info {
    16 namespace globalcode {
    17 namespace parameterLister {
    18 
    19 const string ENV_BASE = "PARAMETER_LISTER";
    20 const string ENV_OUTPUT_MODULE_NAME = ENV_BASE + "_OUTPUT";
    21 
    22 boost::optional<string> getenv(const string name) {
    23 	const char * value = ::getenv(name.c_str());
    24 	if (value == 0) {
    25 		return boost::optional<string>();
    26 	} else {
    27 		return boost::optional<string>(value);
    28 	}
    29 }
    30 
    31 }
    32 }
    33 }
    34 
    35 shared_ptr<OutputModule> chooseOutputModule() {
    36 	boost::optional<string> moduleName = getenv(ENV_OUTPUT_MODULE_NAME);
    37 	if (moduleName.is_initialized()) {
    38 		// TODO: use a map instead of sequence of IFs
    39 		if (moduleName.get() == "terminal") {
    40 			return shared_ptr<OutputModule>(new TerminalOutputModule());
    41 		}
    42 	}
    43 
    44 	return shared_ptr<OutputModule>(new OutputModule());
    45 }
    46 
    47 int main(int argc, char* argv[]) {
    48 
    49 	/** Load arguments */
    50 	string command = argv[0];
    51 	vector<string> args;
    52 
    53 	for (int i = 1; i < argc; i++) {
    54 		args.push_back(argv[i]);
    55 	}
    56 
    57 	/** Do formatting */
    58 	shared_ptr<OutputModule> outputModule = chooseOutputModule();
    59 
    60 	return outputModule->process(cout, command, args);
    61 }