add to_string values to argparse (make it cleaner

v1
Brett 2024-02-08 08:49:19 -05:00
parent a54c45a975
commit 82821c0095
2 changed files with 37 additions and 25 deletions

View File

@ -57,7 +57,8 @@ namespace blt
arg_vector_t(std::initializer_list<std::string> f): flags(f) arg_vector_t(std::initializer_list<std::string> f): flags(f)
{ {
if (flags.size() == 1) { if (flags.size() == 1)
{
if (!blt::string::starts_with(flags[0], '-')) if (!blt::string::starts_with(flags[0], '-'))
{ {
name = flags[0]; name = flags[0];
@ -478,6 +479,8 @@ namespace blt
std::string to_string(const blt::arg_data_internal_t& v); std::string to_string(const blt::arg_data_internal_t& v);
std::string to_string(const blt::arg_data_vec_t& v);
} }
#endif //BLT_TESTS_ARGPARSE_H #endif //BLT_TESTS_ARGPARSE_H

View File

@ -7,6 +7,7 @@
#include <iostream> #include <iostream>
#include <blt/std/string.h> #include <blt/std/string.h>
#include <algorithm> #include <algorithm>
#include "blt/std/utility.h"
namespace blt namespace blt
{ {
@ -92,36 +93,44 @@ namespace blt
std::string to_string(const arg_data_t& v) std::string to_string(const arg_data_t& v)
{ {
if (std::holds_alternative<arg_data_internal_t>(v)) return std::visit(blt::lambda_visitor{
return to_string(std::get<arg_data_internal_t>(v)); [](const arg_data_internal_t& v) {
else if (std::holds_alternative<arg_data_vec_t>(v)) return to_string(v);
{ },
const auto& vec = std::get<arg_data_vec_t>(v); [](const arg_data_vec_t& v) {
if (vec.size() == 1) return to_string(v);
return to_string(vec[0]); }
if (vec.empty()) }, v);
return "Empty Vector";
std::string str;
for (const auto& r : vec)
{
str += to_string(r);
str += ' ';
}
return "Vector of contents: " + str;
}
return "Empty";
} }
std::string to_string(const arg_data_internal_t& v) std::string to_string(const arg_data_internal_t& v)
{ {
if (std::holds_alternative<std::string>(v)) return std::visit(blt::lambda_visitor{
[&](const std::string& str) {
return str;
},
[&](bool b) {
return std::string(b ? "True" : "False");
},
[&](int32_t i) {
return std::to_string(i);
}
}, v);
}
std::string to_string(const blt::arg_data_vec_t& vec)
{
std::string result = "[";
for (const auto& value : blt::enumerate(vec))
{ {
return std::get<std::string>(v); result += to_string(value.second);
} else if (std::holds_alternative<bool>(v)) if (value.first != vec.size() - 1)
{ result += ", ";
return std::get<bool>(v) ? "True" : "False";
} }
return std::to_string(std::get<int32_t>(v)); result += "]";
return result;
} }
std::string arg_parse::filename(const std::string& path) std::string arg_parse::filename(const std::string& path)