-
Notifications
You must be signed in to change notification settings - Fork 1
/
arguments.cpp
executable file
·168 lines (142 loc) · 4.2 KB
/
arguments.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// Created by Haifa Bogdan Adnan on 05/02/2019.
//
#include "common.h"
#include "arguments.h"
arguments::arguments(int argc, char **argv) {
__argv_0 = argv[0];
__init();
int c = 0;
char buff[50];
while (c != -1)
{
static struct option options[] =
{
{"help", no_argument, NULL, 'h'},
{"verbose", no_argument, NULL, 'v'},
{"port", required_argument, NULL, 'p'},
{"intensity", required_argument, NULL, 'i'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long (argc, argv, "hvp:i:",
options, &option_index);
switch (c)
{
case -1:
case 0:
break;
case 1:
sprintf(buff, "%s: invalid arguments",
argv[0]);
__error_message = buff;
__error_flag = true;
c = -1;
break;
case 'h':
__help_flag = 1;
break;
case 'v':
__verbose_flag = 1;
break;
case 'p':
if(strcmp(optarg, "-h") == 0 || strcmp(optarg, "--help") == 0) {
__help_flag = 1;
}
else {
__port = atoi(optarg);
}
break;
case 'i':
if(strcmp(optarg, "-h") == 0 || strcmp(optarg, "--help") == 0) {
__help_flag = 1;
}
else {
__intensity = atof(optarg);
}
break;
case ':':
__error_flag = true;
break;
default:
__error_flag = true;
break;
}
}
if (optind < argc)
{
sprintf(buff, "%s: invalid arguments",
argv[0]);
__error_message = buff;
__error_flag = true;
}
}
bool arguments::valid(string &error) {
error = __error_message;
if(__error_flag)
return false;
if (__intensity < 0 || __intensity > 100) {
error = "Intensity must be between 0 - disabled and 100 - full load.";
return false;
}
if (__port < 10) {
error = "Port should be a positive number bigger than 10.";
return false;
}
return true;
}
bool arguments::is_help() {
return __help_flag == 1;
}
bool arguments::is_verbose() {
return __verbose_flag == 1;
}
int arguments::port() {
return __port;
}
double arguments::intensity() {
return __intensity;
}
string arguments::get_help() {
return
"\nArgonValidationService v." ArgonValidationService_VERSION_MAJOR "." ArgonValidationService_VERSION_MINOR "." ArgonValidationService_VERSION_REVISION "\n"
"Copyright (C) 2019 Haifa Bogdan Adnan\n"
"\n"
"Usage:\n"
" ArgonValidationService --port <port> --intensity <intensity> --verbose\n"
"\n"
"Parameters:\n"
" --help: show this help text\n"
" --verbose: print more informative text during run\n"
" --port <port>: port on which to listen for clients, defaults to 2000\n"
" --intensity: percentage of available CPU cores to use\n"
" value from 0 (disabled) to 100 (full load)\n"
" this is optional, defaults to 100\n"
"\n"
;
}
void arguments::__init() {
__help_flag = 0;
__verbose_flag = 0;
__intensity = 100;
__port = 2000;
__error_flag = false;
}
string arguments::__argv_0 = "./";
vector<string> arguments::__parse_multiarg(const string &arg) {
string::size_type pos, lastPos = 0, length = arg.length();
vector<string> tokens;
while(lastPos < length + 1)
{
pos = arg.find_first_of(",", lastPos);
if(pos == std::string::npos)
{
pos = length;
}
if(pos != lastPos)
tokens.push_back(string(arg.c_str()+lastPos,
pos-lastPos ));
lastPos = pos + 1;
}
return tokens;
}