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;
443 std::unordered_map<std::string, ParamDocEntry> paramInfos;
446 for (
const auto& it : parameterDescriptions)
449 if (paramInfos.find(it.first) == paramInfos.end())
451 paramInfos[it.first] = ParamDocEntry();
453 paramInfos[it.first].description = it.second;
458 for (
const auto& it : parameterAbreviations)
461 if (paramInfos.find(it.second) == paramInfos.end())
463 paramInfos[it.second] = ParamDocEntry();
465 paramInfos[it.second].abbreviation = it.first;
469 for (
const auto& it : parameterConstraints)
472 if (paramInfos.find(it.first) == paramInfos.end())
474 paramInfos[it.first] = ParamDocEntry();
476 ParamDocEntry& cached = paramInfos[it.first];
477 cached.required = it.second.required;
478 cached.typeIsForced = it.second.constrainType;
481 std::stringstream defaultValueSs;
482 for (
const std::string& s : it.second.defaultValue)
484 defaultValueSs <<
'\'' << s <<
'\'';
487 if ((
void*)&s != (
void*)&it.second.defaultValue.back())
488 defaultValueSs <<
" ";
490 cached.defaultVal = defaultValueSs.str();
494 if (paramInfos.size() > 0)
497 <<
"==== AVAILABLE PARAMETERS ===="
498 << std::endl << std::endl;
500 std::size_t counter = 0;
501 for (
const auto& it : paramInfos)
503 const ParamDocEntry& pde = it.second;
506 ss << it.first <<
" ";
509 if (pde.abbreviation.length() > 0)
510 ss << pde.abbreviation <<
" ";
513 if (pde.typeIsForced)
514 ss << pde.type <<
" ";
517 if (pde.defaultVal.length() > 0)
518 ss <<
"default=[" << pde.defaultVal <<
"] ";
521 if ((pde.required) && (pde.defaultVal.length() == 0))
522 ss <<
"[[REQUIRED]] ";
525 if (pde.description.length() > 0)
526 ss << pde.description;
529 if (counter < paramInfos.size()-1)
530 ss << std::endl << std::endl;
539 void CmdArgsInterface::ApplyConstraints()
542 for (
const auto& pc : parameterConstraints)
547 if (pc.second.defaultValue.size() > 0)
550 Value* tmp = ParseValue(pc.second.defaultValue, &pc.second);
551 parameters.insert(std::pair<std::string, Parameter*>(
563 if (pc.second.required)
577 for (
const std::string& incompatibility : pc.second.incompatibleParameters)
578 for (
const std::pair<std::string, Parameter*>& otherParam : parameters)
580 if (otherParam.first == incompatibility)
590 return parameterConstraints.find(parameter)->second;
595 parameterConstraints.erase(parameter);
601 return executableName;
610 return *parameters.find(key)->second->GetValue();
615 parameterAbreviations.insert(std::pair<std::string, std::string>(abbrev, target));
624 return parameterAbreviations.find(abbrev)->second;
629 return parameterAbreviations.find(abbrev) != parameterAbreviations.end();
634 parameterAbreviations.erase(abbrevation);
640 parameterAbreviations.clear();
647 (parameterConstraints[key] = constraint).key = key;
653 parameterConstraints.clear();
659 this->crashOnFail = crashOnFail;
663 const ParamConstraint* CmdArgsInterface::GetConstraintForKey(
const std::string& key)
const
665 const auto constraint = parameterConstraints.find(key);
667 if (constraint == parameterConstraints.end())
670 return &constraint->second;