make argparse work on c++ 17

v1
Brett 2023-11-02 16:02:40 -04:00
parent 1d8f9b4bbd
commit 15bcd37834
3 changed files with 47 additions and 7 deletions

View File

@ -22,10 +22,12 @@
#if __cplusplus >= 202002L
#define BLT_CONTAINS(container, value) container.contains(value)
#define BLT_CPP20_CONSTEXPR constexpr
#define BLT_USE_CPP20
#else
#include <algorithm>
#define BLT_CONTAINS(container, value) std::find(container.begin(), container.end(), value) != container.end()
#define BLT_CPP20_CONSTEXPR
#undef BLT_USE_CPP20
#endif
#define INCLUDE_FS \

View File

@ -13,6 +13,7 @@
#include <initializer_list>
#include <optional>
#include <blt/std/hashmap.h>
#include <blt/std/string.h>
#include <variant>
#include <algorithm>
#include <type_traits>
@ -118,7 +119,6 @@ namespace blt
struct arg_properties_t
{
private:
public:
arg_vector_t a_flags;
arg_action_t a_action = arg_action_t::STORE;
@ -130,6 +130,11 @@ namespace blt
std::string a_version{};
std::string a_metavar{};
bool a_required = true;
arg_properties_t() = delete;
explicit arg_properties_t(arg_vector_t flags): a_flags(std::move(flags))
{}
};
class arg_builder
@ -237,13 +242,13 @@ namespace blt
// returns true if the current arg is a flag
inline bool isFlag()
{
return args[currentIndex].starts_with('-');
return blt::string::starts_with(args[currentIndex], '-');
}
// returns true if we have next and the next arg is a flag
inline bool isNextFlag()
{
return hasNext() && args[currentIndex + 1].starts_with('-');
return hasNext() && blt::string::starts_with(args[currentIndex + 1], '-');
}
// advances to the next flag
@ -306,6 +311,7 @@ namespace blt
return static_cast<T>(std::stoll(s));
return static_cast<T>(std::stoull(s));
}
private:
struct
{
@ -359,9 +365,9 @@ namespace blt
inline bool contains(const std::string& key)
{
if (key.starts_with("--"))
if (blt::string::starts_with(key, "--"))
return data.find(key.substr(2)) != data.end();
if (key.starts_with('-'))
if (blt::string::starts_with(key, '-'))
return data.find(key.substr(1)) != data.end();
return data.find(key) != data.end();
}

View File

@ -63,6 +63,9 @@ namespace blt::string
static inline BLT_CPP20_CONSTEXPR bool starts_with(const std::string& string, const std::string& search)
{
#ifdef BLT_USE_CPP20
return string.starts_with(search);
#else
if (search.length() > string.length())
return false;
auto chars = string.c_str();
@ -73,10 +76,25 @@ namespace blt::string
return false;
}
return true;
#endif
}
static inline BLT_CPP20_CONSTEXPR bool starts_with(const std::string& string, char search)
{
#ifdef BLT_USE_CPP20
return string.starts_with(search);
#else
if (string.empty())
return false;
return string[0] == search;
#endif
}
static inline BLT_CPP20_CONSTEXPR bool ends_with(const std::string& string, const std::string& search)
{
#ifdef BLT_USE_CPP20
return string.ends_with(search);
#else
if (search.length() > string.length())
return false;
auto chars = string.c_str();
@ -88,6 +106,18 @@ namespace blt::string
return false;
}
return true;
#endif
}
static inline BLT_CPP20_CONSTEXPR bool ends_with(const std::string& string, char search)
{
#ifdef BLT_USE_CPP20
return string.ends_with(search);
#else
if (string.empty())
return false;
return string[string.size() - 1] == search;
#endif
}
static inline BLT_CPP20_CONSTEXPR bool contains(const std::string& string, const char search)
@ -97,7 +127,8 @@ namespace blt::string
return c == search;
});
#else
for (const char c : string){
for (const char c : string)
{
if (c == search)
return true;
}
@ -278,7 +309,8 @@ namespace blt::string
return std::isdigit(c);
});
#else
for (const char c : s){
for (const char c : s)
{
if (!std::isdigit(c))
return false;
}