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