Argx 1.2.2-build
Simple yet powerful argument parser made in C++
Loading...
Searching...
No Matches
argx::Argx Class Reference

#include <Argx.hpp>

Public Member Functions

 Argx ()
 Argx (const std::string &id, int argc, char *argv[])
 Create Argx with the specific id, argc and argv
 ~Argx ()
 Deconstruct allocated objects.
void add (ARGXOptions option) const
 Add param options.
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.
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.
int findParam (const std::string &id)
 Find parameter and sub-parameter index.
int getArgc () const
 Get the main options from the main() function as argc.
int getArgIDPos (const std::string &arg)
 Get argument using ID.
int getArgPos (const std::string &arg)
 Get argument position with specified arg
std::string getID () const
 Get Argx ID.
std::vector< std::string > getMainArgs () const
 Get main arguments from main() function argv
ARGXOptions getOption (const std::string &id)
 Get Options from specified ID.
std::vector< ARGXOptionsgetOptions () const
 Get main set options as ARGXOptions.
ARGXParam getParam (const std::string &id)
 Get the param from id
bool getSubParam (const argx::ARGXParam &param, const std::string &id)
 Get the sub-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 corresponds to a registered parameter.
int getWrongArgs (const std::vector< std::string > &argv)
 Get the incorrect arguments and sub-arguments that were not registered.
bool hasTag (const std::string &id, const std::string &tag)
 Check if the tag exists in option with ID of id
bool paramExists (const std::string &id)
 Get if param exists in the param options.
std::string paramToID (const std::string &param)
 Normal parameter or sub-paramter to its corresponding ID.
bool subParamExists (const std::string &id)
 Get if sub-param exists in the param options.

Static Public Member Functions

static int formatWrongArgs (const int &_int)
 Format to a positive number if number is negative for a correct execution of code.

Private Attributes

std::string id

Static Private Attributes

static unsigned int mainArgc
static std::vector< std::string > * mainArgs = nullptr
static std::vector< ARGXOptionsoptions

Detailed Description

Definition at line 17 of file Argx.hpp.

Constructor & Destructor Documentation

◆ Argx() [1/2]

argx::Argx::Argx ( const std::string & id,
int argc,
char * argv[] )

Create Argx with the specific id, argc and argv

Parameters
idSet the ID of the Argx
argcSet the argc of the main() function
argvSet the argv of the main() function

Definition at line 34 of file Argx.cpp.

35 : id(id)
36 {
37 this->mainArgs = new std::vector<std::string>(argv, argv + argc);
38 this->mainArgc = argc;
39
40 if (!this->mainArgs)
41 std::cerr << "`Args::mainArgs` is not valid for ID of " + id + " variable is NULL";
42 }
static unsigned int mainArgc
Definition Argx.hpp:25
static std::vector< std::string > * mainArgs
Definition Argx.hpp:23
std::string id
Definition Argx.hpp:20

References Argx(), id, mainArgc, and mainArgs.

Referenced by Argx().

◆ Argx() [2/2]

argx::Argx::Argx ( )

Definition at line 45 of file Argx.cpp.

46 { }

◆ ~Argx()

argx::Argx::~Argx ( )

Deconstruct allocated objects.

Definition at line 48 of file Argx.cpp.

49 {
50 if (this->mainArgs) { delete this->mainArgs; this->mainArgs = nullptr; }
51 }

References mainArgs.

Member Function Documentation

◆ add()

void argx::Argx::add ( ARGXOptions option) const

Add param options.

Parameters
optionAdd the option to the main params

Definition at line 93 of file Argx.cpp.

94 { this->options.emplace_back(option); }
static std::vector< ARGXOptions > options
Definition Argx.hpp:22

References options.

◆ compareArgs()

bool argx::Argx::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.

Parameters
optionsReturn ARGXOptions vector
idID to find
Returns
bool

Definition at line 489 of file Argx.cpp.

490 {
491 // iterate over argv and skip program name
492 for (size_t i = 1; i < argv.size(); ++i)
493 {
494 const std::string &arg = argv[i];
495
496 // find a matching top-level option
497 const ARGXOptions *matched = nullptr;
498
499 for (const auto &opt : options)
500 {
501 if (opt.sparam == arg || opt.param == arg)
502 {
503 matched = &opt;
504
505 break;
506 }
507 }
508
509 // No matched top-level option
510 if (!matched) return false;
511
512 // if option supports subparams, try to get them
513 if (matched->hasSubParams || matched->hasAnySubParams)
514 {
515 size_t j = i + 1;
516
517 while (j < argv.size())
518 {
519 const std::string &next = argv[j];
520
521 // Check the next declared subparam of `matched`
522 bool isSub = false;
523
524 for (const auto &sub : matched->subParams)
525 {
526 if (next == sub.param || next == sub.sparam)
527 {
528 isSub = true;
529
530 break;
531 }
532 }
533
534 // Get it and continue scanning for more subparams
535 if (isSub)
536 {
537 ++j;
538
539 continue;
540 }
541
542 // not a subparam then check if it's a known top-level option
543 bool isGlobal = false;
544
545 for (const auto &g : options)
546 {
547 if (next == g.param || next == g.sparam)
548 {
549 isGlobal = true;
550 break;
551 }
552 }
553
554
555 // stop scanning subparams; outer loop will handle this global option
556 if (isGlobal) break;
557
558 // neither a subparam nor a global option to an invalid sequence
559 return false;
560 }
561
562 // advance outer index to the last consumed token ( j - 1 ).
563 // outer for-loop will increment i, so set `i = j - 1` to continue at j
564 if (j > i + 1)
565 i = j - 1;
566 }
567 }
568
569 return true;
570 }
struct argx::ARGXOptions ARGXOptions

References argx::ARGXOptions::hasAnySubParams, argx::ARGXOptions::hasSubParams, options, and argx::ARGXOptions::subParams.

◆ createDocs()

std::string argx::Argx::createDocs ( ARGXStyle style,
const std::string & title,
const std::string & mainInfo )

Create documentation for the parameters with the specific style, title and main information.

Parameters
styleSet the style using ARGXStyle
titleTitle for docs
Maininformation
Returns
std::string Documentation as a string

Definition at line 293 of file Argx.cpp.

294 {
295 std::string contentStr;
296
297 if (style == ARGXStyle::Professional)
298 {
299 for (const auto &x : this->options)
300 {
301 // Main option header line
302 contentStr += "ID: " + x.id + "\n";
303 contentStr += "[ " + x.sparam + " | " + x.param;
304
305 if (x.hasSubParams && !x.subParams.empty())
306 {
307 contentStr += " [ ";
308
309 for (size_t i = 0; i < x.subParams.size(); ++i)
310 {
311 const auto &sub = x.subParams[i];
312
313 contentStr += sub.param;
314
315 if (i < x.subParams.size() - 1) contentStr += " | ";
316 else if (i <= x.subParams.size()) contentStr += ' ';
317 }
318
319 contentStr += "] ] ";
320 }
321
322 else contentStr += " ] ";
323
324 contentStr += x.info + "\n";
325
326 // Print all sub-options with sparam and param, aligned with ideographic spaces if there are
327 if (x.hasSubParams && !x.subParams.empty())
328 {
329 for (const auto &sub : x.subParams)
330 {
331 // Create ideographic spaces matching the length of main param for alignment
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);
335
336 contentStr += spacing + " [ " + sub.sparam + " | " + sub.param + " ] " + sub.info + "\n";
337 }
338 }
339 }
340 }
341
342 else if (style == ARGXStyle::Simple)
343 {
344 for (const auto &x : this->options)
345 {
346 contentStr += x.sparam + ", " + x.param + " - " + x.info + "\n";
347
348 if (x.hasSubParams && !x.subParams.empty())
349 {
350 for (const auto &sub : x.subParams)
351 {
352 contentStr += " " + sub.sparam + ", " + sub.param + " - " + sub.info + "\n";
353 }
354 }
355 }
356 } // ARGXStyle
357
358 return title + "\n" + mainInfo + "\n" + contentStr;
359 }

References options, argx::Professional, and argx::Simple.

◆ findParam()

int argx::Argx::findParam ( const std::string & id)

Find parameter and sub-parameter index.

Parameters
idID to find
Returns
int Index

Definition at line 96 of file Argx.cpp.

97 {
98 // First check if it's a main parameter
99 for (size_t i = 0; i < this->options.size(); i++)
100 {
101 if (this->options[i].id == id)
102 {
103 // Check if this main parameter exists in arguments
104 for (const std::string &arg : *this->mainArgs)
105 {
106 if (arg == this->options[i].param || arg == this->options[i].sparam)
107 {
108 return static_cast<int>(i);
109 }
110 }
111 }
112 }
113
114 // Then look for sub-parameters
115 for (const auto &opt : this->options)
116 {
117 // Check if the parent option exists in the arguments
118 bool parentExists = false;
119
120 for (const std::string &arg : *this->mainArgs)
121 {
122 if (arg == opt.param || arg == opt.sparam)
123 {
124 parentExists = true;
125 break;
126 }
127 }
128
129 if (parentExists)
130 {
131 // Find the index of the requested sub-parameter
132 for (size_t i = 0; i < opt.subParams.size(); i++)
133 {
134 if (opt.subParams[i].id == id) return static_cast<int>(i);
135 }
136 }
137 }
138
139 return -1; // Not found
140 }

References mainArgs, and options.

Referenced by getSubParam(), hasTag(), and paramExists().

◆ formatWrongArgs()

int argx::Argx::formatWrongArgs ( const int & _int)
static

Format to a positive number if number is negative for a correct execution of code.

Parameters
_intNumber to convert
Returns
int Positive value number

Definition at line 482 of file Argx.cpp.

483 {
484 if (_int < 0) return -_int; // Convert to unsigned SAFELY
485
486 return _int;
487 }

◆ getArgc()

int argx::Argx::getArgc ( ) const

Get the main options from the main() function as argc.

Returns
int Number of params including the executable param

Definition at line 636 of file Argx.cpp.

637 { return this->mainArgc; }

References mainArgc.

◆ getArgIDPos()

int argx::Argx::getArgIDPos ( const std::string & arg)

Get argument using ID.

Parameters
argArgument to find
Returns
int Argument position

Definition at line 53 of file Argx.cpp.

54 {
55 ARGXOptions option = this->getOption(arg);
56
57 int argPos = this->getArgPos(option.param);
58 int shortArgPos = this->getArgPos(option.sparam);
59
60 if (argPos >= 0) return argPos;
61 if (shortArgPos >= 0) return shortArgPos;
62
63 return -1;
64 }
int getArgPos(const std::string &arg)
Get argument position with specified arg
Definition Argx.cpp:79
ARGXOptions getOption(const std::string &id)
Get Options from specified ID.
Definition Argx.cpp:572

References getArgPos(), getOption(), argx::ARGXOptions::param, and argx::ARGXOptions::sparam.

◆ getArgPos()

int argx::Argx::getArgPos ( const std::string & arg)

Get argument position with specified arg

Parameters
argFind argument
Returns
int Return position of found arg from the options

Definition at line 79 of file Argx.cpp.

80 {
81 if (!this->mainArgs)
82 return -2;
83
84 for (size_t i = 0; i < this->mainArgs->size(); ++i)
85 {
86 if (this->mainArgs->at(i) == arg)
87 return i;
88 }
89
90 return -1;
91 }

References mainArgs.

Referenced by getArgIDPos(), and getSubValue().

◆ getID()

std::string argx::Argx::getID ( ) const

Get Argx ID.

Returns
std::string Argx ID

Definition at line 642 of file Argx.cpp.

643 { return this->id; }

◆ getMainArgs()

std::vector< std::string > argx::Argx::getMainArgs ( ) const

Get main arguments from main() function argv

Returns
std::vector<std::string> Vector of strings for main arguments from argv

Definition at line 633 of file Argx.cpp.

634 { return *this->mainArgs; }

References mainArgs.

Referenced by getSubValue().

◆ getOption()

ARGXOptions argx::Argx::getOption ( const std::string & id)

Get Options from specified ID.

Parameters
idID to find
ARGXOptionsOption information

Definition at line 572 of file Argx.cpp.

573 {
574 for (const auto &x : this->options)
575 if (x.id == id) return x;
576
577 return {};
578 }

References options.

Referenced by getArgIDPos(), and getSubValue().

◆ getOptions()

std::vector< ARGXOptions > argx::Argx::getOptions ( ) const

Get main set options as ARGXOptions.

Returns
std::vector<ARGXOptions> Options to return

Definition at line 639 of file Argx.cpp.

640 { return this->options; }

References options.

◆ getParam()

ARGXParam argx::Argx::getParam ( const std::string & id)

Get the param from id

Parameters
idThe ID to get
Returns
ARGXParam Returnted parameter to get

Definition at line 171 of file Argx.cpp.

172 {
173 if (this->mainArgc <= 1) return {};
174
175 ARGXParam result;
176
177 // First, check if this is a top-level option
178 for (const auto &opt : this->options)
179 {
180 if (opt.id == id)
181 {
182 // Find the position of the main option in arguments
183 int mainOptionPos = -1;
184
185 for (size_t i = 0; i < this->mainArgs->size(); ++i)
186 {
187 if ((*this->mainArgs)[i] == opt.param || (*this->mainArgs)[i] == opt.sparam)
188 {
189 result.exists = true;
190 mainOptionPos = i;
191 break;
192 }
193 }
194
195 if (result.exists)
196 {
197 if (opt.hasSubParams || opt.hasAnySubParams)
198 {
199 // Check each sub-parameter
200 for (const auto &sub : opt.subParams)
201 {
202 bool subMatched = false;
203
204 // Look for sub-parameters after the main option
205 for (size_t i = mainOptionPos + 1; i < this->mainArgs->size(); ++i)
206 {
207 if ((*this->mainArgs)[i] == sub.param || (*this->mainArgs)[i] == sub.sparam)
208 {
209 subMatched = true;
210 break;
211 }
212 }
213
214 result.subExists.push_back(subMatched);
215 }
216 }
217
218 return result;
219 }
220 }
221 }
222
223 // If not found as top-level, check if it's a sub-parameter
224 for (const auto &opt : this->options)
225 {
226 // Find if the parent option exists and get its position
227 size_t parentPos = -1;
228
229 for (size_t i = 0; i < this->mainArgs->size(); ++i)
230 {
231 if ((*this->mainArgs)[i] == opt.param || (*this->mainArgs)[i] == opt.sparam)
232 {
233 parentPos = i;
234 break;
235 }
236 }
237
238 if (parentPos > -1 && (opt.hasSubParams || opt.hasAnySubParams))
239 {
240 // Check if the requested sub-parameter exists after the parent
241 for (const auto &sub : opt.subParams)
242 {
243 if (sub.id == id)
244 {
245 for (size_t i = parentPos + 1 ; i < this->mainArgs->size(); ++i)
246 {
247 if ((*this->mainArgs)[i] == sub.param || (*this->mainArgs)[i] == sub.sparam)
248 {
249 result.exists = true;
250 break;
251 }
252 }
253
254 if (!result.exists && parentPos + 1 < this->mainArgs->size())
255 {
256 std::string nextArg = (*this->mainArgs)[parentPos + 1];
257
258 if (nextArg == sub.param || nextArg == sub.sparam) result.exists = true;
259 }
260
261 // Handle any sub-sub-parameters if they exist
262 if (result.exists && (sub.hasSubParams || sub.hasAnySubParams))
263 {
264 for (const auto &subsub : sub.subParams)
265 {
266 bool subsubMatched = false;
267
268 for (size_t i = 0; i < this->mainArgs->size(); ++i)
269 {
270 if ((*this->mainArgs)[i] == subsub.param || (*this->mainArgs)[i] == subsub.sparam)
271 {
272 subsubMatched = true;
273 break;
274 }
275 }
276
277 result.subExists.push_back(subsubMatched);
278 }
279 }
280
281 return result;
282 }
283 }
284 }
285 }
286
287 return result;
288 }
struct argx::ARGXParam ARGXParam

References argx::ARGXParam::exists, mainArgc, mainArgs, options, and argx::ARGXParam::subExists.

◆ getSubParam()

bool argx::Argx::getSubParam ( const argx::ARGXParam & param,
const std::string & id )

Get the sub-param from id

Parameters
paramOriginal param
idThe ID to get
Returns
bool

Definition at line 290 of file Argx.cpp.

291 { return this->paramExists(id) && param.subExists[this->findParam(id)]; }
bool paramExists(const std::string &id)
Get if param exists in the param options.
Definition Argx.cpp:142
int findParam(const std::string &id)
Find parameter and sub-parameter index.
Definition Argx.cpp:96
std::vector< bool > subExists
Definition types.hpp:60

References findParam(), paramExists(), and argx::ARGXParam::subExists.

◆ getSubValue()

std::vector< std::string > argx::Argx::getSubValue ( const std::string & id)

Get sub-parameter values, starting from the first value found until the first found value that corresponds to a registered parameter.

Parameters
idID to find to get sub-value
Returns
std::vector<std::string> Values found from first to last

Definition at line 580 of file Argx.cpp.

581 {
582 // Use `Argx::getArgPos()` function for sub-params
583 size_t idPos = this->getArgPos(id) + 1;
584
585 if (idPos < 0 || idPos == std::string::npos)
586 return {this->getOption(id).defaultValue};
587
588 std::vector<std::string> values;
589
590 for (size_t i = idPos ; i < this->getMainArgs().size() ; i++)
591 {
592 // End of the sub-parameter finding
593 // Assume the search is done due to an existsing sub-param
594 if (i != idPos && this->subParamExists(this->getMainArgs()[i]))
595 break;
596
597 values.emplace_back(this->getMainArgs()[i]);
598 }
599
600 std::string defaultValue;
601
602 {
603 bool breakOut = false;
604
605 for (size_t i = 0; i < this->mainArgs->size(); ++i)
606 {
607 for (size_t j = 0 ; j < this->options.size() ; ++j)
608 {
609 if (this->options[i].subParams.size() > j)
610 {
611 if (this->options[i].subParams[j].id == id)
612 {
613 defaultValue = this->options[i].subParams[j].defaultValue;
614
615
616 breakOut = true;
617
618 break;
619 }
620 }
621 }
622
623 if (breakOut) break;
624 }
625 }
626
627 if (values.empty())
628 values.emplace_back(defaultValue);
629
630 return values;
631 }
std::vector< std::string > getMainArgs() const
Get main arguments from main() function argv
Definition Argx.cpp:633
bool subParamExists(const std::string &id)
Get if sub-param exists in the param options.
Definition Argx.cpp:149
std::string defaultValue
Definition types.hpp:48

References argx::ARGXOptions::defaultValue, getArgPos(), getMainArgs(), getOption(), mainArgs, options, and subParamExists().

◆ getWrongArgs()

int argx::Argx::getWrongArgs ( const std::vector< std::string > & argv)

Get the incorrect arguments and sub-arguments that were not registered.

Parameters
argvMain arguments from argv
Returns
int Argument position

Definition at line 395 of file Argx.cpp.

396 {
397 int pos = 1; // skip program name
398 bool isNormalParam = true;
399
400 while (pos < (int)argv.size())
401 {
402 const std::string &arg = argv[pos];
403
404 // Find matching top-level option
405 const ARGXOptions *matched = nullptr;
406
407 for (const auto &opt : this->options)
408 {
409 if (arg == opt.param || arg == opt.sparam)
410 {
411 matched = &opt;
412 break;
413 }
414 }
415
416 // unknown top-level arg
417 if (!matched) return (isNormalParam ? pos : -pos);
418
419 // matched a top-level option
420 isNormalParam = true;
421
422 if (matched->hasSubParams || matched->hasAnySubParams)
423 {
424 isNormalParam = false;
425
426 int scanPos = pos + 1;
427
428 while (scanPos < (int)argv.size())
429 {
430 const std::string &nextArg = argv[scanPos];
431
432 // Is it a declared subparam for this option?
433 bool isSub = false;
434
435 for (const auto &subOpt : matched->subParams)
436 {
437 if (nextArg == subOpt.param || nextArg == subOpt.sparam)
438 {
439 isSub = true;
440 break;
441 }
442 }
443
444 if (isSub)
445 {
446 // Consume that subparam and continue scanning
447 pos = scanPos;
448 ++scanPos;
449
450 continue;
451 }
452
453 // If not a subparam, is it a global option? (do **NOT** consume it)
454 bool isGlobalOpt = false;
455
456 for (const auto &globalOpt : this->options)
457 {
458 if (nextArg == globalOpt.param || nextArg == globalOpt.sparam)
459 {
460 isGlobalOpt = true;
461
462 break;
463 }
464 }
465
466 // valid global option follows; stop subparam scan and let outer loop handle it
467 if (isGlobalOpt) break;
468
469 // neither subparam nor global option; it's an invalid sub-parameter token
470 return -scanPos;
471 }
472 }
473
474 ++pos;
475 }
476
477 // If nothing wrong found; return your existing success codes
478 if (isNormalParam) return (this->mainArgs->size() > 2 ? 2 : 1);
479 else return (this->mainArgs->size() > 2 ? -2 : -1);
480 }

References argx::ARGXOptions::hasAnySubParams, argx::ARGXOptions::hasSubParams, mainArgs, options, and argx::ARGXOptions::subParams.

◆ hasTag()

bool argx::Argx::hasTag ( const std::string & id,
const std::string & tag )

Check if the tag exists in option with ID of id

Parameters
idID from option
tagTag to find
Returns
bool Return false if there is no match, else, return true

Definition at line 159 of file Argx.cpp.

160 {
161 int paramID = this->findParam(id);
162
163 if (paramID < 0) return false;
164
165 // Validate if tag from options is equal to this tag from function param
166 if (this->options[paramID].tag == tag) return true;
167
168 return false;
169 }

References findParam(), and options.

◆ paramExists()

bool argx::Argx::paramExists ( const std::string & id)

Get if param exists in the param options.

Parameters
idID to get
Returns
bool

Definition at line 142 of file Argx.cpp.

143 {
144 if (this->findParam(id) >= 0) return true;
145
146 return false;
147 }

References findParam().

Referenced by getSubParam().

◆ paramToID()

std::string argx::Argx::paramToID ( const std::string & param)

Normal parameter or sub-paramter to its corresponding ID.

Parameters
paramParameter value or name
Returns
std::string ID of the param or sub-param

Definition at line 66 of file Argx.cpp.

67 {
68 std::string id;
69
70 for (const auto &option : this->options)
71 {
72 if (option.param == param || option.sparam == param)
73 return option.id;
74 }
75
76 return id;
77 }

References id, and options.

◆ subParamExists()

bool argx::Argx::subParamExists ( const std::string & id)

Get if sub-param exists in the param options.

Parameters
idID to get
Returns
bool

Definition at line 149 of file Argx.cpp.

150 {
151 for (const auto &p : this->options)
152 {
153 if (p.sparam == id) return true;
154 }
155
156 return false;
157 }

References options.

Referenced by getSubValue().

Member Data Documentation

◆ id

std::string argx::Argx::id
private

Definition at line 20 of file Argx.hpp.

Referenced by Argx(), and paramToID().

◆ mainArgc

unsigned int argx::Argx::mainArgc
staticprivate

Definition at line 25 of file Argx.hpp.

Referenced by Argx(), getArgc(), and getParam().

◆ mainArgs

std::vector< std::string > * argx::Argx::mainArgs = nullptr
staticprivate

Definition at line 23 of file Argx.hpp.

Referenced by Argx(), ~Argx(), findParam(), getArgPos(), getMainArgs(), getParam(), getSubValue(), and getWrongArgs().

◆ options

std::vector< ARGXOptions > argx::Argx::options
staticprivate

The documentation for this class was generated from the following files: