Parse an argument specified as a short argument (prefixed with "-") Short arguments with values are specified as "-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 2 otherwise). */
| 1623 | arguments that don't take values or 2 otherwise). |
| 1624 | */ |
| 1625 | int parseShortArg(const std::string& name, const std::string& value) |
| 1626 | { |
| 1627 | if (name.size() == 1) |
| 1628 | throw arg_error("No argument found following '-'."); |
| 1629 | assert(name.size() == 2); |
| 1630 | |
| 1631 | Arg *arg = findShortArg(name[1]); |
| 1632 | if (!arg) |
| 1633 | throw arg_error("Unexpected argument '-" + std::string(1, name[1]) + |
| 1634 | "'."); |
| 1635 | |
| 1636 | int cnt; |
| 1637 | if (arg->needsValue()) |
| 1638 | { |
| 1639 | // If the value starts with a '-', assume it's an option |
| 1640 | // rather than a value. |
| 1641 | if (value.empty() || value[0] == '-') |
| 1642 | { |
| 1643 | throw arg_error("Short option '" + name + "' expects value " |
| 1644 | "but none directly follows."); |
| 1645 | } |
| 1646 | else |
| 1647 | { |
| 1648 | cnt = 2; |
| 1649 | arg->setValue(value); |
| 1650 | } |
| 1651 | } |
| 1652 | else |
| 1653 | { |
| 1654 | arg->setValue("true"); |
| 1655 | cnt = 1; |
| 1656 | } |
| 1657 | return cnt; |
| 1658 | } |
| 1659 | |
| 1660 | /* |
| 1661 | Make sure we don't have any required positional args after |
nothing calls this directly
no test coverage detected