c++/parameter-lister/CLI.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 04 May 2015 00:12:33 +0200
changeset 11 870b868b6b57
parent 10 145b45ef7751
child 15 09adb33465e4
permissions -rw-r--r--
move formatting logic to output modules
franta-hg@0
     1
#include <cstdlib>
franta-hg@1
     2
#include <iostream>
franta-hg@3
     3
#include <vector>
franta-hg@3
     4
#include <algorithm>
franta-hg@5
     5
#include <boost/optional.hpp>
franta-hg@10
     6
#include <memory>
franta-hg@3
     7
franta-hg@5
     8
#include "info/globalcode/parameterLister/terminalCodes/TerminalCodes.h"
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@5
    19
boost::optional<string> getenv(const string name) {
franta-hg@5
    20
	const char * value = ::getenv(name.c_str());
franta-hg@5
    21
	if (value == 0) {
franta-hg@5
    22
		return boost::optional<string>();
franta-hg@5
    23
	} else {
franta-hg@5
    24
		return boost::optional<string>(value);
franta-hg@5
    25
	}
franta-hg@5
    26
}
franta-hg@5
    27
franta-hg@5
    28
}
franta-hg@5
    29
}
franta-hg@5
    30
}
franta-hg@5
    31
franta-hg@3
    32
int main(int argc, char* argv[]) {
franta-hg@1
    33
	terminalCodes::Modifier fgGreen(terminalCodes::FG_GREEN);
franta-hg@1
    34
	terminalCodes::Modifier fgReset(terminalCodes::FG_DEFAULT);
franta-hg@4
    35
franta-hg@3
    36
	cout << "INFO: " << fgGreen << "Parameter lister" << fgReset << " is starting" << endl;
franta-hg@1
    37
franta-hg@3
    38
	string command = argv[0];
franta-hg@3
    39
	vector<string> args;
franta-hg@3
    40
franta-hg@3
    41
	for (int i = 1; i < argc; i++) {
franta-hg@3
    42
		args.push_back(argv[i]);
franta-hg@3
    43
	}
franta-hg@3
    44
franta-hg@11
    45
	/** Load environment variable */
franta-hg@5
    46
	{
franta-hg@5
    47
		string envName = args[0];
franta-hg@5
    48
		boost::optional<string> outputModule = getenv(envName);
franta-hg@4
    49
franta-hg@5
    50
		if (outputModule.is_initialized()) {
franta-hg@9
    51
			cout << "ENV: " << envName << " = " << outputModule.get() << endl;
franta-hg@5
    52
		} else {
franta-hg@9
    53
			cout << "ENV: " << envName << " is missing" << endl;
franta-hg@5
    54
		}
franta-hg@5
    55
	}
franta-hg@10
    56
franta-hg@11
    57
	/** Do formatting */
franta-hg@7
    58
	{
franta-hg@10
    59
		shared_ptr<OutputModule> om(new OutputModule());
franta-hg@10
    60
		shared_ptr<TerminalOutputModule> tom(new TerminalOutputModule());
franta-hg@10
    61
		shared_ptr<OutputModule> o(new TerminalOutputModule());
franta-hg@10
    62
franta-hg@11
    63
		om->process(cout, command, args);
franta-hg@11
    64
		tom->process(cout, command, args);
franta-hg@11
    65
		o->process(cout, command, args);
franta-hg@7
    66
	}
franta-hg@3
    67
franta-hg@0
    68
	return 0;
franta-hg@10
    69
}