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

Use enum names in config_to_str() output? #791

Open
DanielSeemaier opened this issue Oct 23, 2022 · 1 comment
Open

Use enum names in config_to_str() output? #791

DanielSeemaier opened this issue Oct 23, 2022 · 1 comment

Comments

@DanielSeemaier
Copy link

This is essentially the enum_ostream example with a config_to_str() call added:

#include <iostream>
#include <map>
#include <string>

#include "CLI11.hpp"

enum class Level : int { High, Medium, Low };

std::ostream &operator<<(std::ostream &out, const Level &level) {
    switch (level) {
        case Level::High: return out << "high";
        case Level::Medium: return out << "medium";
        case Level::Low: return out << "low";
    }
    return out << "invalid";
}

int main(int argc, char **argv) {
    CLI::App app;
    Level level = Level::Low;

    std::map<std::string, Level> map{
        {"high", Level::High}, {"medium", Level::Medium}, {"low", Level::Low}};

    app.add_option("-l,--level", level, "Level settings")
        ->required()
        ->transform(CLI::CheckedTransformer(map, CLI::ignore_case));

    CLI11_PARSE(app, argc, argv);

    using CLI::enums::operator<<;

    std::cout << app.config_to_str(true, true);
    std::cout << "Enum received: " << level << std::endl;

    return 0;
}

When calling ./a.out -l high, this outputs

$ ./a.out -l high
#

# Level settings
level=0
Enum received: high

Is it intended that CLI11 uses "0" rather than "high" in the configuration file output? If so, is it possible to change it to "high"?

Thanks!

@phlptp
Copy link
Collaborator

phlptp commented Oct 24, 2022

The way it works is it will store whatever string is saved in the option after the processing. In this case I think that is whatever the output of the transformer is. I don't think the operator<< is ever used in this context since internally it never stores a Level so would have no way of converting that to a string.

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

2 participants