aboutsummaryrefslogtreecommitdiff
path: root/args.hxx
diff options
context:
space:
mode:
authorTaylor C. Richberger <Taywee@gmx.com>2016-05-11 18:10:53 -0600
committerTaylor C. Richberger <Taywee@gmx.com>2016-05-11 18:10:53 -0600
commit1b9b9823d4619ef62783e25768b9460dcde55a74 (patch)
tree37c60846dfdc469cc591b82390aac8728bed9ccf /args.hxx
parentbump version (diff)
downloadargs.hxx-1b9b9823d4619ef62783e25768b9460dcde55a74.tar.xz
add mapping flag, throw maperror, ensure throwing works properly
Diffstat (limited to 'args.hxx')
-rw-r--r--args.hxx54
1 files changed, 54 insertions, 0 deletions
diff --git a/args.hxx b/args.hxx
index 9198afc..aade975 100644
--- a/args.hxx
+++ b/args.hxx
@@ -32,6 +32,7 @@
#include <string>
#include <tuple>
#include <vector>
+#include <unordered_map>
#include <unordered_set>
#include <type_traits>
@@ -174,6 +175,13 @@ namespace args
virtual ~UsageError() {};
};
+ class MapError : public Error
+ {
+ public:
+ MapError(const std::string &problem) : Error(problem) {}
+ virtual ~MapError() {};
+ };
+
/** An exception that indicates that the user has requested help
*/
class Help : public Error
@@ -1419,6 +1427,52 @@ namespace args
}
};
+ /** A mapping value flag class
+ *
+ * \tparam K the type to extract the argument as
+ * \tparam T the type to store the result map as
+ * \tparam Reader The function used to read the argument, taking the name, value, and destination reference
+ */
+ template <typename K, typename T, void (*Reader)(const std::string &, const std::string &, K&) = ValueReader<K>, typename Map = std::unordered_map<K, T>>
+ class MapFlag : public ValueFlagBase
+ {
+ private:
+ const Map map;
+ T value;
+
+ public:
+
+ MapFlag(Group &group, const std::string &name, const std::string &help, Matcher &&matcher, const Map &map, const T &defaultValue = T()): ValueFlagBase(name, help, std::move(matcher)), map(map), value(defaultValue)
+ {
+ group.Add(*this);
+ }
+
+ virtual ~MapFlag() {}
+
+ virtual void ParseValue(const std::string &value) override
+ {
+ K key;
+ Reader(name, value, key);
+ auto it = map.find(key);
+ if (it == std::end(map))
+ {
+ std::ostringstream problem;
+ problem << "Could not find key '" << key << "' in map for arg '" << name << "'";
+ throw MapError(problem.str());
+ } else
+ {
+ this->value = it->second;
+ }
+ }
+
+ /** Get the value
+ */
+ T &Get() noexcept
+ {
+ return value;
+ }
+ };
+
/** An argument-accepting flag class that pushes the found values into a list
*
* \tparam T the type to extract the argument as