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)
{
if (flags.size() == 1) {
if (flags.size() == 1)
{
if (!blt::string::starts_with(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_vec_t& v);
}
#endif //BLT_TESTS_ARGPARSE_H

View File

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