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