-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOptionParser.cpp
68 lines (59 loc) · 2.46 KB
/
OptionParser.cpp
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
#include "OptionParser.h"
OptionParser::OptionParser(bool withQt){
if(withQt){
possibleFlags << "cmap" << "widgetcount" << "reverse";
possibleOptions << "style" << "stylesheet" << "session" << "graphicssystem" << "qmljsdebugger" << "display" << "geometry" << "fn" << "font" << "bg" << "background" << "fg" << "foreground" << "btn" << "button" << "name" << "title" << "visual" << "ncols" << "im" << "inputstyle";
}
addFlag("help","h",tr("Show help text"));
}
void OptionParser::addOption(const QString& longName, const QString& shortName, const QString& description){
optionAliases[shortName] = longName;
optionAliases[longName] = shortName;
possibleOptions << longName << shortName;
optionDescriptions << ("--" + longName + " | -" + shortName + ": " + description);
}
void OptionParser::addFlag(const QString& longName, const QString& shortName, const QString& description){
optionAliases[shortName] = longName;
optionAliases[longName] = shortName;
possibleFlags << longName << shortName;
flagDescriptions << ("--" + longName + " | -" + shortName + ": " + description);
}
bool OptionParser::parseArgs(int argc, char* argv[]){
QStringList cmdArgs;
for(int j = 1; j < argc; ++j) cmdArgs << QString::fromUtf8(argv[j]);
QString optionName = "";
for(int j = 0; j < cmdArgs.size(); ++j){
if(optionName != ""){
options[optionName] = cmdArgs[j];
optionName = "";
} else if(cmdArgs[j][0] == '-' && cmdArgs[j].size() > 1){
int start = 1;
if(cmdArgs[j][1] == '-') start = 2;
optionName = cmdArgs[j].mid(start);
if(possibleFlags.contains(optionName)){
flags << optionName;
optionName = "";
}
} else arguments << cmdArgs[j];
}
flagDescriptions.sort();
optionDescriptions.sort();
return true;
}
QString OptionParser::get(const QString& argument, const QString& elsevalue) const{
if(options.contains(argument)) return options[argument];
if(options.contains(optionAliases[argument])) return options[optionAliases[argument]];
return elsevalue;
}
QString OptionParser::getArgument(int j) const{
return arguments[j];
}
bool OptionParser::checkFlag(const QString& argument) const{
return flags.contains(argument) || (optionAliases.contains(argument) && options.contains(optionAliases[argument]));
}
int OptionParser::argumentCount() const{
return arguments.size();
}
QString OptionParser::helpText() const{
return tr("Possible Flags:") + "\n\n " + flagDescriptions.join("\n ") + "\n\n" + tr("Possible Options:") + "\n\n " + optionDescriptions.join("\n ");
}