MCPcopy Create free account
hub / github.com/PDAL/PDAL / parseLongArg

Method parseLongArg

pdal/util/ProgramArgs.hpp:1564–1614  ·  view source on GitHub ↗

Parse an argument specified as a long argument (prefixed with "--") Long arguments with values can be specified as "--name=value" or "--name value". \param name Name of argument specified on command line. \param value Potential value assigned to argument. \return Number of strings consumed (1 for positional arguments or arguments that don't take values or

Source from the content-addressed store, hash-verified

1562 arguments that don't take values or 2 otherwise).
1563 */
1564 int parseLongArg(const std::string& inName, const std::string& inValue)
1565 {
1566 bool attachedValue = false;
1567
1568 if (inName.size() == 2)
1569 throw arg_error("No argument found following '--'.");
1570
1571 std::string name = inName.substr(2);
1572 std::string value = inValue;
1573
1574 std::size_t pos = name.find_first_of("=");
1575 if (pos != std::string::npos)
1576 {
1577 if (pos < name.size() + 1)
1578 {
1579 value = name.substr(pos + 1);
1580 name = name.substr(0, pos);
1581 attachedValue = true;
1582 }
1583 }
1584 else if (value.size() && value[0] == '-')
1585 {
1586 // If a value starts with a '-' and isn't attached to a name,
1587 // we assume it's an option and not a value.
1588 value.clear();
1589 }
1590
1591 Arg *arg = findLongArg(name);
1592 if (!arg)
1593 throw arg_error("Unexpected argument '" + name + "'.");
1594
1595 if (!arg->needsValue())
1596 {
1597 if (attachedValue)
1598 {
1599 if (value != "true" && value != "false")
1600 {
1601 throw arg_error("Value '" + value +
1602 "' provided for argument '" + name +
1603 "' when 'true' or 'false' is expected.");
1604 }
1605 }
1606 else
1607 value = "invert";
1608 arg->setValue(value);
1609 return 1;
1610 }
1611
1612 arg->setValue(value);
1613 return (attachedValue ? 1 : 2);
1614 }
1615
1616 /*
1617 Parse an argument specified as a short argument (prefixed with "-")

Callers

nothing calls this directly

Calls 4

sizeMethod · 0.45
clearMethod · 0.45
needsValueMethod · 0.45
setValueMethod · 0.45

Tested by

no test coverage detected