64 lines
1.3 KiB
C++
64 lines
1.3 KiB
C++
#include "picli.h"
|
|
|
|
|
|
PICLI::PICLI(int argc, char * argv[]) {
|
|
needParse = true;
|
|
_prefix_short = "-";
|
|
_prefix_full = "--";
|
|
_count_opt = 0;
|
|
_count_mand = 1;
|
|
for (int i = 0; i < argc; ++i)
|
|
_args_raw << argv[i];
|
|
}
|
|
|
|
|
|
void PICLI::parse() {
|
|
if (!needParse) return;
|
|
PIString cra, full;
|
|
Argument * last = 0;
|
|
for (int i = 1; i < _args_raw.size_s(); ++i) {
|
|
cra = _args_raw[i];
|
|
if (cra.left(2) == _prefix_full) {
|
|
last = 0;
|
|
full = cra.right(cra.length() - 2);
|
|
piForeach (Argument & a, _args) {
|
|
if (a.full_key == full) {
|
|
a.found = true;
|
|
last = &a;
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
if (cra.left(1) == _prefix_short) {
|
|
last = 0;
|
|
for (int j = 1; j < cra.length(); ++j) {
|
|
piForeach (Argument & a, _args) {
|
|
if (a.short_key == cra[j]) {
|
|
a.found = true;
|
|
last = &a;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
if (last == 0 ? true : !last->has_value) {
|
|
if (_args_mand.size_s() < _count_mand) {
|
|
_args_mand << cra;
|
|
continue;
|
|
}
|
|
if (_args_opt.size_s() < _count_opt) {
|
|
_args_opt << cra;
|
|
continue;
|
|
}
|
|
cout << "[PICli] Arguments overflow, \"" << cra << "\" ignored" << endl;
|
|
}
|
|
if (last == 0 ? false : last->has_value) {
|
|
last->value = cra;
|
|
last = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
needParse = false;
|
|
}
|