# HG changeset patch # User František Kučera # Date 1430857164 -7200 # Node ID 65f51abd5fb81b16d8f1496b93d005d21be945a7 # Parent 09adb33465e4c07936491822bcec85b021376ee5 chooseOutputModule diff -r 09adb33465e4 -r 65f51abd5fb8 c++/parameter-lister/CLI.cpp --- a/c++/parameter-lister/CLI.cpp Mon May 04 00:34:27 2015 +0200 +++ b/c++/parameter-lister/CLI.cpp Tue May 05 22:19:24 2015 +0200 @@ -1,11 +1,11 @@ #include #include #include +#include #include #include #include -#include "info/globalcode/parameterLister/terminalCodes/TerminalCodes.h" #include "info/globalcode/parameterLister/OutputModule.h" #include "info/globalcode/parameterLister/TerminalOutputModule.h" @@ -16,6 +16,9 @@ namespace globalcode { namespace parameterLister { +const string ENV_BASE = "PARAMETER_LISTER"; +const string ENV_OUTPUT_MODULE_NAME = ENV_BASE + "_OUTPUT"; + boost::optional getenv(const string name) { const char * value = ::getenv(name.c_str()); if (value == 0) { @@ -29,12 +32,21 @@ } } +shared_ptr chooseOutputModule() { + boost::optional moduleName = getenv(ENV_OUTPUT_MODULE_NAME); + if (moduleName.is_initialized()) { + // TODO: use a map instead of sequence of IFs + if (moduleName.get() == "terminal") { + return shared_ptr(new TerminalOutputModule()); + } + } + + return shared_ptr(new OutputModule()); +} + int main(int argc, char* argv[]) { - terminalCodes::Modifier fgGreen(terminalCodes::FG_GREEN); - terminalCodes::Modifier fgReset(terminalCodes::FG_DEFAULT); - cout << "INFO: " << fgGreen << "Parameter lister" << fgReset << " is starting" << endl; - + /** Load arguments */ string command = argv[0]; vector args; @@ -42,28 +54,8 @@ args.push_back(argv[i]); } - /** Load environment variable */ - if (args.size() > 0) { - string envName = args[0]; - boost::optional outputModule = getenv(envName); + /** Do formatting */ + shared_ptr outputModule = chooseOutputModule(); - if (outputModule.is_initialized()) { - cout << "ENV: " << envName << " = " << outputModule.get() << endl; - } else { - cout << "ENV: " << envName << " is missing" << endl; - } - } - - /** Do formatting */ - { - shared_ptr om(new OutputModule()); - shared_ptr tom(new TerminalOutputModule()); - shared_ptr o(new TerminalOutputModule()); - - om->process(cout, command, args); - tom->process(cout, command, args); - o->process(cout, command, args); - } - - return 0; + return outputModule->process(cout, command, args); } diff -r 09adb33465e4 -r 65f51abd5fb8 c++/parameter-lister/info/globalcode/parameterLister/OutputModule.cpp --- a/c++/parameter-lister/info/globalcode/parameterLister/OutputModule.cpp Mon May 04 00:34:27 2015 +0200 +++ b/c++/parameter-lister/info/globalcode/parameterLister/OutputModule.cpp Tue May 05 22:19:24 2015 +0200 @@ -7,7 +7,7 @@ namespace globalcode { namespace parameterLister { -void OutputModule::process(std::ostream &output, std::string &command, std::vector &args) { +int OutputModule::process(std::ostream &output, std::string &command, std::vector &args) { terminalCodes::Modifier fgGreen(terminalCodes::FG_GREEN); terminalCodes::Modifier fgReset(terminalCodes::FG_DEFAULT); @@ -20,6 +20,8 @@ }); output << "" << endl; + + return 0; } diff -r 09adb33465e4 -r 65f51abd5fb8 c++/parameter-lister/info/globalcode/parameterLister/OutputModule.h --- a/c++/parameter-lister/info/globalcode/parameterLister/OutputModule.h Mon May 04 00:34:27 2015 +0200 +++ b/c++/parameter-lister/info/globalcode/parameterLister/OutputModule.h Tue May 05 22:19:24 2015 +0200 @@ -10,7 +10,14 @@ class OutputModule { public: - virtual void process(std::ostream &output, std::string &command, std::vector &args); + /** + * + * @param output + * @param command + * @param args + * @return exit code + */ + virtual int process(std::ostream &output, std::string &command, std::vector &args); private: }; diff -r 09adb33465e4 -r 65f51abd5fb8 c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.cpp --- a/c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.cpp Mon May 04 00:34:27 2015 +0200 +++ b/c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.cpp Tue May 05 22:19:24 2015 +0200 @@ -1,19 +1,26 @@ #include "TerminalOutputModule.h" +#include "terminalCodes/TerminalCodes.h" namespace info { namespace globalcode { namespace parameterLister { -void TerminalOutputModule::process(std::ostream &output, std::string &command, std::vector &args) { +int TerminalOutputModule::process(std::ostream &output, std::string &command, std::vector &args) { using namespace std; - output << "" << endl; + + terminalCodes::Modifier fgGreen(terminalCodes::FG_GREEN); + terminalCodes::Modifier fgReset(terminalCodes::FG_DEFAULT); + + output << fgGreen << "" << fgReset << endl; for (int i = 0; i < args.size(); i++) { string s = args[i]; output << i + 1 << ":" << s.length() << " = \"" << s << "\"" << endl; } - output << "" << endl; + output << fgGreen << "" << fgReset << endl; + + return 0; } } diff -r 09adb33465e4 -r 65f51abd5fb8 c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.h --- a/c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.h Mon May 04 00:34:27 2015 +0200 +++ b/c++/parameter-lister/info/globalcode/parameterLister/TerminalOutputModule.h Tue May 05 22:19:24 2015 +0200 @@ -11,7 +11,7 @@ class TerminalOutputModule : public OutputModule { public: - virtual void process(std::ostream &output, std::string &command, std::vector &args); + virtual int process(std::ostream &output, std::string &command, std::vector &args); private: };