diff --git a/src/main.cpp b/src/main.cpp index 1e8a03f..f0a3bb8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #ifndef NDEBUG #define UNDEF_NDEBUG @@ -19,14 +21,52 @@ namespace { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- - void GetCommandLine (boost::program_options::variables_map& parVarMap, int parArgc, const char* const parArgv[]) { - boost::program_options::options_description desc("Program parameters"); - desc.add_options() - ("help", "show this help screen") + const char* GetBaseName (const char* parPath) { + const size_t len = std::strlen(parPath); + const char* found; + const char* currSearch = parPath; + do { + found = currSearch; + currSearch = std::find(found, parPath + len, '/'); + } while (currSearch++ != parPath + len); + + return found; + } + + ///------------------------------------------------------------------------- + ///------------------------------------------------------------------------- + bool GetCommandLine (boost::program_options::variables_map& parVarMap, int parArgc, const char* const parArgv[]) { + const char* const programName = parArgv[0]; + std::ostringstream oss; + oss << "Usage is " << GetBaseName(parArgv[0]) << " [options] ; parameters"; + boost::program_options::options_description desc(oss.str()); + boost::program_options::positional_options_description positionals; + positionals.add("source-lang", 0); + positionals.add("dest-lang", 1); + positionals.add("word", 2); + + boost::program_options::options_description hidden("hidden options"); + hidden.add_options() + ("source-lang", boost::program_options::value(), "source language") + ("dest-lang", boost::program_options::value(), "dest language") + ("word", boost::program_options::value(), "search term") ; - boost::program_options::store(boost::program_options::parse_command_line(parArgc, parArgv, desc), parVarMap); + desc.add_options() + ("help,h", "show this help screen") + ("listlanguages,l", "list available languages with their codes") + ; + + boost::program_options::options_description commandLine; + commandLine.add(desc).add(hidden); + boost::program_options::store(boost::program_options::command_line_parser(parArgc, parArgv).options(commandLine).positional(positionals).run(), parVarMap); boost::program_options::notify(parVarMap); + bool shownSomething = false; + if (parVarMap.count("help")) { + std::cout << desc << "\n"; + shownSomething = true; + } + return shownSomething; } } //unnamed namespace @@ -36,7 +76,22 @@ int main (int parArgc, const char* const parArgv[]) { WordReference wref("en", "it"); boost::program_options::variables_map vm; - GetCommandLine(vm, parArgc, parArgv); - std::cout << wref.GetHttpLink("north face") << "\n© WordReference.com" << std::endl; + if (GetCommandLine(vm, parArgc, parArgv)) + return 0; + + if (vm.count("listlanguages")) { + std::vector codes, names; + GetAvailableLanguages(codes, names); + std::cout << "Supported languages are:\n"; + for (size_t z = 0; z < codes.size(); ++z) { + std::cout << "\t" << *names[z] << " [" << *codes[z] << "]\n"; + } + std::cout << "\n"; + std::cout << "Note that the current WordReference specifications (Aug 2013) says:\n\""; + std::cout << "Only to and from English. We do not currently offer any combination that\ndoes not include English (such as Spanish-French).\"\n"; + std::cout << "This program does not enforce this rule, but it may malfunction if you\nchoose the wrong languages.\n"; + return 0; + } + std::cout << wref.GetHttpLink("north face") << "\nWritten by King_DuckZ; © WordReference.com" << std::endl; return 0; }