c++/parameter-lister/CLI.cpp
author František Kučera <franta-hg@frantovo.cz>
Mon, 04 May 2015 00:34:27 +0200
changeset 15 09adb33465e4
parent 11 870b868b6b57
child 16 65f51abd5fb8
permissions -rw-r--r--
fix core dump (SIGSEGV) when there are no arguments
     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 }
    29 }
    30 }
    31 
    32 int main(int argc, char* argv[]) {
    33 	terminalCodes::Modifier fgGreen(terminalCodes::FG_GREEN);
    34 	terminalCodes::Modifier fgReset(terminalCodes::FG_DEFAULT);
    35 
    36 	cout << "INFO: " << fgGreen << "Parameter lister" << fgReset << " is starting" << endl;
    37 
    38 	string command = argv[0];
    39 	vector<string> args;
    40 
    41 	for (int i = 1; i < argc; i++) {
    42 		args.push_back(argv[i]);
    43 	}
    44 
    45 	/** Load environment variable */
    46 	if (args.size() > 0) {
    47 		string envName = args[0];
    48 		boost::optional<string> outputModule = getenv(envName);
    49 
    50 		if (outputModule.is_initialized()) {
    51 			cout << "ENV: " << envName << " = " << outputModule.get() << endl;
    52 		} else {
    53 			cout << "ENV: " << envName << " is missing" << endl;
    54 		}
    55 	}
    56 
    57 	/** Do formatting */
    58 	{
    59 		shared_ptr<OutputModule> om(new OutputModule());
    60 		shared_ptr<TerminalOutputModule> tom(new TerminalOutputModule());
    61 		shared_ptr<OutputModule> o(new TerminalOutputModule());
    62 
    63 		om->process(cout, command, args);
    64 		tom->process(cout, command, args);
    65 		o->process(cout, command, args);
    66 	}
    67 
    68 	return 0;
    69 }