Skip to content

Commit

Permalink
[argparse] update library
Browse files Browse the repository at this point in the history
Update to PR p-ranav/argparse#356

Also fixes the incompatibility between store_into and scan and action: when the three methods above were called, being all based on the (unique) action, the last one would overwrite the previous ones.

This issue was making the parser strictly dependant on the order of the scan/store_into/action calls making them mutually exclusive.
  • Loading branch information
elpaso committed Jun 3, 2024
1 parent 6fc7fe1 commit 58d7081
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
3 changes: 2 additions & 1 deletion apps/argparse/README.TXT
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Provenance: https://github.com/p-ranav/argparse

Retrieved from https://github.com/p-ranav/argparse/blob/b85a0a414d415fccdf6d77072421b1d3887c8f79/include/argparse/argparse.hpp
Retrieved: https://github.com/p-ranav/argparse/blob/8dead89026466b3818e9c6b6b1d938600db39d8f/include/argparse/argparse.hpp

46 changes: 21 additions & 25 deletions apps/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,9 @@ class Argument {
std::is_void_v<std::invoke_result_t<F, Args..., std::string const>>,
void_action, valued_action>;
if constexpr (sizeof...(Args) == 0) {
m_action.emplace<action_type>(std::forward<F>(callable));
m_actions.emplace_back<action_type>(std::forward<F>(callable));
} else {
m_action.emplace<action_type>(
m_actions.emplace_back<action_type>(
[f = std::forward<F>(callable),
tup = std::make_tuple(std::forward<Args>(bound_args)...)](
std::string const &opt) mutable {
Expand All @@ -702,14 +702,7 @@ class Argument {
template <typename T, typename std::enable_if<std::is_integral<T>::value>::type * = nullptr>
auto &store_into(T &var) {
if (m_default_value.has_value()) {
try
{
var = std::any_cast<T>(m_default_value);
}
catch (...)
{
var = static_cast<T>(std::any_cast<int>(m_default_value));
}
var = std::any_cast<T>(m_default_value);

This comment has been minimized.

Copy link
@abellgithub

abellgithub Jun 6, 2024

Contributor

This change broke existing code. It seems like things were overwritten instead of merged. I made a PR for the change that was stomped to hopefully avoid in the future.

This comment has been minimized.

Copy link
@rouault

rouault Jun 6, 2024

Member

restored in 399a0f5

}
action([&var](const auto &s) {
var = details::parse_number<T, details::radix_10>()(s);
Expand All @@ -719,14 +712,7 @@ class Argument {

auto &store_into(double &var) {
if (m_default_value.has_value()) {
try
{
var = std::any_cast<double>(m_default_value);
}
catch (...)
{
var = std::any_cast<int>(m_default_value);
}
var = std::any_cast<double>(m_default_value);
}
action([&var](const auto &s) {
var = details::parse_number<double, details::chars_format::general>()(s);
Expand Down Expand Up @@ -994,7 +980,12 @@ class Argument {
if (num_args_max == 0) {
if (!dry_run) {
m_values.emplace_back(m_implicit_value);
std::visit([](const auto &f) { f({}); }, m_action);
for(auto &action: m_actions) {
std::visit([&](const auto &f) { f({}); }, action);
}
if(m_actions.empty()){
std::visit([&](const auto &f) { f({}); }, m_default_action);
}
m_is_used = true;
}
return start;
Expand All @@ -1011,8 +1002,7 @@ class Argument {
std::bind(is_optional, std::placeholders::_1, m_prefix_chars));
dist = static_cast<std::size_t>(std::distance(start, end));
if (dist < num_args_min) {
throw std::runtime_error("Too few arguments for '" +
std::string(m_used_name) + "'.");
throw std::runtime_error("Too few arguments");
}
}

Expand All @@ -1035,7 +1025,12 @@ class Argument {
Argument &self;
};
if (!dry_run) {
std::visit(ActionApply{start, end, *this}, m_action);
for(auto &action: m_actions) {
std::visit(ActionApply{start, end, *this}, action);
}
if(m_actions.empty()){
std::visit(ActionApply{start, end, *this}, m_default_action);
}
m_is_used = true;
}
return end;
Expand Down Expand Up @@ -1585,9 +1580,10 @@ class Argument {
std::optional<std::vector<std::string>> m_choices{std::nullopt};
using valued_action = std::function<std::any(const std::string &)>;
using void_action = std::function<void(const std::string &)>;
std::variant<valued_action, void_action> m_action{
std::in_place_type<valued_action>,
[](const std::string &value) { return value; }};
std::vector<std::variant<valued_action, void_action>> m_actions;
std::variant<valued_action, void_action> m_default_action{
std::in_place_type<valued_action>,
[](const std::string &value) { return value; }};
std::vector<std::any> m_values;
NArgsRange m_num_args_range{1, 1};
// Bit field of bool values. Set default value in ctor.
Expand Down

0 comments on commit 58d7081

Please sign in to comment.