MCPcopy Create free account
hub / github.com/Tablecruncher/tablecruncher / parseIntOption

Function parseIntOption

external/utf8-cpp-utils/test/doctest.h:6555–6587  ·  view source on GitHub ↗

parses an int/bool option from the command line

Source from the content-addressed store, hash-verified

6553
6554 // parses an int/bool option from the command line
6555 bool parseIntOption(int argc, const char* const* argv, const char* pattern, optionType type,
6556 int& res) {
6557 String parsedValue;
6558 if(!parseOption(argc, argv, pattern, &parsedValue))
6559 return false;
6560
6561 if(type) {
6562 // integer
6563 // TODO: change this to use std::stoi or something else! currently it uses undefined behavior - assumes '0' on failed parse...
6564 int theInt = std::atoi(parsedValue.c_str());
6565 if (theInt != 0) {
6566 res = theInt; //!OCLINT parameter reassignment
6567 return true;
6568 }
6569 } else {
6570 // boolean
6571 const char positive[][5] = { "1", "true", "on", "yes" }; // 5 - strlen("true") + 1
6572 const char negative[][6] = { "0", "false", "off", "no" }; // 6 - strlen("false") + 1
6573
6574 // if the value matches any of the positive/negative possibilities
6575 for (unsigned i = 0; i < 4; i++) {
6576 if (parsedValue.compare(positive[i], true) == 0) {
6577 res = 1; //!OCLINT parameter reassignment
6578 return true;
6579 }
6580 if (parsedValue.compare(negative[i], true) == 0) {
6581 res = 0; //!OCLINT parameter reassignment
6582 return true;
6583 }
6584 }
6585 }
6586 return false;
6587 }
6588} // namespace
6589
6590Context::Context(int argc, const char* const* argv)

Callers

nothing calls this directly

Calls 1

parseOptionFunction · 0.85

Tested by

no test coverage detected