c++/parameter-lister/CLI.cpp
changeset 16 65f51abd5fb8
parent 15 09adb33465e4
child 28 bfef9f34e438
     1.1 --- a/c++/parameter-lister/CLI.cpp	Mon May 04 00:34:27 2015 +0200
     1.2 +++ b/c++/parameter-lister/CLI.cpp	Tue May 05 22:19:24 2015 +0200
     1.3 @@ -1,11 +1,11 @@
     1.4  #include <cstdlib>
     1.5  #include <iostream>
     1.6  #include <vector>
     1.7 +#include <map>
     1.8  #include <algorithm>
     1.9  #include <boost/optional.hpp>
    1.10  #include <memory>
    1.11  
    1.12 -#include "info/globalcode/parameterLister/terminalCodes/TerminalCodes.h"
    1.13  #include "info/globalcode/parameterLister/OutputModule.h"
    1.14  #include "info/globalcode/parameterLister/TerminalOutputModule.h"
    1.15  
    1.16 @@ -16,6 +16,9 @@
    1.17  namespace globalcode {
    1.18  namespace parameterLister {
    1.19  
    1.20 +const string ENV_BASE = "PARAMETER_LISTER";
    1.21 +const string ENV_OUTPUT_MODULE_NAME = ENV_BASE + "_OUTPUT";
    1.22 +
    1.23  boost::optional<string> getenv(const string name) {
    1.24  	const char * value = ::getenv(name.c_str());
    1.25  	if (value == 0) {
    1.26 @@ -29,12 +32,21 @@
    1.27  }
    1.28  }
    1.29  
    1.30 +shared_ptr<OutputModule> chooseOutputModule() {
    1.31 +	boost::optional<string> moduleName = getenv(ENV_OUTPUT_MODULE_NAME);
    1.32 +	if (moduleName.is_initialized()) {
    1.33 +		// TODO: use a map instead of sequence of IFs
    1.34 +		if (moduleName.get() == "terminal") {
    1.35 +			return shared_ptr<OutputModule>(new TerminalOutputModule());
    1.36 +		}
    1.37 +	}
    1.38 +
    1.39 +	return shared_ptr<OutputModule>(new OutputModule());
    1.40 +}
    1.41 +
    1.42  int main(int argc, char* argv[]) {
    1.43 -	terminalCodes::Modifier fgGreen(terminalCodes::FG_GREEN);
    1.44 -	terminalCodes::Modifier fgReset(terminalCodes::FG_DEFAULT);
    1.45  
    1.46 -	cout << "INFO: " << fgGreen << "Parameter lister" << fgReset << " is starting" << endl;
    1.47 -
    1.48 +	/** Load arguments */
    1.49  	string command = argv[0];
    1.50  	vector<string> args;
    1.51  
    1.52 @@ -42,28 +54,8 @@
    1.53  		args.push_back(argv[i]);
    1.54  	}
    1.55  
    1.56 -	/** Load environment variable */
    1.57 -	if (args.size() > 0) {
    1.58 -		string envName = args[0];
    1.59 -		boost::optional<string> outputModule = getenv(envName);
    1.60 +	/** Do formatting */
    1.61 +	shared_ptr<OutputModule> outputModule = chooseOutputModule();
    1.62  
    1.63 -		if (outputModule.is_initialized()) {
    1.64 -			cout << "ENV: " << envName << " = " << outputModule.get() << endl;
    1.65 -		} else {
    1.66 -			cout << "ENV: " << envName << " is missing" << endl;
    1.67 -		}
    1.68 -	}
    1.69 -
    1.70 -	/** Do formatting */
    1.71 -	{
    1.72 -		shared_ptr<OutputModule> om(new OutputModule());
    1.73 -		shared_ptr<TerminalOutputModule> tom(new TerminalOutputModule());
    1.74 -		shared_ptr<OutputModule> o(new TerminalOutputModule());
    1.75 -
    1.76 -		om->process(cout, command, args);
    1.77 -		tom->process(cout, command, args);
    1.78 -		o->process(cout, command, args);
    1.79 -	}
    1.80 -
    1.81 -	return 0;
    1.82 +	return outputModule->process(cout, command, args);
    1.83  }