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

Can`t parse "--help" argument #4

Open
marvin-Yu opened this issue May 12, 2020 · 4 comments
Open

Can`t parse "--help" argument #4

marvin-Yu opened this issue May 12, 2020 · 4 comments

Comments

@marvin-Yu
Copy link

When I was trying to test the first use case under the Quickstart, all the argument parser works great except the "--help".

$ ./TestArg.exe --name=marvin
marvin

$ ./TestArg.exe --help
Segmentation fault

Could help to check this?

@Dmry
Copy link
Contributor

Dmry commented May 13, 2020

Everything works without fault on my Linux machine, so this might be windows specific.

@marvin-Yu
Copy link
Author

I debugged the case to find the exact cause. Finally I found that this lambda expression can`t capture the variables correctly in windows.

 [&, this](auto&& attribute) -> decltype(attribute(x, *this, arg), void()) { attribute(x, *this, arg); }
template<class T, class... Ts>
void parse(T&& x, Ts&&... xs)
{
    argument arg;
    arg.write_value = [&x](const std::string& s) { args::write_value_to(x, s); };
    arg.type = args::get_argument_type(x);
    arg.metavar = args::type_to_help(x);
    args::each_arg(args::overload(
        [&](const std::string& name) { arg.flags.push_back(name); },
        [&, this](auto&& attribute) -> decltype(attribute(x, *this, arg), void()) { attribute(x, *this, arg); }
    ), std::forward<Ts>(xs)...);
    this->add(std::move(arg));
}

By the way, I can`t compile the "args.hpp" file directly at vs2019.
vs2019 compile output show as below.

1>------ Build started: Project: TestArg, Configuration: Debug x64 ------
1>TestArg.cpp
1>C:\Users\weifeiyu\source\repos\TestArg\args.hpp(344,1): error C3861: '__this': identifier not found
...

@pfultz2
Copy link
Owner

pfultz2 commented May 13, 2020

I think this is a problem with handling expression SFINAE in MSVC. I've never tested this on windows. If there is some workarounds to make this work on MSVC, please let me know.

@marvin-Yu
Copy link
Author

I'm newbie here of c++, so the root cause I can't figure out. In my test, this issue just happen in the "--help" parser. Cause of the project limit, I have to do some ugly modification, but work.
In order to avoid compile error, I use local variable to instead of this pointer.

template<class T, class... Ts>                                                                              |template<class T, class... Ts>
void parse(T&& x, Ts&&... xs)                                                                               |void parse(T&& x, Ts&&... xs)
{                                                                                                           |{
    context* cxt = this;                                                                                    |-------------------------------------------------------------------------------------------------------------------------------------
    argument arg;                                                                                           |    argument arg;
    arg.write_value = [&x](const std::string& s) { args::write_value_to(x, s); };                           |    arg.write_value = [&x](const std::string& s) {args::write_value_to(x, s); };
    arg.type = args::get_argument_type(x);                                                                  |    arg.type = args::get_argument_type(x);
    arg.metavar = args::type_to_help(x);                                                                    |    arg.metavar = args::type_to_help(x);
    args::each_arg(args::overload(                                                                          |    args::each_arg(args::overload(
        [&](const std::string& name) { arg.flags.push_back(name); },                                        |        [&, this](const std::string& name) { arg.flags.push_back(name); },
        [&, cxt](auto&& attribute) -> decltype(attribute(x, *cxt, arg), void()) { attribute(x, *cxt, arg); }|        [&, this](auto&& attribute) -> decltype(attribute(x, *this, arg), void()) { attribute(x, *this, arg); }
    ), std::forward<Ts>(xs)...);                                                                            |    ), std::forward<Ts>(xs)...);
    this->add(std::move(arg));                                                                              |    this->add(std::move(arg));
}                                                                                                           |}
          

Then, Using the 'ctx' variable captured by copy to support show "--help" information.

template<class... Ts, class T>                                                    |  template<class... Ts, class T>
context<T&, Ts...> build_context(T& cmd)                                          |  context<T&, Ts...> build_context(T& cmd)
{                                                                                 |  {
    context<T&, Ts...> ctx;                                                       |      context<T&, Ts...> ctx;
    args::assign_subcommands(rank<1>{}, ctx, cmd);                                |      args::assign_subcommands(rank<1>{}, ctx, cmd);
                                                                                  |      ctx.parse(nullptr, "--help", "-h", args::help("Show help."),
----------------------------------------------------------------------------------|          args::eager_callback([](std::nullptr_t, const auto& c, const argument&)
----------------------------------------------------------------------------------|      {
----------------------------------------------------------------------------------|          c.show_help(get_name<T>(), get_help<T>(), get_options_metavar<T>());
----------------------------------------------------------------------------------|      }));
    args::try_parse(rank<1>{}, cmd, [&](auto&&... xs)                             |      args::try_parse(rank<1>{}, cmd, [&](auto&&... xs)
    {                                                                             |      {
        ctx.parse(std::forward<decltype(xs)>(xs)...);                             |          ctx.parse(std::forward<decltype(xs)>(xs)...);
    });                                                                           |      });
    ctx.parse(nullptr, "--help", "-h", args::help("Show help."),                  |  -------------------------------------------------------------------------------
        args::eager_callback([ctx](std::nullptr_t, const auto& c, const argument&)|  -------------------------------------------------------------------------------
    {                                                                             |  -------------------------------------------------------------------------------
        ctx.show_help(get_name<T>(), get_help<T>(), get_options_metavar<T>());    |  -------------------------------------------------------------------------------
    }));                                                                          |  -------------------------------------------------------------------------------
    return ctx;                                                                   |      return ctx;
}                                                                                 |  }
  

Hope someone can solve this problem really.

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

No branches or pull requests

3 participants