From b8870fac3ab825d7d3b42483321f6ba32b67ec8f Mon Sep 17 00:00:00 2001 From: "Taylor C. Richberger" Date: Wed, 11 May 2016 20:40:18 -0600 Subject: finish mapping types --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'README.md') diff --git a/README.md b/README.md index 18ac5e4..f4435d0 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,10 @@ The code can be downloaded at https://github.com/Taywee/args There are also somewhat extensive examples below. +You can find the complete test cases at +https://github.com/Taywee/args/blob/master/test.cxx, which should very well +describe the usage, as it's built to push the boundaries. + # What does it do? It: @@ -867,3 +871,47 @@ int main(int argc, char **argv) This goes after the options. % ``` + +# Mapping positionals + +I haven't written out a long example for this, but here's the test case you should be able to discern the meaning from: + +```cpp +void ToLowerReader(const std::string &name, const std::string &value, std::string &destination) +{ + destination = value; + std::transform(destination.begin(), destination.end(), destination.begin(), ::tolower); +} + +TEST_CASE("Mapping types work as needed", "[args]") +{ + std::unordered_map map{ + {"default", MappingEnum::def}, + {"foo", MappingEnum::foo}, + {"bar", MappingEnum::bar}, + {"red", MappingEnum::red}, + {"yellow", MappingEnum::yellow}, + {"green", MappingEnum::green}}; + args::ArgumentParser parser("This is a test program.", "This goes after the options."); + args::MapFlag dmf(parser, "DMF", "Maps string to an enum", args::Matcher{"dmf"}, map); + args::MapFlag mf(parser, "MF", "Maps string to an enum", args::Matcher{"mf"}, map); + args::MapFlag cimf(parser, "CIMF", "Maps string to an enum case-insensitively", args::Matcher{"cimf"}, map); + args::MapFlagList mfl(parser, "MFL", "Maps string to an enum list", args::Matcher{"mfl"}, map); + args::MapPositional mp(parser, "MP", "Maps string to an enum", map); + args::MapPositionalList mpl(parser, "MPL", "Maps string to an enum list", map); + parser.ParseArgs(std::vector{"--mf=red", "--cimf=YeLLoW", "--mfl=bar", "foo", "--mfl=green", "red", "--mfl", "bar", "default"}); + REQUIRE_FALSE(dmf); + REQUIRE(args::get(dmf) == MappingEnum::def); + REQUIRE(mf); + REQUIRE(args::get(mf) == MappingEnum::red); + REQUIRE(cimf); + REQUIRE(args::get(cimf) == MappingEnum::yellow); + REQUIRE(mfl); + REQUIRE((args::get(mfl) == std::vector{MappingEnum::bar, MappingEnum::green, MappingEnum::bar})); + REQUIRE(mp); + REQUIRE((args::get(mp) == MappingEnum::foo)); + REQUIRE(mpl); + REQUIRE((args::get(mpl) == std::vector{MappingEnum::red, MappingEnum::def})); + REQUIRE_THROWS_AS(parser.ParseArgs(std::vector{"--mf=YeLLoW"}), args::MapError); +} +``` -- cgit v1.2.1