c++/parameter-lister/CLI.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 11 Sep 2016 19:52:26 +0200
changeset 28 bfef9f34e438
parent 16 65f51abd5fb8
permissions -rw-r--r--
license: GNU GPL v3
     1 /**
     2  * parameter-lister
     3  * Copyright © 2015 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 #include <cstdlib>
    19 #include <iostream>
    20 #include <vector>
    21 #include <map>
    22 #include <algorithm>
    23 #include <boost/optional.hpp>
    24 #include <memory>
    25 
    26 #include "info/globalcode/parameterLister/OutputModule.h"
    27 #include "info/globalcode/parameterLister/TerminalOutputModule.h"
    28 
    29 using namespace std;
    30 using namespace info::globalcode::parameterLister;
    31 
    32 namespace info {
    33 namespace globalcode {
    34 namespace parameterLister {
    35 
    36 const string ENV_BASE = "PARAMETER_LISTER";
    37 const string ENV_OUTPUT_MODULE_NAME = ENV_BASE + "_OUTPUT";
    38 
    39 boost::optional<string> getenv(const string name) {
    40 	const char * value = ::getenv(name.c_str());
    41 	if (value == 0) {
    42 		return boost::optional<string>();
    43 	} else {
    44 		return boost::optional<string>(value);
    45 	}
    46 }
    47 
    48 }
    49 }
    50 }
    51 
    52 shared_ptr<OutputModule> chooseOutputModule() {
    53 	boost::optional<string> moduleName = getenv(ENV_OUTPUT_MODULE_NAME);
    54 	if (moduleName.is_initialized()) {
    55 		// TODO: use a map instead of sequence of IFs
    56 		if (moduleName.get() == "terminal") {
    57 			return shared_ptr<OutputModule>(new TerminalOutputModule());
    58 		}
    59 	}
    60 
    61 	return shared_ptr<OutputModule>(new OutputModule());
    62 }
    63 
    64 int main(int argc, char* argv[]) {
    65 
    66 	/** Load arguments */
    67 	string command = argv[0];
    68 	vector<string> args;
    69 
    70 	for (int i = 1; i < argc; i++) {
    71 		args.push_back(argv[i]);
    72 	}
    73 
    74 	/** Do formatting */
    75 	shared_ptr<OutputModule> outputModule = chooseOutputModule();
    76 
    77 	return outputModule->process(cout, command, args);
    78 }