aboutsummaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorTaylor C. Richberger <Taywee@gmx.com>2016-05-11 20:40:18 -0600
committerTaylor C. Richberger <Taywee@gmx.com>2016-05-11 20:40:18 -0600
commitb8870fac3ab825d7d3b42483321f6ba32b67ec8f (patch)
tree5ef1c18abcfddabea96f4a3fab6a8c593d123514 /README.md
parentadd mapping flag, throw maperror, ensure throwing works properly (diff)
downloadargs.hxx-b8870fac3ab825d7d3b42483321f6ba32b67ec8f.tar.xz
finish mapping types
Diffstat (limited to 'README.md')
-rw-r--r--README.md48
1 files changed, 48 insertions, 0 deletions
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<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);
+}
+```