args icon indicating copy to clipboard operation
args copied to clipboard

Can`t parse "--help" argument

Open marvin-Yu opened this issue 4 years ago • 4 comments

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?

marvin-Yu avatar May 12 '20 03:05 marvin-Yu

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

Dmry avatar May 13 '20 00:05 Dmry

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
...

marvin-Yu avatar May 13 '20 01:05 marvin-Yu

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.

pfultz2 avatar May 13 '20 03:05 pfultz2

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.

marvin-Yu avatar May 14 '20 05:05 marvin-Yu