From 96e5343d02146aaaded015084a3050d8b522b7cf Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 18 Feb 2025 00:46:30 -0500 Subject: [PATCH] more work on argparse --- CMakeLists.txt | 2 +- include/blt/parse/argparse_v2.h | 24 ++++++++++++++++++++---- src/blt/parse/argparse_v2.cpp | 21 +++++++++++---------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 81e6256..dab0d1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 4.0.12) +set(BLT_VERSION 4.0.13) set(BLT_TARGET BLT) diff --git a/include/blt/parse/argparse_v2.h b/include/blt/parse/argparse_v2.h index bb35c98..50d84b1 100644 --- a/include/blt/parse/argparse_v2.h +++ b/include/blt/parse/argparse_v2.h @@ -376,10 +376,26 @@ namespace blt::argparse }; m_dest_vec_func = [](const std::string_view dest, argument_storage_t& storage, const std::vector& values) { - std::vector converted_values; - for (const auto& value : values) - converted_values.push_back(detail::arg_string_converter_t::convert(value)); - storage.m_data.insert({dest, converted_values}); + if (storage.m_data.contains(dest)) + { + auto& data = storage.m_data[dest]; + if (!std::holds_alternative>(data)) + { + std::cerr << "Invalid type conversion. Trying to add type " << blt::type_string() << + " but this does not match existing type index '" << data.index() << "'!" << std::endl; + std::exit(1); + } + std::vector& converted_values = std::get>(data); + for (const auto& value : values) + converted_values.push_back(detail::arg_string_converter_t::convert(value)); + } + else + { + std::vector converted_values; + for (const auto& value : values) + converted_values.push_back(detail::arg_string_converter_t::convert(value)); + storage.m_data.insert({dest, converted_values}); + } }; return *this; } diff --git a/src/blt/parse/argparse_v2.cpp b/src/blt/parse/argparse_v2.cpp index bcaf666..79310dd 100644 --- a/src/blt/parse/argparse_v2.cpp +++ b/src/blt/parse/argparse_v2.cpp @@ -313,24 +313,23 @@ namespace blt::argparse } if (argc == 0) { + } else if (argc == 1) { switch (flag->m_action) { case action_t::STORE: - flag->m_dest_func(dest, parsed_args, args.front()); + flag->m_dest_func(dest, parsed_args, args.front()); break; case action_t::APPEND: case action_t::EXTEND: - { - - break; - } + flag->m_dest_vec_func(dest, parsed_args, args); + break; case action_t::APPEND_CONST: if (flag->m_const_value) { - std::cerr << "Append const chosen as an action but const value not provided for flag '" << arg << '\'' << + std::cerr << "Append const chosen as an action but const value not provided for argument '" << arg << '\'' << std::endl; std::exit(1); } @@ -340,7 +339,7 @@ namespace blt::argparse auto visitor = detail::arg_meta_type_helper_t::make_visitor( [arg](auto& primitive) { - std::cerr << "Invalid type - '" << arg << "' expected list type, found '" + std::cerr << "Invalid type for argument '" << arg << "' expected list type, found '" << blt::type_string() << "' with value " << primitive << std::endl; std::exit(1); }, @@ -349,7 +348,7 @@ namespace blt::argparse using type = typename meta::remove_cvref_t::value_type; if (!std::holds_alternative(*flag->m_const_value)) { - std::cerr << "Constant value for flag '" << arg << + std::cerr << "Constant value for argument '" << arg << "' type doesn't match values already present! Expected to be of type '" << blt::type_string() << "'!" << std::endl; std::exit(1); @@ -397,7 +396,8 @@ namespace blt::argparse if constexpr (std::is_convertible_v) { return primitive + static_cast(args.size()); - } else + } + else { std::cerr << "Error: count called but stored type is " << blt::type_string() << std::endl; std::exit(1); @@ -405,7 +405,8 @@ namespace blt::argparse }, [](auto&) -> detail::arg_data_t { - std::cerr << "List present on count. This condition doesn't make any sense! (How did we get here, please report this!)"; + std::cerr << + "List present on count. This condition doesn't make any sense! (How did we get here, please report this!)"; std::exit(1); } );