From: Christian P. <cp...@us...> - 2005-01-21 09:21:17
|
Update of /cvsroot/pclasses/pclasses2/src/App In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24324/src/App Modified Files: CmdLine.cpp Log Message: Removed default values from ctor. Index: CmdLine.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/App/CmdLine.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- CmdLine.cpp 20 Jan 2005 19:56:29 -0000 1.4 +++ CmdLine.cpp 21 Jan 2005 09:21:07 -0000 1.5 @@ -21,7 +21,7 @@ #include "pclasses/App/CmdLine.h" #include <sstream> #include <iomanip> -#include <iostream> +#include <list> namespace P { @@ -71,10 +71,8 @@ } CmdLineFlag::CmdLineFlag(const std::string& shortOpt, - const std::string& longOpt, const std::string& helpText, - bool defaultVal, bool required) -: CmdLineOption(shortOpt, longOpt, helpText, required, false), - _defaultVal(defaultVal) + const std::string& longOpt, const std::string& helpText, bool required) +: CmdLineOption(shortOpt, longOpt, helpText, required, false) { } @@ -95,18 +93,16 @@ CmdLineValue::CmdLineValue(const std::string& shortOpt, - const std::string& longOpt, const std::string& helpText, - const std::string& defaultVal) -: CmdLineOption(shortOpt, longOpt, helpText, false, true), - _defaultVal(defaultVal) + const std::string& longOpt, const std::string& helpText) +: CmdLineOption(shortOpt, longOpt, helpText, false, true) { + _isset = true; } CmdLineValue::CmdLineValue(const std::string& shortOpt, const std::string& longOpt, const std::string& helpText, bool required) -: CmdLineOption(shortOpt, longOpt, helpText, required, true), - _defaultVal("") +: CmdLineOption(shortOpt, longOpt, helpText, required, true) { } @@ -176,18 +172,19 @@ void CmdLineParser::parse(int argc, char* argv[]) throw(CmdLineError) { _unnamedValues.clear(); - CmdLineOption* opt = 0; - std::string optName, optVal; + CmdLineOption* opt = 0; + std::list<CmdLineOption*> foundOpts; std::string tmp; for(int i = 1; i < argc; ++i) { tmp = argv[i]; - if(tmp.size() > 1 && tmp[0] == '-') + if(!opt && tmp.size() > 1 && tmp[0] == '-') { bool isLongOpt = false; + std::string optName, optVal; // long option? if(tmp.size() > 2 && tmp[1] == '-') @@ -217,7 +214,7 @@ // unknown option ... if(!opt) - throw CmdLineError("Unknown command line option: " + + throw CmdLineError("Unknown command-line argument: " + std::string(argv[i]), P_SOURCEINFO); // do we have value for this option ....? @@ -225,12 +222,14 @@ // option values for flags are simply discarded if(pos != std::string::npos) { + foundOpts.push_back(opt); opt->setValue(optVal); opt = 0; } // option does not need a value... else if(!opt->needValue()) { + foundOpts.push_back(opt); opt->setValue(std::string()); opt = 0; } @@ -241,6 +240,7 @@ // the value is for the current option... if(opt) { + foundOpts.push_back(opt); opt->setValue(tmp); opt = 0; } @@ -251,6 +251,50 @@ } } } + + // we started parsing a option, but the value is missing. + if(opt) + { + std::ostringstream errOs; + errOs << "Value for command-line argument '"; + if(opt->shortName().size() > 0) + errOs << "-" << opt->shortName(); + else + errOs << "--" << opt->longName(); + errOs << "' is missing." << std::endl; + + throw CmdLineError(errOs.str(), P_SOURCEINFO); + } + + // search for required option which where not found + int i = 0; + while(_opts[i]) + { + if(*foundOpts.begin() == _opts[i]) + { + // we processed this option .. remove it from the foundOpts list + foundOpts.erase(foundOpts.begin()); + } + else + { + // this options is required .. and we did not process it + // do not yiel the error when !isset() (respects default values) + if(_opts[i]->required() && !_opts[i]->isset()) + { + std::ostringstream errOs; + errOs << "Required command-line argument '"; + if(_opts[i]->shortName().size() > 0) + errOs << "-" << _opts[i]->shortName(); + else + errOs << "--" << _opts[i]->longName(); + errOs << "' missing." << std::endl; + + throw CmdLineError(errOs.str(), P_SOURCEINFO); + } + } + + ++i; + } } const std::string& CmdLineParser::value(unsigned int index) const @@ -269,6 +313,7 @@ void CmdLineParser::parse(const std::string& cmdline) throw(CmdLineError) { + //@fixme throw CmdLineError("Not implemented", P_SOURCEINFO); } |