more work on argparse

v2
Brett 2025-02-18 00:46:30 -05:00
parent 7dc19efbaa
commit 96e5343d02
3 changed files with 32 additions and 15 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20) cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake) include(cmake/color.cmake)
set(BLT_VERSION 4.0.12) set(BLT_VERSION 4.0.13)
set(BLT_TARGET BLT) set(BLT_TARGET BLT)

View File

@ -376,10 +376,26 @@ namespace blt::argparse
}; };
m_dest_vec_func = [](const std::string_view dest, argument_storage_t& storage, const std::vector<std::string_view>& values) m_dest_vec_func = [](const std::string_view dest, argument_storage_t& storage, const std::vector<std::string_view>& values)
{ {
std::vector<T> converted_values; if (storage.m_data.contains(dest))
for (const auto& value : values) {
converted_values.push_back(detail::arg_string_converter_t<T>::convert(value)); auto& data = storage.m_data[dest];
storage.m_data.insert({dest, converted_values}); if (!std::holds_alternative<std::vector<T>>(data))
{
std::cerr << "Invalid type conversion. Trying to add type " << blt::type_string<T>() <<
" but this does not match existing type index '" << data.index() << "'!" << std::endl;
std::exit(1);
}
std::vector<T>& converted_values = std::get<std::vector<T>>(data);
for (const auto& value : values)
converted_values.push_back(detail::arg_string_converter_t<T>::convert(value));
}
else
{
std::vector<T> converted_values;
for (const auto& value : values)
converted_values.push_back(detail::arg_string_converter_t<T>::convert(value));
storage.m_data.insert({dest, converted_values});
}
}; };
return *this; return *this;
} }

View File

@ -313,24 +313,23 @@ namespace blt::argparse
} }
if (argc == 0) if (argc == 0)
{ {
} }
else if (argc == 1) else if (argc == 1)
{ {
switch (flag->m_action) switch (flag->m_action)
{ {
case action_t::STORE: case action_t::STORE:
flag->m_dest_func(dest, parsed_args, args.front()); flag->m_dest_func(dest, parsed_args, args.front());
break; break;
case action_t::APPEND: case action_t::APPEND:
case action_t::EXTEND: case action_t::EXTEND:
{ flag->m_dest_vec_func(dest, parsed_args, args);
break;
break;
}
case action_t::APPEND_CONST: case action_t::APPEND_CONST:
if (flag->m_const_value) 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::endl;
std::exit(1); std::exit(1);
} }
@ -340,7 +339,7 @@ namespace blt::argparse
auto visitor = detail::arg_meta_type_helper_t::make_visitor( auto visitor = detail::arg_meta_type_helper_t::make_visitor(
[arg](auto& primitive) [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<decltype(primitive)>() << "' with value " << primitive << std::endl; << blt::type_string<decltype(primitive)>() << "' with value " << primitive << std::endl;
std::exit(1); std::exit(1);
}, },
@ -349,7 +348,7 @@ namespace blt::argparse
using type = typename meta::remove_cvref_t<decltype(vec)>::value_type; using type = typename meta::remove_cvref_t<decltype(vec)>::value_type;
if (!std::holds_alternative<type>(*flag->m_const_value)) if (!std::holds_alternative<type>(*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 '" << "' type doesn't match values already present! Expected to be of type '" <<
blt::type_string<type>() << "'!" << std::endl; blt::type_string<type>() << "'!" << std::endl;
std::exit(1); std::exit(1);
@ -397,7 +396,8 @@ namespace blt::argparse
if constexpr (std::is_convertible_v<decltype(args.size()), type>) if constexpr (std::is_convertible_v<decltype(args.size()), type>)
{ {
return primitive + static_cast<type>(args.size()); return primitive + static_cast<type>(args.size());
} else }
else
{ {
std::cerr << "Error: count called but stored type is " << blt::type_string<type>() << std::endl; std::cerr << "Error: count called but stored type is " << blt::type_string<type>() << std::endl;
std::exit(1); std::exit(1);
@ -405,7 +405,8 @@ namespace blt::argparse
}, },
[](auto&) -> detail::arg_data_t [](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); std::exit(1);
} }
); );