c++/parameter-lister/CLI.cpp
author František Kučera <franta-hg@frantovo.cz>
Sun, 03 May 2015 18:08:40 +0200
changeset 5 1581a99ab33f
parent 4 a4c3f27b6be7
child 7 2f46492e90bc
permissions -rw-r--r--
getenv(), namespace move
     1 #include <cstdlib>
     2 #include <iostream>
     3 #include <vector>
     4 #include <algorithm>
     5 #include <boost/optional.hpp>
     6 
     7 #include "info/globalcode/parameterLister/terminalCodes/TerminalCodes.h"
     8 
     9 using namespace std;
    10 using namespace info::globalcode::parameterLister;
    11 
    12 namespace info {
    13 namespace globalcode {
    14 namespace parameterLister {
    15 
    16 boost::optional<string> getenv(const string name) {
    17 	const char * value = ::getenv(name.c_str());
    18 	if (value == 0) {
    19 		return boost::optional<string>();
    20 	} else {
    21 		return boost::optional<string>(value);
    22 	}
    23 }
    24 
    25 
    26 }
    27 }
    28 }
    29 
    30 int main(int argc, char* argv[]) {
    31 	terminalCodes::Modifier fgGreen(terminalCodes::FG_GREEN);
    32 	terminalCodes::Modifier fgReset(terminalCodes::FG_DEFAULT);
    33 
    34 	cout << "INFO: " << fgGreen << "Parameter lister" << fgReset << " is starting" << endl;
    35 
    36 	string command = argv[0];
    37 	vector<string> args;
    38 
    39 	for (int i = 1; i < argc; i++) {
    40 		args.push_back(argv[i]);
    41 	}
    42 
    43 	for_each(args.begin(), args.end(), [command, fgGreen, fgReset](string s) {
    44 		cout << fgGreen << command << fgReset << ": " << s << endl;
    45 	});
    46 
    47 
    48 	for (int i = 0; i < args.size(); i++) {
    49 		string s = args[i];
    50 		cout << i + 1 << ":" << s.length() << " = \"" << s << "\"" << endl;
    51 	}
    52 
    53 	{
    54 		string envName = args[0];
    55 		boost::optional<string> outputModule = getenv(envName);
    56 
    57 		if (outputModule.is_initialized()) {
    58 			cout << "ENV: " << envName << " = " << outputModule.get();
    59 		} else {
    60 			cout << "ENV: " << envName << " is missing";
    61 		}
    62 	}
    63 
    64 
    65 	return 0;
    66 }