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