28 for (
auto& it : parameters)
41 PopulateRawArgs(argc, argv);
44 ExpandAbbreviations();
46 executableName = std::string(rawArgs[0]);
50 while (i < rawArgs.size())
52 if ((rawArgs[i].length() > 2) && (rawArgs[i].substr(0, 2) ==
"--"))
55 i = ParseNextParameter(i, param);
57 parameters.insert(std::pair<std::string, Parameter*>(param->
Key(), param));
66 if ((!catchHelp) || (!
HasParam(
"--help")))
74 std::cerr <<
"Parameter error: " << exc.
What() << std::endl;
85 std::cerr <<
"Parameter error: " << exc.
What() << std::endl;
96 std::cerr <<
"Parameter error: " << exc.
What() << std::endl;
107 std::cerr <<
"Parameter error: " << exc.
What() << std::endl;
118 std::cerr <<
"Parameter error: " << exc.
What() << std::endl;
126 if ((catchHelp) && (
HasParam(
"--help")))
135 std::size_t CmdArgsInterface::ParseNextParameter(
const std::size_t parIndex,
Parameter*& out_Par)
137 std::size_t i = parIndex;
138 const std::string key = rawArgs[parIndex];
139 std::vector<std::string> values;
142 for (i++; i < rawArgs.size(); i++)
144 if ((rawArgs[i].length() < 2) || (rawArgs[i].substr(0, 2) !=
"--"))
145 values.emplace_back(rawArgs[i]);
154 Value* parsedVal = ParseValue(values, pcn);
155 if (parsedVal !=
nullptr)
163 throw std::runtime_error(
"Unable to parse parameter!");
168 void CmdArgsInterface::PopulateRawArgs(
const int argc,
const char*
const* argv)
171 rawArgs.reserve(argc);
173 for (
int i = 0; i < argc; i++)
174 rawArgs.emplace_back(std::string(argv[i]));
179 void CmdArgsInterface::ExpandAbbreviations()
182 if (parameterAbreviations.size() == 0)
185 for (std::string& arg : rawArgs)
188 auto abbr = parameterAbreviations.find(arg);
189 if (abbr != parameterAbreviations.end())
201 return parameters.find(key) != parameters.end();
204 Value* CmdArgsInterface::ParseValue(
const std::vector<std::string>& values,
const ParamConstraint* constraint)
210 const bool constrainType = (constraint !=
nullptr) && (constraint->
constrainType);
213 if (values.size() == 0)
218 if ((constrainType) &&
223 else if ((constrainType) &&
228 else if ((constrainType) &&
243 else if ((constrainType) &&
250 else if (values.size() > 1)
255 if ((constrainType) &&
267 for (
const std::string& val : values)
269 Value* tmp = ParseValue({ val });
277 const std::string& val = values[0];
286 if ((constrainType) &&
293 Value* tmp = ParseValue({ val });
314 if ((constrainType) &&
332 return new IntValue((
long long int)num);
340 Value* tmp = ParseValue({ val });
352 return new IntValue((
long long int)num);
370 this->catchHelp = catchHelp;
381 briefDescription = description;
387 return briefDescription;
392 parameterDescriptions[parameter] = description;
399 if (!HasDescription(parameter))
404 return parameterDescriptions.find(parameter)->second;
409 return parameterDescriptions.find(parameter) != parameterDescriptions.end();
415 parameterDescriptions.erase(parameter);
421 parameterDescriptions.clear();
427 std::stringstream ss;
430 if (briefDescription.length() > 0)
431 ss << briefDescription << std::endl;
436 std::string abbreviation;
437 std::string description;
439 bool required =
false;
440 bool typeIsForced =
false;
441 std::string defaultVal;
442 std::string incompatibilities;
444 std::unordered_map<std::string, ParamDocEntry> paramInfos;
447 for (
const auto& it : parameterDescriptions)
450 if (paramInfos.find(it.first) == paramInfos.end())
452 paramInfos[it.first] = ParamDocEntry();
454 paramInfos[it.first].description = it.second;
459 for (
const auto& it : parameterAbreviations)
462 if (paramInfos.find(it.second) == paramInfos.end())
464 paramInfos[it.second] = ParamDocEntry();
466 paramInfos[it.second].abbreviation = it.first;
470 for (
const auto& it : parameterConstraints)
473 if (paramInfos.find(it.first) == paramInfos.end())
475 paramInfos[it.first] = ParamDocEntry();
477 ParamDocEntry& cached = paramInfos[it.first];
478 cached.required = it.second.required;
479 cached.typeIsForced = it.second.constrainType;
483 std::stringstream vec2str_ss;
484 for (
const std::string& s : it.second.defaultValue)
486 vec2str_ss <<
'\'' << s <<
'\'';
489 if ((
void*)&s != (
void*)&it.second.defaultValue.back())
492 cached.defaultVal = vec2str_ss.str();
497 for (
const std::string& s : it.second.incompatibleParameters)
502 if ((
void*)&s != (
void*)&it.second.incompatibleParameters.back())
505 cached.incompatibilities = vec2str_ss.str();
509 if (paramInfos.size() > 0)
512 <<
"==== AVAILABLE PARAMETERS ===="
513 << std::endl << std::endl;
515 std::size_t counter = 0;
516 for (
const auto& it : paramInfos)
518 const ParamDocEntry& pde = it.second;
521 ss << it.first <<
" ";
524 if (pde.abbreviation.length() > 0)
525 ss << pde.abbreviation <<
" ";
528 if (pde.typeIsForced)
529 ss << pde.type <<
" ";
532 if (pde.defaultVal.length() > 0)
533 ss <<
"default=[" << pde.defaultVal <<
"] ";
536 if (pde.incompatibilities.length() > 0)
537 ss <<
"incompatibilities=[" << pde.incompatibilities <<
"] ";
540 if ((pde.required) && (pde.defaultVal.length() == 0))
541 ss <<
"[[REQUIRED]] ";
544 if (pde.description.length() > 0)
545 ss << pde.description;
548 if (counter < paramInfos.size()-1)
549 ss << std::endl << std::endl;
558 void CmdArgsInterface::ApplyConstraints()
561 for (
const auto& pc : parameterConstraints)
566 if (pc.second.defaultValue.size() > 0)
569 Value* tmp = ParseValue(pc.second.defaultValue, &pc.second);
570 parameters.insert(std::pair<std::string, Parameter*>(
582 if (pc.second.required)
596 for (
const std::string& incompatibility : pc.second.incompatibleParameters)
597 for (
const std::pair<std::string, Parameter*>& otherParam : parameters)
599 if (otherParam.first == incompatibility)
609 return parameterConstraints.find(parameter)->second;
614 parameterConstraints.erase(parameter);
620 return executableName;
629 return *parameters.find(key)->second->GetValue();
634 parameterAbreviations.insert(std::pair<std::string, std::string>(abbrev, target));
643 return parameterAbreviations.find(abbrev)->second;
648 return parameterAbreviations.find(abbrev) != parameterAbreviations.end();
653 parameterAbreviations.erase(abbrevation);
659 parameterAbreviations.clear();
666 (parameterConstraints[key] = constraint).key = key;
672 parameterConstraints.clear();
678 this->crashOnFail = crashOnFail;
682 const ParamConstraint* CmdArgsInterface::GetConstraintForKey(
const std::string& key)
const
684 const auto constraint = parameterConstraints.find(key);
686 if (constraint == parameterConstraints.end())
689 return &constraint->second;