argparse bug fix

v1
Brett 2024-03-31 01:42:38 -04:00
parent 31bc3f3bf7
commit 822d926651
2 changed files with 31 additions and 9 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.5)
include(cmake/color.cmake) include(cmake/color.cmake)
set(BLT_VERSION 0.15.7) set(BLT_VERSION 0.15.8)
set(BLT_TEST_VERSION 0.0.1) set(BLT_TEST_VERSION 0.0.1)
set(BLT_TARGET BLT) set(BLT_TARGET BLT)

View File

@ -17,10 +17,11 @@
#include <variant> #include <variant>
#include <algorithm> #include <algorithm>
#include <type_traits> #include <type_traits>
#include "blt/std/utility.h"
namespace blt namespace blt
{ {
typedef std::variant<std::string, bool, int32_t, float, double, int64_t> arg_data_internal_t; typedef std::variant<std::string, bool, int32_t> arg_data_internal_t;
typedef std::vector<arg_data_internal_t> arg_data_vec_t; typedef std::vector<arg_data_internal_t> arg_data_vec_t;
typedef std::variant<arg_data_internal_t, arg_data_vec_t> arg_data_t; typedef std::variant<arg_data_internal_t, arg_data_vec_t> arg_data_t;
@ -307,8 +308,13 @@ namespace blt
template<typename T> template<typename T>
static inline T get_cast(arg_data_t& v) static inline T get_cast(arg_data_t& v)
{ {
if constexpr (std::is_same_v<T, arg_data_vec_t>) if (std::holds_alternative<arg_data_vec_t>(v))
return std::get<arg_data_vec_t>(v); {
if constexpr (std::is_same_v<T, arg_data_vec_t>)
return std::get<arg_data_vec_t>(v);
else
throw std::runtime_error("Cannot request singular data from stored vector type");
}
auto t = std::get<arg_data_internal_t>(v); auto t = std::get<arg_data_internal_t>(v);
// user is requesting an int, but holds a string, we are going to make the assumption the data can be converted // 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! // it is up to the user to deal with the variant if they do not want this behaviour!
@ -319,12 +325,20 @@ namespace blt
return static_cast<T>(std::get<int32_t>(t)); return static_cast<T>(std::get<int32_t>(t));
if (std::holds_alternative<bool>(t)) if (std::holds_alternative<bool>(t))
return static_cast<T>(std::get<bool>(t)); return static_cast<T>(std::get<bool>(t));
auto s = std::get<std::string>(t); auto s = std::get<std::string>(t);
if (s.empty())
throw std::runtime_error("Key does not have value!");
if constexpr (std::is_floating_point_v<T>) if constexpr (std::is_floating_point_v<T>)
return static_cast<T>(std::stod(s)); return static_cast<T>(std::stod(s));
if constexpr (std::is_signed_v<T>) else if constexpr (std::is_signed_v<T>)
return static_cast<T>(std::stoll(s)); return static_cast<T>(std::stoll(s));
return static_cast<T>(std::stoull(s)); else if constexpr (std::is_unsigned_v<T>)
return static_cast<T>(std::stoull(s));
else
return static_cast<T>(s);
} }
struct arg_results struct arg_results
@ -346,10 +360,17 @@ namespace blt
template<typename T> template<typename T>
inline T get(const std::string& key) inline T get(const std::string& key)
{ {
if constexpr (std::is_same_v<T, std::string>) hashmap_t<std::string, arg_data_t>::iterator val;
return blt::arg_parse::get<T>(data[key]); if (blt::string::starts_with(key, "--"))
val = data.find(key.substr(2));
else if (blt::string::starts_with(key, '-'))
val = data.find(key.substr(1));
else else
return blt::arg_parse::get_cast<T>(data[key]); val = data.find(key);
if constexpr (std::is_same_v<T, std::string>)
return blt::arg_parse::get<T>(val->second);
else
return blt::arg_parse::get_cast<T>(val->second);
} }
inline auto begin() inline auto begin()
@ -371,6 +392,7 @@ namespace blt
return data.find(key) != data.end(); return data.find(key) != data.end();
} }
}; };
private: private:
struct struct
{ {