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

std::optional makes the underlying type to be badly deduced #447

Closed
sobczyk opened this issue Mar 16, 2020 · 5 comments
Closed

std::optional makes the underlying type to be badly deduced #447

sobczyk opened this issue Mar 16, 2020 · 5 comments

Comments

@sobczyk
Copy link

sobczyk commented Mar 16, 2020

For code:

#include <CLI/CLI.hpp>
#include <optional>

int main(int argc, char** argv)
{
        std::optional<uint32_t> int32;
        CLI::App app{"test"};
        app.add_option("--test", int32, "should be unsinged int");
        CLI11_PARSE(app, argc, argv);
        return 0;
}

compiled with
g++ -std=c++17 test.cpp
the output is:

./a.out --help
test
Usage: ./a.out [OPTIONS]

Options:
  -h,--help                   Print this help message and exit
  --test FLOAT                should be unsinged int

It also has problems with parsing optional class enum with mapping for the enum (it does not find how to parse it)

For testing I've used g++ 7.5 and clang 8 with libcxx 8
Library 1.9.0

I think #285 had a negative impact on option parsing.

@phlptp
Copy link
Collaborator

phlptp commented Mar 16, 2020

@sobczyk
Copy link
Author

sobczyk commented Mar 16, 2020

./a.out --help
test
Usage: ./a.out [OPTIONS]

Options:
  -h,--help                   Print this help message and exit
  --test UINT                 should be unsinged int

for the case I've provided this branch works.
I've additionally tested it with the enum version, that does not compile on master:

#include <CLI/CLI.hpp>
#include <optional>
#include <map>
#include <iostream>

enum class EC: uint32_t
{
        ONE = 1,
        TWO = 2,
};

static const std::map<const std::string,EC> ecm {
        {"one", EC::ONE},
        {"two", EC::TWO},
};

int main(int argc, char** argv)
{
                std::optional<uint32_t> int32;
                std::optional<EC> ec_opt;
                CLI::App app{"test"};
                app.add_option("--test", int32, "should be unsinged int");
                app.add_option("--enum", ec_opt, "should just work")->transform(CLI::CheckedTransformer(ecm));
                CLI11_PARSE(app, argc, argv);
                if (ec_opt)
                        std::cout << uint32_t(*ec_opt) << std::endl;
                else
                        std::cout << "ec_opt not set" << std::endl;
                return 0;
}

and it compiles, and works as expected

./a.out --help
test
Usage: ./a.out [OPTIONS]

Options:
  -h,--help                   Print this help message and exit
  --test UINT                 should be unsinged int
  --enum ENUM:value in {one->1,two->2} OR {1,2}
                              should just work

./a.out --enum one
1

./a.out
ec_opt not set

@phlptp
Copy link
Collaborator

phlptp commented Mar 16, 2020

Good to know it works for you.

I suspect it could work with master using the two argument template for add_option

app.add_option<std::optional<uint32_t>,uint32_t>("--test", int32, "should be unsinged int");
 app.add_option<std::optional<EC>,EC>("--enum", ec_opt, "should just work");

which definitely isn't as convenient but should work. Also what drove #285.

@sobczyk
Copy link
Author

sobczyk commented Mar 17, 2020

Will this branch be merged before next release?
if yes, when do you plan next release?

This feature is really nice

@henryiii
Copy link
Collaborator

henryiii commented Mar 17, 2020

I'm going to be reviewing and merging things somewhat soon. Everything is likely to go in. I'm not currently sure if I'm going to try to make a 1.10 release before 2.0 - I've got two key changes planned for 2.0 that are going to take some time (optional compile ahead and no-exception-catching support). Behavior changing changes will be 2.0 only as well. I think this is one of them.

@phlptp phlptp closed this as completed Mar 23, 2020
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