From 82821c00957dc7fcc53cbe3f0ee8ef003ec10312 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Thu, 8 Feb 2024 08:49:19 -0500 Subject: [PATCH] add to_string values to argparse (make it cleaner --- include/blt/parse/argparse.h | 5 +++- src/blt/parse/argparse.cpp | 57 +++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/include/blt/parse/argparse.h b/include/blt/parse/argparse.h index 54d67bc..ddc6d4e 100755 --- a/include/blt/parse/argparse.h +++ b/include/blt/parse/argparse.h @@ -57,7 +57,8 @@ namespace blt arg_vector_t(std::initializer_list 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 diff --git a/src/blt/parse/argparse.cpp b/src/blt/parse/argparse.cpp index 247468d..92daea9 100755 --- a/src/blt/parse/argparse.cpp +++ b/src/blt/parse/argparse.cpp @@ -7,6 +7,7 @@ #include #include #include +#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(v)) - return to_string(std::get(v)); - else if (std::holds_alternative(v)) - { - const auto& vec = std::get(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 "Vector of contents: " + str; - } - return "Empty"; + 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); + } + }, v); } std::string to_string(const arg_data_internal_t& v) { - if (std::holds_alternative(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(v); - } else if (std::holds_alternative(v)) - { - return std::get(v) ? "True" : "False"; + result += to_string(value.second); + if (value.first != vec.size() - 1) + result += ", "; } - return std::to_string(std::get(v)); + result += "]"; + + return result; } std::string arg_parse::filename(const std::string& path)