22#if defined(ARGX_AS_PYTHON_PACKAGE)
23 Argx::Argx(
const std::string &
id,
const std::vector<std::string> &args)
26 this->mainArgs =
new std::vector<std::string>(args);
27 this->mainArgc = args.size();
34 Argx::Argx(
const std::string &
id,
int argc,
char *argv[])
37 this->
mainArgs =
new std::vector<std::string>(argv, argv + argc);
41 std::cerr <<
"`Args::mainArgs` is not valid for ID of " +
id +
" variable is NULL";
60 if (argPos >= 0)
return argPos;
61 if (shortArgPos >= 0)
return shortArgPos;
70 for (
const auto &option : this->
options)
72 if (option.param == param || option.sparam == param)
84 for (
size_t i = 0; i < this->
mainArgs->size(); ++i)
94 { this->
options.emplace_back(option); }
99 for (
size_t i = 0; i < this->
options.size(); i++)
104 for (
const std::string &arg : *this->
mainArgs)
106 if (arg == this->
options[i].param || arg == this->
options[i].sparam)
108 return static_cast<int>(i);
115 for (
const auto &opt : this->
options)
118 bool parentExists =
false;
120 for (
const std::string &arg : *this->
mainArgs)
122 if (arg == opt.param || arg == opt.sparam)
132 for (
size_t i = 0; i < opt.subParams.size(); i++)
134 if (opt.subParams[i].id ==
id)
return static_cast<int>(i);
144 if (this->
findParam(
id) >= 0)
return true;
151 for (
const auto &p : this->
options)
153 if (p.sparam ==
id)
return true;
163 if (paramID < 0)
return false;
166 if (this->
options[paramID].tag == tag)
return true;
178 for (
const auto &opt : this->
options)
183 int mainOptionPos = -1;
185 for (
size_t i = 0; i < this->
mainArgs->size(); ++i)
187 if ((*this->
mainArgs)[i] == opt.param || (*this->mainArgs)[i] == opt.sparam)
197 if (opt.hasSubParams || opt.hasAnySubParams)
200 for (
const auto &sub : opt.subParams)
202 bool subMatched =
false;
205 for (
size_t i = mainOptionPos + 1; i < this->
mainArgs->size(); ++i)
207 if ((*this->
mainArgs)[i] == sub.param || (*this->mainArgs)[i] == sub.sparam)
224 for (
const auto &opt : this->options)
227 size_t parentPos = -1;
229 for (
size_t i = 0; i < this->
mainArgs->size(); ++i)
231 if ((*this->
mainArgs)[i] == opt.param || (*this->mainArgs)[i] == opt.sparam)
238 if (parentPos > -1 && (opt.hasSubParams || opt.hasAnySubParams))
241 for (
const auto &sub : opt.subParams)
245 for (
size_t i = parentPos + 1 ; i < this->
mainArgs->size(); ++i)
247 if ((*this->
mainArgs)[i] == sub.param || (*this->mainArgs)[i] == sub.sparam)
254 if (!result.
exists && parentPos + 1 < this->mainArgs->size())
256 std::string nextArg = (*this->
mainArgs)[parentPos + 1];
258 if (nextArg == sub.param || nextArg == sub.sparam) result.
exists =
true;
262 if (result.
exists && (sub.hasSubParams || sub.hasAnySubParams))
264 for (
const auto &subsub : sub.subParams)
266 bool subsubMatched =
false;
268 for (
size_t i = 0; i < this->
mainArgs->size(); ++i)
270 if ((*this->
mainArgs)[i] == subsub.param || (*this->mainArgs)[i] == subsub.sparam)
272 subsubMatched =
true;
277 result.
subExists.push_back(subsubMatched);
295 std::string contentStr;
299 for (
const auto &x : this->
options)
302 contentStr +=
"ID: " + x.id +
"\n";
303 contentStr +=
"[ " + x.sparam +
" | " + x.param;
305 if (x.hasSubParams && !x.subParams.empty())
309 for (
size_t i = 0; i < x.subParams.size(); ++i)
311 const auto &sub = x.subParams[i];
313 contentStr += sub.param;
315 if (i < x.subParams.size() - 1) contentStr +=
" | ";
316 else if (i <= x.subParams.size()) contentStr +=
' ';
319 contentStr +=
"] ] ";
322 else contentStr +=
" ] ";
324 contentStr += x.info +
"\n";
327 if (x.hasSubParams && !x.subParams.empty())
329 for (
const auto &sub : x.subParams)
332 std::wstring wideSpaces(x.param.size(), L
'\u3000');
333 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
334 std::string spacing = converter.to_bytes(wideSpaces);
336 contentStr += spacing +
" [ " + sub.sparam +
" | " + sub.param +
" ] " + sub.info +
"\n";
344 for (
const auto &x : this->
options)
346 contentStr += x.sparam +
", " + x.param +
" - " + x.info +
"\n";
348 if (x.hasSubParams && !x.subParams.empty())
350 for (
const auto &sub : x.subParams)
352 contentStr +=
" " + sub.sparam +
", " + sub.param +
" - " + sub.info +
"\n";
358 return title +
"\n" + mainInfo +
"\n" + contentStr;
398 bool isNormalParam =
true;
400 while (pos < (
int)argv.size())
402 const std::string &arg = argv[pos];
407 for (
const auto &opt : this->
options)
409 if (arg == opt.param || arg == opt.sparam)
417 if (!matched)
return (isNormalParam ? pos : -pos);
420 isNormalParam =
true;
424 isNormalParam =
false;
426 int scanPos = pos + 1;
428 while (scanPos < (
int)argv.size())
430 const std::string &nextArg = argv[scanPos];
435 for (
const auto &subOpt : matched->
subParams)
437 if (nextArg == subOpt.param || nextArg == subOpt.sparam)
454 bool isGlobalOpt =
false;
456 for (
const auto &globalOpt : this->options)
458 if (nextArg == globalOpt.param || nextArg == globalOpt.sparam)
467 if (isGlobalOpt)
break;
478 if (isNormalParam)
return (this->
mainArgs->size() > 2 ? 2 : 1);
479 else return (this->
mainArgs->size() > 2 ? -2 : -1);
484 if (_int < 0)
return -_int;
492 for (
size_t i = 1; i < argv.size(); ++i)
494 const std::string &arg = argv[i];
499 for (
const auto &opt :
options)
501 if (opt.sparam == arg || opt.param == arg)
510 if (!matched)
return false;
517 while (j < argv.size())
519 const std::string &next = argv[j];
524 for (
const auto &sub : matched->
subParams)
526 if (next == sub.param || next == sub.sparam)
543 bool isGlobal =
false;
547 if (next == g.param || next == g.sparam)
574 for (
const auto &x : this->
options)
575 if (x.id ==
id)
return x;
585 if (idPos < 0 || idPos == std::string::npos)
588 std::vector<std::string> values;
590 for (
size_t i = idPos ; i < this->
getMainArgs().size() ; i++)
600 std::string defaultValue;
603 bool breakOut =
false;
605 for (
size_t i = 0; i < this->
mainArgs->size(); ++i)
607 for (
size_t j = 0 ; j < this->
options.size() ; ++j)
609 if (this->
options[i].subParams.size() > j)
611 if (this->
options[i].subParams[j].
id ==
id)
613 defaultValue = this->
options[i].subParams[j].defaultValue;
628 values.emplace_back(defaultValue);
std::string paramToID(const std::string ¶m)
Normal parameter or sub-paramter to its corresponding ID.
Argx(const std::string &id, int argc, char *argv[])
Create Argx with the specific id, argc and argv
ARGXParam getParam(const std::string &id)
Get the param from id
std::vector< std::string > getSubValue(const std::string &id)
Get sub-parameter values, starting from the first value found until the first found value that corres...
static unsigned int mainArgc
std::vector< ARGXOptions > getOptions() const
Get main set options as ARGXOptions.
static std::vector< std::string > * mainArgs
std::vector< std::string > getMainArgs() const
Get main arguments from main() function argv
std::string getID() const
Get Argx ID.
int getArgPos(const std::string &arg)
Get argument position with specified arg
int getArgIDPos(const std::string &arg)
Get argument using ID.
int getArgc() const
Get the main options from the main() function as argc.
bool paramExists(const std::string &id)
Get if param exists in the param options.
bool hasTag(const std::string &id, const std::string &tag)
Check if the tag exists in option with ID of id
int findParam(const std::string &id)
Find parameter and sub-parameter index.
~Argx()
Deconstruct allocated objects.
std::string createDocs(ARGXStyle style, const std::string &title, const std::string &mainInfo)
Create documentation for the parameters with the specific style, title and main information.
bool subParamExists(const std::string &id)
Get if sub-param exists in the param options.
void add(ARGXOptions option) const
Add param options.
static std::vector< ARGXOptions > options
int getWrongArgs(const std::vector< std::string > &argv)
Get the incorrect arguments and sub-arguments that were not registered.
static int formatWrongArgs(const int &_int)
Format to a positive number if number is negative for a correct execution of code.
bool compareArgs(std::vector< ARGXOptions > options, std::vector< std::string > argv)
Compare if options contains the required id, if the ID does not exist, return false.
bool getSubParam(const argx::ARGXParam ¶m, const std::string &id)
Get the sub-param from id
ARGXOptions getOption(const std::string &id)
Get Options from specified ID.
std::string param
Param (param).
std::vector< ARGXOptions > subParams
std::string sparam
Short Param (sparam):
std::vector< bool > subExists