From 0e17dff8628a9bce45cf3e8bd8d58f3f4c82736c Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Fri, 18 Aug 2023 19:15:21 -0400 Subject: [PATCH] update variant access in arg_parse::get --- include/blt/parse/argparse.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/include/blt/parse/argparse.h b/include/blt/parse/argparse.h index 0f85c42..aad19fc 100755 --- a/include/blt/parse/argparse.h +++ b/include/blt/parse/argparse.h @@ -15,6 +15,7 @@ #include #include #include +#include namespace blt { @@ -329,13 +330,28 @@ namespace blt return std::holds_alternative(v) && std::holds_alternative(std::get(v)); } + /** + * Attempt to cast the variant stored in the arg results to the requested type + * if user is requesting an int, but holds a string, we are going to make the assumption the data can be converted + * it is up to the user to deal with the variant if they do not want this behaviour! + * @tparam T type to convert to + * @param v + * @return + */ template static inline T& get(arg_data_t& v) { if constexpr (std::is_same_v) return std::get(v); else - return std::get(std::get(v)); + { + auto t = std::get(v); + // user is requesting an int, but holds a string, we are going to make the assumption the data can be converted + // it is up to the user to deal with the variant if they do not want this behaviour! + if constexpr (std::holds_alternative(t) && std::is_same_v) + return std::stoi(std::get(t)); + return std::get(t); + } } public: