Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

action that returns nothing #39

Closed
zhihaoy opened this issue Nov 12, 2019 · 0 comments · Fixed by #46
Closed

action that returns nothing #39

zhihaoy opened this issue Nov 12, 2019 · 0 comments · Fixed by #46

Comments

@zhihaoy
Copy link
Contributor

zhihaoy commented Nov 12, 2019

Currently this code isn't allowed in argparse:

program.add_argument(...)
    .action([](std::string const& filename)
    {
        load_cfg(filename);
    });

Because argparse internal expects action to return some value, but this one returns void.
My workaround for that is to define using unit = std::tuple<>; and returns unit(), but this isn't nice to C++'s taste.
But it's possible to bake this workaround into argparse (even though there is no any<void>). Let's say we define a type similar to the unit above, call is none_internal. In action, detects the the input's return type:

  template<class F>
  Argument &action(F&& f) {
    if constexpr (std::is_void_v<std::invoke_result_t<F, std::string>>)
      mAction = [f](std::string const& arg) { f(arg); return none_internal(); };
    else
      mAction = std::move(aAction);
    return *this;
  }
zhihaoy added a commit to zhihaoy/argparse that referenced this issue Nov 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant