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)
include(cmake/color.cmake)
set(BLT_VERSION 4.0.12)
set(BLT_VERSION 4.0.13)
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)
{
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});
if (storage.m_data.contains(dest))
{
auto& data = storage.m_data[dest];
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;
}

View File

@ -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<decltype(primitive)>() << "' with value " << primitive << std::endl;
std::exit(1);
},
@ -349,7 +348,7 @@ namespace blt::argparse
using type = typename meta::remove_cvref_t<decltype(vec)>::value_type;
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 '" <<
blt::type_string<type>() << "'!" << std::endl;
std::exit(1);
@ -397,7 +396,8 @@ namespace blt::argparse
if constexpr (std::is_convertible_v<decltype(args.size()), type>)
{
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::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);
}
);