#include "ptypes.h"␍␊ |
#include "pstreams.h"␍␊ |
␍␊ |
USING_PTYPES␍␊ |
␍␊ |
pparser::pparser(int argc, char * argv[], const tobjlist<Parameter> & parameters)␍␊ |
{␍␊ |
␉this->checkreq = true;␍␊ |
␉// Assumptions:␍␊ |
␉// *) options will not start with -␍␊ |
␉// *) parameters will not be repeated␍␊ |
␉int z = 0;␍␊ |
␉tpodlist<int> parasused;␍␊ |
␉bool docontinue = false;␍␊ |
␉for(int i = 1; i < argc; i++)␍␊ |
␉{␍␊ |
␍␊ |
␉␉if (argv[i][0] == '-')␍␊ |
␉␉{␍␊ |
␉␉␉if (z != parameters.get_count())␍␊ |
␉␉␉{␍␊ |
␉␉␉␉␍␊ |
␉␉␉␉for (int zz = 0; zz < parameters.get_count(); zz++)␍␊ |
␉␉␉␉{␍␊ |
␉␉␉␉␉docontinue = false;␍␊ |
␉␉␉␉␉for (int pu = 0; pu < parasused.get_count(); pu++)␍␊ |
␉␉␉␉␉{␉␍␊ |
␉␉␉␉␉␉if (parasused[pu] == zz)␍␊ |
␉␉␉␉␉␉{␍␊ |
␉␉␉␉␉␉␉docontinue = true;␍␊ |
␉␉␉␉␉␉␉break;␍␊ |
␉␉␉␉␉␉}␍␊ |
␉␉␉␉␉}␍␊ |
␉␉␉␉␉if (docontinue)␍␊ |
␉␉␉␉␉␉continue;␍␊ |
␉␉␉␉␉␍␊ |
␉␉␉␉␉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++;␍␊ |
␉␉␉␉␉␉parasused.add(zz);␍␊ |
␉␉␉␉␉␉break;␍␊ |
␉␉␉␉␉}␍␊ |
␉␉␉␉}␍␊ |
␉␉␉␉␍␊ |
␉␉␉} 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 <char *>((const char *)parameters[zz]->shortparameter));␍␊ |
␉␉␉}␍␊ |
␉␉}␍␊ |
␉}␍␊ |
␉this->paras = const_cast <tobjlist<Parameter> *>(¶meters);␍␊ |
␍␊ |
␍␊ |
}␍␊ |
␍␊ |
void pparser::printusage(const char * header, const char * pname, const char * footer)␍␊ |
{␍␊ |
␉pout.put(header);␍␊ |
␉pout.put("\n");␍␊ |
␉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 + "\n\n";␍␊ |
␉}␍␊ |
␉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;␍␊ |
} |