i made some changes

v2
Brett 2025-02-18 01:32:26 -05:00
parent 96e5343d02
commit e0d36269bf
3 changed files with 129 additions and 96 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.13) set(BLT_VERSION 4.0.14)
set(BLT_TARGET BLT) set(BLT_TARGET BLT)

View File

@ -150,7 +150,7 @@ namespace blt::argparse
} }
}; };
using arg_meta_type_helper_t = arg_data_helper_t<i8, i16, i32, i64, u8, u16, u32, u64, float, double, std::string_view>; using arg_meta_type_helper_t = arg_data_helper_t<bool, i8, i16, i32, i64, u8, u16, u32, u64, float, double, std::string_view>;
using arg_data_t = arg_meta_type_helper_t::variant_t; using arg_data_t = arg_meta_type_helper_t::variant_t;
template <typename T> template <typename T>

View File

@ -311,121 +311,154 @@ namespace blt::argparse
<< std::endl; << std::endl;
std::exit(1); std::exit(1);
} }
if (argc == 0)
{
} switch (flag->m_action)
else if (argc == 1)
{ {
switch (flag->m_action) case action_t::STORE:
if (argc == 0)
{ {
case action_t::STORE: std::cerr << "Error: argument '" << arg <<
"' action is store but takes in no arguments. This condition is invalid!" << std::endl;
std::exit(1);
}
if (argc == 1)
flag->m_dest_func(dest, parsed_args, args.front()); flag->m_dest_func(dest, parsed_args, args.front());
break; else
case action_t::APPEND: {
case action_t::EXTEND: std::cerr << "Error: argument '" << arg << "' action is store but takes in more than one argument. " <<
flag->m_dest_vec_func(dest, parsed_args, args); "This condition is invalid, did you mean to use action_t::APPEND or action_t::EXTEND?" << std::endl;
break; std::exit(1);
case action_t::APPEND_CONST: }
if (flag->m_const_value) break;
{ case action_t::APPEND:
std::cerr << "Append const chosen as an action but const value not provided for argument '" << arg << '\'' << case action_t::EXTEND:
std::endl; if (argc == 0)
std::exit(1); {
} std::cerr << "Error: argument '" << arg <<
if (parsed_args.contains(dest)) "' action is append or extend but takes in no arguments. This condition is invalid!" << std::endl;
{ std::exit(1);
auto& data = parsed_args.m_data[dest]; }
auto visitor = detail::arg_meta_type_helper_t::make_visitor( flag->m_dest_vec_func(dest, parsed_args, args);
[arg](auto& primitive) break;
case action_t::APPEND_CONST:
if (argc != 0)
{
std::cerr << "Error: argument '" << arg << "' action is append const but takes in arguments. "
"This condition is invalid!" << std::endl;
std::exit(1);
}
if (flag->m_const_value)
{
std::cerr << "Append const chosen as an action but const value not provided for argument '" << arg << '\'' <<
std::endl;
std::exit(1);
}
if (parsed_args.contains(dest))
{
auto& data = parsed_args.m_data[dest];
auto visitor = detail::arg_meta_type_helper_t::make_visitor(
[arg](auto& primitive)
{
std::cerr << "Invalid type for argument '" << arg << "' expected list type, found '"
<< blt::type_string<decltype(primitive)>() << "' with value " << primitive << std::endl;
std::exit(1);
},
[&flag, arg](auto& vec)
{
using type = typename meta::remove_cvref_t<decltype(vec)>::value_type;
if (!std::holds_alternative<type>(*flag->m_const_value))
{ {
std::cerr << "Invalid type for argument '" << arg << "' expected list type, found '" std::cerr << "Constant value for argument '" << arg <<
<< blt::type_string<decltype(primitive)>() << "' with value " << primitive << std::endl; "' type doesn't match values already present! Expected to be of type '" <<
blt::type_string<type>() << "'!" << std::endl;
std::exit(1); std::exit(1);
}, }
[&flag, arg](auto& vec) vec.push_back(std::get<type>(*flag->m_const_value));
{ });
using type = typename meta::remove_cvref_t<decltype(vec)>::value_type; std::visit(visitor, data);
if (!std::holds_alternative<type>(*flag->m_const_value)) }
{ else
std::cerr << "Constant value for argument '" << arg << {
"' type doesn't match values already present! Expected to be of type '" << auto visitor = detail::arg_meta_type_helper_t::make_visitor(
blt::type_string<type>() << "'!" << std::endl; [&flag, &parsed_args, &dest](auto& primitive)
std::exit(1); {
} std::vector<meta::remove_cvref_t<decltype(primitive)>> vec;
vec.push_back(std::get<type>(*flag->m_const_value)); vec.push_back(primitive);
}); parsed_args.m_data.insert({dest, std::move(vec)});
std::visit(visitor, data); },
} [](auto&)
else {
{ std::cerr << "Append const should not be a list type!" << std::endl;
auto visitor = detail::arg_meta_type_helper_t::make_visitor( std::exit(1);
[&flag, &parsed_args, &dest](auto& primitive) });
{ std::visit(visitor, *flag->m_const_value);
std::vector<meta::remove_cvref_t<decltype(primitive)>> vec; }
vec.push_back(primitive); break;
parsed_args.m_data.insert({dest, std::move(vec)}); case action_t::STORE_CONST:
}, if (argc != 0)
[](auto&) {
{
std::cerr << "Append const should not be a list type!" << std::endl;
std::exit(1);
});
std::visit(visitor, *flag->m_const_value);
}
break;
case action_t::STORE_CONST:
std::cerr << "Store const flag called with an argument. This condition doesn't make sense." << std::endl; std::cerr << "Store const flag called with an argument. This condition doesn't make sense." << std::endl;
print_usage(); print_usage();
std::exit(1); std::exit(1);
case action_t::STORE_TRUE: }
if (!flag->m_const_value)
{
std::cerr << "Store const flag called with no value. This condition doesn't make sense." << std::endl;
std::exit(1);
}
parsed_args.m_data.insert({dest, *flag->m_const_value});
case action_t::STORE_TRUE:
if (argc != 0)
{
std::cerr << "Store true flag called with an argument. This condition doesn't make sense." << std::endl; std::cerr << "Store true flag called with an argument. This condition doesn't make sense." << std::endl;
print_usage(); print_usage();
std::exit(1); std::exit(1);
case action_t::STORE_FALSE: }
parsed_args.m_data.insert({dest, true});
case action_t::STORE_FALSE:
if (argc != 0)
{
std::cerr << "Store false flag called with an argument. This condition doesn't make sense." << std::endl; std::cerr << "Store false flag called with an argument. This condition doesn't make sense." << std::endl;
print_usage(); print_usage();
std::exit(1); std::exit(1);
case action_t::COUNT: }
if (parsed_args.m_data.contains(dest)) parsed_args.m_data.insert({dest, false});
{ case action_t::COUNT:
auto visitor = detail::arg_meta_type_helper_t::make_visitor( if (parsed_args.m_data.contains(dest))
[&args](auto& primitive) -> detail::arg_data_t {
auto visitor = detail::arg_meta_type_helper_t::make_visitor(
[&args](auto& primitive) -> detail::arg_data_t
{
using type = meta::remove_cvref_t<decltype(primitive)>;
if constexpr (std::is_convertible_v<decltype(args.size()), type>)
{ {
using type = meta::remove_cvref_t<decltype(primitive)>; return primitive + static_cast<type>(args.size());
if constexpr (std::is_convertible_v<decltype(args.size()), type>) }
{ else
return primitive + static_cast<type>(args.size());
}
else
{
std::cerr << "Error: count called but stored type is " << blt::type_string<type>() << std::endl;
std::exit(1);
}
},
[](auto&) -> detail::arg_data_t
{ {
std::cerr << std::cerr << "Error: count called but stored type is " << blt::type_string<type>() << std::endl;
"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);
} }
); },
parsed_args.m_data[dest] = std::visit(visitor, parsed_args.m_data[dest]); [](auto&) -> detail::arg_data_t
} {
else std::cerr <<
parsed_args.m_data.insert({dest, args.size()}); "List present on count. This condition doesn't make any sense! (How did we get here, please report this!)";
break; std::exit(1);
case action_t::HELP: }
print_help(); );
std::exit(1); parsed_args.m_data[dest] = std::visit(visitor, parsed_args.m_data[dest]);
case action_t::VERSION:
print_version();
break;
} }
flag->m_dest_func(dest, parsed_args, args.front()); else
parsed_args.m_data.insert({dest, args.size()});
break;
case action_t::HELP:
print_help();
std::exit(1);
case action_t::VERSION:
print_version();
break;
} }
else
flag->m_dest_vec_func(dest, parsed_args, args);
} }
}, flag->m_nargs); }, flag->m_nargs);
} }