diff -r 8176df6732447125986de97a4b5c14907d5f3012 -r da6db760aa7b0e7f6212a8fa6229c41e72cd41bb win32/ppparser.cxx --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/win32/ppparser.cxx Fri May 24 21:12:45 2013 -0500 @@ -0,0 +1,141 @@ +#include "ptypes.h" +#include "pstreams.h" + +USING_PTYPES + +pparser::pparser(int argc, char * argv[], const tobjlist & parameters) +{ + this->checkreq = true; + // Assumptions: + // *) options will not start with - + // *) parameters will not be repeated + int z = 0; + for(int i = 1; i < argc; i++) + { + + if (argv[i][0] == '-') + { + if (z != parameters.get_count()) + { + for (int zz = z; zz < parameters.get_count(); zz++) + { + if (argv[i] == ("-" + parameters[zz]->longparameter) || argv[i] == ("-" + parameters[zz]->shortparameter)) + { + if (parameters[zz]->numberofparameters == 0) + { + put(this->choices, argv[i], ""); + } else { + variant options; + if ((i+parameters[zz]->numberofparameters) <= argc) + { + for(int ii = 0; ii < parameters[zz]->numberofparameters; ii++) + { + add(options, (pt::string)argv[ii+i+1]); + } + put(this->choices, argv[i], options); + i += parameters[zz]->numberofparameters; + } else { + this->checkreq = false; + } + } + z++; + } + } + + } else { + // hit the end of scanned parameters but user provided parameters still exist? + this->checkreq = false; + this->invalidparas.add(argv[i]); + } + + } else { + //should never look at a parameter that doesn't start with a - + this->checkreq = false; + this->invalidparas.add(argv[i]); + } + + this->argv.add(argv[i]); + } + + if (z != parameters.get_count()) + { + //scan through parameters for missing paras + for(int zz = z; zz < parameters.get_count(); zz++) + { + if (parameters[zz]->required) + this->checkreq = false; + this->missingparas.add(const_cast ((const char *)parameters[zz]->shortparameter)); + } + } + this->paras = const_cast *>(¶meters); + + +} + +void pparser::printusage(const char * header, const char * pname, const char * footer) +{ + pout.put(header); + if (this->invalidparas.get_count() > 0) + { + pout.put("Invalid parameters: "); + for(int i = 0; i < this->invalidparas.get_count(); i++) + { + pout.put(this->invalidparas[i]); + pout.put(" "); + } + pout.put("\n\n"); + } + + if (this->missingparas.get_count() > 0) + { + pout.put("Missing parameters: "); + for(int i = 0; i < this->missingparas.get_count(); i++) + { + pout.put("-"); + pout.put(this->missingparas[i]); + pout.put(" "); + } + pout.put("\n\n"); + } + pout.put("USAGE: "); + pout.put(pname); + pout.put(" "); + string parameterlist; + string paralisthints; + for(int i = 0; i < this->paras->get_count(); i++) + { + if ((*this->paras)[i]->required) + parameterlist += "-" + (*this->paras)[i]->shortparameter + " "; + else + parameterlist += "[-" + (*this->paras)[i]->shortparameter + "] "; + + paralisthints += "-" + (*this->paras)[i]->shortparameter + ", --" + (*this->paras)[i]->longparameter + "\n" + (*this->paras)[i]->hint; + } + pout.put(parameterlist); + pout.put("\n"); + pout.put("\n"); + pout.put(paralisthints); + + pout.put(footer); + pout.put("\n"); +} + +bool pparser::isparameterset(const Parameter * para) +{ + if (get(this->choices, "--" + para->longparameter) == nullvar && get(this->choices, "-" + para->shortparameter) == nullvar) + { + return false; + } + return true; +} + +variant pparser::getoptions(const Parameter * para) +{ + if (get(this->choices, "--" + para->longparameter) != nullvar) + return get(this->choices, "--" + para->longparameter); + + if (get(this->choices, "-" + para->shortparameter) != nullvar) + return get(this->choices, "-" + para->shortparameter); + + return nullvar; +} \ No newline at end of file