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
     1 #include <cstdlib>
     2 #include <iostream>
     3 #include <vector>
     4 #include <algorithm>
     5 #include <boost/optional.hpp>
     6 #include <memory>
     7 
     8 #include "info/globalcode/parameterLister/terminalCodes/TerminalCodes.h"
     9 #include "info/globalcode/parameterLister/OutputModule.h"
    10 #include "info/globalcode/parameterLister/TerminalOutputModule.h"
    11 
    12 using namespace std;
    13 using namespace info::globalcode::parameterLister;
    14 
    15 namespace info {
    16 namespace globalcode {
    17 namespace parameterLister {
    18 
    19 boost::optional<string> getenv(const string name) {
    20 	const char * value = ::getenv(name.c_str());
    21 	if (value == 0) {
    22 		return boost::optional<string>();
    23 	} else {
    24 		return boost::optional<string>(value);
    25 	}
    26 }
    27 
    28 void process(shared_ptr<OutputModule> om) {
    29 	om->process(cout);
    30 }
    31 
    32 }
    33 }
    34 }
    35 
    36 int main(int argc, char* argv[]) {
    37 	terminalCodes::Modifier fgGreen(terminalCodes::FG_GREEN);
    38 	terminalCodes::Modifier fgReset(terminalCodes::FG_DEFAULT);
    39 
    40 	cout << "INFO: " << fgGreen << "Parameter lister" << fgReset << " is starting" << endl;
    41 
    42 	string command = argv[0];
    43 	vector<string> args;
    44 
    45 	for (int i = 1; i < argc; i++) {
    46 		args.push_back(argv[i]);
    47 	}
    48 
    49 	for_each(args.begin(), args.end(), [command, fgGreen, fgReset](string s) {
    50 		cout << fgGreen << command << fgReset << ": " << s << endl;
    51 	});
    52 
    53 
    54 	for (int i = 0; i < args.size(); i++) {
    55 		string s = args[i];
    56 		cout << i + 1 << ":" << s.length() << " = \"" << s << "\"" << endl;
    57 	}
    58 
    59 	{
    60 		string envName = args[0];
    61 		boost::optional<string> outputModule = getenv(envName);
    62 
    63 		if (outputModule.is_initialized()) {
    64 			cout << "ENV: " << envName << " = " << outputModule.get() << endl;
    65 		} else {
    66 			cout << "ENV: " << envName << " is missing" << endl;
    67 		}
    68 	}
    69 
    70 	{
    71 		shared_ptr<OutputModule> om(new OutputModule());
    72 		shared_ptr<TerminalOutputModule> tom(new TerminalOutputModule());
    73 		shared_ptr<OutputModule> o(new TerminalOutputModule());
    74 
    75 		process(om);
    76 		process(tom);
    77 		process(o);
    78 	}
    79 
    80 	return 0;
    81 }