-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexampleMainExtended.cpp
87 lines (78 loc) · 3.37 KB
/
exampleMainExtended.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include "include/argvParser.h"
using namespace std;
/**
* argument that has additional parameter
*/
int printCallBack(int index, char **buff) {
index++;
cout << "print : " << buff[index] << endl; // access to the next element
return index; // +1 because one further argument has been used
}
int loggingCallBack(int index, char **buff) {
cout << "enable logging" << endl;
return index; // +1 because one further argument has been used
}
/**
* argument that has additional parameter
*/
int logFileCallBack(int index, char **buff) {
index++;
cout << "log to " << buff[index] << endl;
return index; // +1 because one further argument has been used
}
/**
* argument that has additional parameter
*/
int enumCallBack(int index, char **buff) {
index++;
string got = string(buff[index]);
if (got == "abc" || got == "def" || got == "xyz") {
cout << "got enum " << buff[index] << endl;
} else {
cout << "gor wrong enum" << endl;
}
return index;
}
bool disableCliH = false;
int main(int argvs, char **argv) {
// define program description
argvParser *p = new argvParser("Extendend Example","This application intends to be an example ");
// define program arguments
// Simple Lambda CallBack
p->addArg("-t", "--test", "test argument", []() { cout << "got \"test\"" << endl; })
->addAdditionalHelp("It is possible to add addition information for one command. Now the user can ype <-h -t> and see this additional information"); // no further arguments used);
// Lambda CallBack as required and one additional parameter
function<int(int, char **)> lambdaCallback = [](int index, char **buff) {
index++;
cout << "got \"foo\" with : " << buff[index] << endl;
return index;
};
p->addArg("-f", "--foo", "foo test argument required argument example",
lambdaCallback)->required()->numberOfParameter(1)->addAdditionalHelp("prints the next element to the cli");
// Function as Callback
p->addArg("-p", "--print", "echo text", printCallBack)->numberOfParameter(1)->addAdditionalHelp(
"echo the next argument");
// pre defined parameter for "-e" with bash autocompletion
p->addArg("-e", "--enums", "enum example", enumCallBack)->numberOfParameter(1)->allowedParameter(3, "abc", "def",
"xyz")
->addAdditionalHelp("here is just a pre defined set of arguments allowed");
// autocompletion with filenames
p->addArg("-o", "--open", "example to complete a file/dir", printCallBack)->numberOfParameter(1)->asFile();
// lambda
p->addArg("-nh", "--noHighlight", "disabledCli highlighting", [] { disableCliH = false; });
p->addSection("logging");
p->addArg("-l", "--logging", "enable logging", loggingCallBack);
p->addArg("-logf", "--logFile", "generate logfile", logFileCallBack);
// check if all arguments are valid
if (!p->analyseArgv(argvs, argv)) {
p->printHelpMessage(!disableCliH);
// check if all required arguments have been parsed
if (!p->foundAllRequierdArgs()) {
cout
<< "you have not entered at least one required argument \n\t -f has been marked as an required argument try it with -f"
<< endl;
}
}
return 0;
}