Working on the command line parameters.

This commit is contained in:
King_DuckZ 2013-08-11 18:19:21 +02:00
parent c7bb69e20a
commit e6e0ba6505

View file

@ -5,6 +5,8 @@
#include <boost/program_options/variables_map.hpp> #include <boost/program_options/variables_map.hpp>
#include <boost/program_options/options_description.hpp> #include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp> #include <boost/program_options/parsers.hpp>
#include <sstream>
#include <cstring>
#ifndef NDEBUG #ifndef NDEBUG
#define UNDEF_NDEBUG #define UNDEF_NDEBUG
@ -19,14 +21,52 @@
namespace { namespace {
///------------------------------------------------------------------------- ///-------------------------------------------------------------------------
///------------------------------------------------------------------------- ///-------------------------------------------------------------------------
void GetCommandLine (boost::program_options::variables_map& parVarMap, int parArgc, const char* const parArgv[]) { const char* GetBaseName (const char* parPath) {
boost::program_options::options_description desc("Program parameters"); const size_t len = std::strlen(parPath);
desc.add_options() const char* found;
("help", "show this help screen") 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] <from language> <to language> <search term>; 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<std::string>(), "source language")
("dest-lang", boost::program_options::value<std::string>(), "dest language")
("word", boost::program_options::value<std::string>(), "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); boost::program_options::notify(parVarMap);
bool shownSomething = false;
if (parVarMap.count("help")) {
std::cout << desc << "\n";
shownSomething = true;
}
return shownSomething;
} }
} //unnamed namespace } //unnamed namespace
@ -36,7 +76,22 @@ int main (int parArgc, const char* const parArgv[]) {
WordReference wref("en", "it"); WordReference wref("en", "it");
boost::program_options::variables_map vm; boost::program_options::variables_map vm;
GetCommandLine(vm, parArgc, parArgv); if (GetCommandLine(vm, parArgc, parArgv))
std::cout << wref.GetHttpLink("north face") << "\n© WordReference.com" << std::endl; return 0;
if (vm.count("listlanguages")) {
std::vector<const std::string*> 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; return 0;
} }