2019-08-08 17:54:29 +00:00
|
|
|
#include "options.hpp"
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
/* Skeleton.
|
|
|
|
|
|
|
|
{
|
2020-08-18 20:07:51 +00:00
|
|
|
std::array l{…};
|
|
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
2019-08-08 17:54:29 +00:00
|
|
|
|
2020-08-18 20:07:51 +00:00
|
|
|
assert((oper == options::Values{…}));
|
2019-08-08 17:54:29 +00:00
|
|
|
assert((opts == options::Dictionary{
|
2020-08-18 20:07:51 +00:00
|
|
|
{"option", {"value", …}},
|
2019-08-08 17:54:29 +00:00
|
|
|
…
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2020-08-18 20:07:51 +00:00
|
|
|
/* 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{}));
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:54:29 +00:00
|
|
|
/* Operands. */
|
|
|
|
{
|
2020-08-18 20:07:51 +00:00
|
|
|
std::array l{"all", "these", "are", "operands"};
|
|
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
2019-08-08 17:54:29 +00:00
|
|
|
|
2020-08-18 20:07:51 +00:00
|
|
|
assert((oper == options::Values{"all", "these", "are", "operands"}));
|
|
|
|
assert((opts == options::Dictionary{}));
|
2019-08-08 17:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Short Unix options. */
|
|
|
|
{
|
2020-08-18 20:07:51 +00:00
|
|
|
std::array l{"-a", "-b", "-c"};
|
|
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
2019-08-08 17:54:29 +00:00
|
|
|
|
2020-08-18 20:07:51 +00:00
|
|
|
assert((oper == options::Values{}));
|
2019-08-08 17:54:29 +00:00
|
|
|
assert((opts == options::Dictionary{
|
|
|
|
{"a", {}},
|
|
|
|
{"b", {}},
|
|
|
|
{"c", {}}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Short Unix options with arguments. */
|
|
|
|
{
|
2020-08-18 20:07:51 +00:00
|
|
|
std::array l{"-v", "value", "not-a-value", "-w", "-v", "other"};
|
|
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
2019-08-08 17:54:29 +00:00
|
|
|
|
2020-08-18 20:07:51 +00:00
|
|
|
assert((oper == options::Values{"not-a-value"}));
|
2019-08-08 17:54:29 +00:00
|
|
|
assert((opts == options::Dictionary{
|
|
|
|
{"v", {"value", "other"}},
|
|
|
|
{"w", {}}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Short Unix options shorthand. */
|
|
|
|
{
|
2020-08-18 20:07:51 +00:00
|
|
|
std::array l{"-abcdef", "value", "not-a-value"};
|
|
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
2019-08-08 17:54:29 +00:00
|
|
|
|
2020-08-18 20:07:51 +00:00
|
|
|
assert((oper == options::Values{"not-a-value"}));
|
2019-08-08 17:54:29 +00:00
|
|
|
assert((opts == options::Dictionary{
|
|
|
|
{"a", {}},
|
|
|
|
{"b", {}},
|
|
|
|
{"c", {}},
|
|
|
|
{"d", {}},
|
|
|
|
{"e", {}},
|
|
|
|
{"f", {"value"}}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Long GNU options. */
|
|
|
|
{
|
2020-08-18 20:07:51 +00:00
|
|
|
std::array l{"--long", "--option"};
|
|
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
2019-08-08 17:54:29 +00:00
|
|
|
|
2020-08-18 20:07:51 +00:00
|
|
|
assert((oper == options::Values{}));
|
2019-08-08 17:54:29 +00:00
|
|
|
assert((opts == options::Dictionary{
|
|
|
|
{"long", {}},
|
|
|
|
{"option", {}}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-08-18 20:07:51 +00:00
|
|
|
/* Long GNU options with arguments separated by whitespace. */
|
2019-08-08 17:54:29 +00:00
|
|
|
{
|
2020-08-18 20:07:51 +00:00
|
|
|
std::array l{"--value", "v", "--value", "-v", "value", "--value"};
|
|
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
2019-08-08 17:54:29 +00:00
|
|
|
|
2020-08-18 20:07:51 +00:00
|
|
|
assert((oper == options::Values{}));
|
2019-08-08 17:54:29 +00:00
|
|
|
assert((opts == options::Dictionary{
|
|
|
|
{"v", {"value"}},
|
|
|
|
{"value", {"v"}}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-08-18 20:07:51 +00:00
|
|
|
/* 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"}}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2019-08-08 17:54:29 +00:00
|
|
|
/* Single dash as option-argument. */
|
|
|
|
{
|
2020-08-18 20:07:51 +00:00
|
|
|
std::array l{"--output", "-", "--input", "-", "-"};
|
|
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
2019-08-08 17:54:29 +00:00
|
|
|
|
2020-08-18 20:07:51 +00:00
|
|
|
assert((oper == options::Values{"-"}));
|
2019-08-08 17:54:29 +00:00
|
|
|
assert((opts == options::Dictionary{
|
|
|
|
{"output", {"-"}},
|
|
|
|
{"input", {"-"}}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Long, short and shorthand options mixed. */
|
|
|
|
{
|
2020-08-18 20:07:51 +00:00
|
|
|
std::array l{"-abc", "content", "--long", "-short", "tree", "out"};
|
|
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
2019-08-08 17:54:29 +00:00
|
|
|
|
2020-08-18 20:07:51 +00:00
|
|
|
assert((oper == options::Values{"out"}));
|
2019-08-08 17:54:29 +00:00
|
|
|
assert((opts == options::Dictionary{
|
|
|
|
{"a", {}},
|
|
|
|
{"b", {}},
|
|
|
|
{"c", {"content"}},
|
|
|
|
{"long", {}},
|
|
|
|
{"s", {}},
|
|
|
|
{"h", {}},
|
|
|
|
{"o", {}},
|
|
|
|
{"r", {}},
|
|
|
|
{"t", {"tree"}}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Operand separator. */
|
|
|
|
{
|
2020-08-18 20:07:51 +00:00
|
|
|
std::array l{"--option", "--", "--not-option"};
|
|
|
|
auto [opts, oper] = options::parse(std::cbegin(l), std::cend(l));
|
2019-08-08 17:54:29 +00:00
|
|
|
|
2020-08-18 20:07:51 +00:00
|
|
|
assert((oper == options::Values{"--not-option"}));
|
2019-08-08 17:54:29 +00:00
|
|
|
assert((opts == options::Dictionary{
|
|
|
|
{"option", {}}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|