1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#ifndef ARGUMENTPARSER_H
#define ARGUMENTPARSER_H
#include<map>
#include<string>
#include<vector>
using namespace std;
enum OptionType {OTSTRING, OTLONG, OTBOOL, OTDOUBLE};
struct Option{
OptionType type;
string shortName,longName,description;
};
class ArgumentParser{
private:
map<string,string> mapS;
map<string,long> mapL;
map<string,bool> mapB;
map<string,double> mapD;
map<string,string> names;
map<string,Option> validOptions;
string programName, argumentDesc, programDesc;
vector<string> arguments;
vector<string> compulsory;
long minimumArguments;
bool existsOption(const string &name, bool warn = false) const;
bool existsName(const string &name) const;
template <typename valueType>
void appendDescription(string *desc,valueType defValue);
public:
bool verbose;
ArgumentParser(const string &pD="",const string &aD="[FILES]", long minArgs = 1){
verbose = false;
init(pD,aD,minArgs);
}
void init(const string &pD="",const string &aD="[FILES]", long minArgs = 1){
programDesc=pD;
argumentDesc=aD;
minimumArguments = minArgs;
}
bool parse(int n,char * argv[]);
void addOptionS(const string &shortName,
const string &longName,
const string &name,
bool comp,
const string &description="",
const string &defValue="noDefault");
void addOptionL(const string &shortName, const string &longName,
const string &name, bool comp, const string &description="",
long defValue=-47);
void addOptionD(const string &shortName, const string &longName,
const string &name, bool comp, const string &description="",
double defValue=-47.47);
void addOptionB(const string &shortName, const string &longName,
const string &name, bool comp, const string &description="",
bool defValue=false);
const vector<string>& args() const { return arguments; }
bool isSet(const string &name) const;
string getS(const string &name) const;
string getLowerS(const string &name) const;
long getL(const string &name) const;
double getD(const string &name) const;
bool flag(const string &name) const;
bool verb() const { return verbose; }
vector<double> getTokenizedS2D(const string &name) const;
void usage();
void writeAll();
void updateS(const string &name, const string &value);
};
#endif
|