diff options
author | Taylor C. Richberger <taywee@gmx.com> | 2016-05-11 22:44:34 -0400 |
---|---|---|
committer | Taylor C. Richberger <taywee@gmx.com> | 2016-05-11 22:44:34 -0400 |
commit | 27f92acf2aa914efaf691f26518f61319537ab1c (patch) | |
tree | 5ef1c18abcfddabea96f4a3fab6a8c593d123514 /README.md | |
parent | bump version (diff) | |
parent | finish mapping types (diff) | |
download | args.hxx-27f92acf2aa914efaf691f26518f61319537ab1c.tar.xz |
Merge branch '17-add-mapping-types' into 'master'
4.1.0
17 add mapping types
Closes #17
See merge request !11
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 52 |
1 files changed, 51 insertions, 1 deletions
@@ -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: @@ -492,7 +496,7 @@ Argument 'numbers' received invalid value type 'a' ```cpp #include <iostream> #include <tuple> -#include <args.hxx> + std::istream& operator>>(std::istream& is, std::tuple<int, int>& ints) { is >> std::get<0>(ints); @@ -501,6 +505,8 @@ std::istream& operator>>(std::istream& is, std::tuple<int, int>& ints) return is; } +#include <args.hxx> + void DoublesReader(const std::string &name, const std::string &value, std::tuple<double, double> &destination) { size_t commapos = 0; @@ -865,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<std::string, MappingEnum> 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<std::string, MappingEnum> dmf(parser, "DMF", "Maps string to an enum", args::Matcher{"dmf"}, map); + args::MapFlag<std::string, MappingEnum> mf(parser, "MF", "Maps string to an enum", args::Matcher{"mf"}, map); + args::MapFlag<std::string, MappingEnum, ToLowerReader> cimf(parser, "CIMF", "Maps string to an enum case-insensitively", args::Matcher{"cimf"}, map); + args::MapFlagList<std::string, MappingEnum> mfl(parser, "MFL", "Maps string to an enum list", args::Matcher{"mfl"}, map); + args::MapPositional<std::string, MappingEnum> mp(parser, "MP", "Maps string to an enum", map); + args::MapPositionalList<std::string, MappingEnum> mpl(parser, "MPL", "Maps string to an enum list", map); + parser.ParseArgs(std::vector<std::string>{"--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>{MappingEnum::bar, MappingEnum::green, MappingEnum::bar})); + REQUIRE(mp); + REQUIRE((args::get(mp) == MappingEnum::foo)); + REQUIRE(mpl); + REQUIRE((args::get(mpl) == std::vector<MappingEnum>{MappingEnum::red, MappingEnum::def})); + REQUIRE_THROWS_AS(parser.ParseArgs(std::vector<std::string>{"--mf=YeLLoW"}), args::MapError); +} +``` |