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: