156 lines
4.2 KiB
C++
156 lines
4.2 KiB
C++
#include "options.hpp"
|
|
#include <cassert>
|
|
|
|
int main()
|
|
{
|
|
/* Skeleton.
|
|
|
|
{
|
|
std::array l{…};
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
|
|
|
assert((oper == options::Values{…}));
|
|
assert((opts == options::Dictionary{
|
|
{"option", {"value", …}},
|
|
…
|
|
}));
|
|
}
|
|
*/
|
|
|
|
/* Empty. */
|
|
{
|
|
std::array<const char*, 0> l{};
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
|
|
|
assert((oper == options::Values{}));
|
|
assert((opts == options::Dictionary{}));
|
|
}
|
|
|
|
/* Operands. */
|
|
{
|
|
std::array l{"all", "these", "are", "operands"};
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
|
|
|
assert((oper == options::Values{"all", "these", "are", "operands"}));
|
|
assert((opts == options::Dictionary{}));
|
|
}
|
|
|
|
/* Short Unix options. */
|
|
{
|
|
std::array l{"-a", "-b", "-c"};
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
|
|
|
assert((oper == options::Values{}));
|
|
assert((opts == options::Dictionary{
|
|
{"a", {}},
|
|
{"b", {}},
|
|
{"c", {}}
|
|
}));
|
|
}
|
|
|
|
/* Short Unix options with arguments. */
|
|
{
|
|
std::array l{"-v", "value", "not-a-value", "-w", "-v", "other"};
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
|
|
|
assert((oper == options::Values{"not-a-value"}));
|
|
assert((opts == options::Dictionary{
|
|
{"v", {"value", "other"}},
|
|
{"w", {}}
|
|
}));
|
|
}
|
|
|
|
/* Short Unix options shorthand. */
|
|
{
|
|
std::array l{"-abcdef", "value", "not-a-value"};
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
|
|
|
assert((oper == options::Values{"not-a-value"}));
|
|
assert((opts == options::Dictionary{
|
|
{"a", {}},
|
|
{"b", {}},
|
|
{"c", {}},
|
|
{"d", {}},
|
|
{"e", {}},
|
|
{"f", {"value"}}
|
|
}));
|
|
}
|
|
|
|
/* Long GNU options. */
|
|
{
|
|
std::array l{"--long", "--option"};
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
|
|
|
assert((oper == options::Values{}));
|
|
assert((opts == options::Dictionary{
|
|
{"long", {}},
|
|
{"option", {}}
|
|
}));
|
|
}
|
|
|
|
/* Long GNU options with arguments separated by whitespace. */
|
|
{
|
|
std::array l{"--value", "v", "--value", "-v", "value", "--value"};
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
|
|
|
assert((oper == options::Values{}));
|
|
assert((opts == options::Dictionary{
|
|
{"v", {"value"}},
|
|
{"value", {"v"}}
|
|
}));
|
|
}
|
|
|
|
/* Long GNU options with arguments separated by equals sign. */
|
|
{
|
|
std::array l{"--value=-2", "--value=-1", "--value=0"};
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
|
|
|
assert((oper == options::Values{}));
|
|
assert((opts == options::Dictionary{
|
|
{"value", {"-2", "-1", "0"}}
|
|
}));
|
|
}
|
|
|
|
/* Single dash as option-argument. */
|
|
{
|
|
std::array l{"--output", "-", "--input", "-", "-"};
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
|
|
|
assert((oper == options::Values{"-"}));
|
|
assert((opts == options::Dictionary{
|
|
{"output", {"-"}},
|
|
{"input", {"-"}}
|
|
}));
|
|
}
|
|
|
|
/* Long, short and shorthand options mixed. */
|
|
{
|
|
std::array l{"-abc", "content", "--long", "-short", "tree", "out"};
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
|
|
|
assert((oper == options::Values{"out"}));
|
|
assert((opts == options::Dictionary{
|
|
{"a", {}},
|
|
{"b", {}},
|
|
{"c", {"content"}},
|
|
{"long", {}},
|
|
{"s", {}},
|
|
{"h", {}},
|
|
{"o", {}},
|
|
{"r", {}},
|
|
{"t", {"tree"}}
|
|
}));
|
|
}
|
|
|
|
/* Operand separator. */
|
|
{
|
|
std::array l{"--option", "--", "--not-option"};
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
|
|
|
assert((oper == options::Values{"--not-option"}));
|
|
assert((opts == options::Dictionary{
|
|
{"option", {}}
|
|
}));
|
|
}
|
|
}
|