c++/parameter-lister/CLI.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 03 May 2015 23:41:43 +0200
changeset 10 145b45ef7751
parent 9 598a575ae57f
child 11 870b868b6b57
permissions -rw-r--r--
shared_ptr
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@10
    28
void process(shared_ptr<OutputModule> om) {
franta-hg@10
    29
	om->process(cout);
franta-hg@10
    30
}
franta-hg@5
    31
franta-hg@5
    32
}
franta-hg@5
    33
}
franta-hg@5
    34
}
franta-hg@5
    35
franta-hg@3
    36
int main(int argc, char* argv[]) {
franta-hg@1
    37
	terminalCodes::Modifier fgGreen(terminalCodes::FG_GREEN);
franta-hg@1
    38
	terminalCodes::Modifier fgReset(terminalCodes::FG_DEFAULT);
franta-hg@4
    39
franta-hg@3
    40
	cout << "INFO: " << fgGreen << "Parameter lister" << fgReset << " is starting" << endl;
franta-hg@1
    41
franta-hg@3
    42
	string command = argv[0];
franta-hg@3
    43
	vector<string> args;
franta-hg@3
    44
franta-hg@3
    45
	for (int i = 1; i < argc; i++) {
franta-hg@3
    46
		args.push_back(argv[i]);
franta-hg@3
    47
	}
franta-hg@3
    48
franta-hg@3
    49
	for_each(args.begin(), args.end(), [command, fgGreen, fgReset](string s) {
franta-hg@3
    50
		cout << fgGreen << command << fgReset << ": " << s << endl;
franta-hg@3
    51
	});
franta-hg@3
    52
franta-hg@3
    53
franta-hg@4
    54
	for (int i = 0; i < args.size(); i++) {
franta-hg@4
    55
		string s = args[i];
franta-hg@4
    56
		cout << i + 1 << ":" << s.length() << " = \"" << s << "\"" << endl;
franta-hg@4
    57
	}
franta-hg@4
    58
franta-hg@5
    59
	{
franta-hg@5
    60
		string envName = args[0];
franta-hg@5
    61
		boost::optional<string> outputModule = getenv(envName);
franta-hg@4
    62
franta-hg@5
    63
		if (outputModule.is_initialized()) {
franta-hg@9
    64
			cout << "ENV: " << envName << " = " << outputModule.get() << endl;
franta-hg@5
    65
		} else {
franta-hg@9
    66
			cout << "ENV: " << envName << " is missing" << endl;
franta-hg@5
    67
		}
franta-hg@5
    68
	}
franta-hg@10
    69
franta-hg@7
    70
	{
franta-hg@10
    71
		shared_ptr<OutputModule> om(new OutputModule());
franta-hg@10
    72
		shared_ptr<TerminalOutputModule> tom(new TerminalOutputModule());
franta-hg@10
    73
		shared_ptr<OutputModule> o(new TerminalOutputModule());
franta-hg@10
    74
franta-hg@10
    75
		process(om);
franta-hg@10
    76
		process(tom);
franta-hg@10
    77
		process(o);
franta-hg@7
    78
	}
franta-hg@3
    79
franta-hg@0
    80
	return 0;
franta-hg@10
    81
}