Evgenii Legotckoi
Evgenii LegotckoiDec. 4, 2018, 5:47 p.m.

Boost - Use enum in parameters boost::program_options

A small addition to the article on the use of boost::program_options .

In this appendix I will describe how to use enum enums. Because immediately they do not work. By default, boost::program_options work only with basic types and some types from the standard library, for example std::string .


Suppose we have a class that contains an enum .

class Logger
{
public:
    explicit Logger();

    enum VerboseLevel
    {
        E_DEBUG = 0
        E_INFO,
        E_WARNING,
        E_CRITICAL,
    };
};

It is logical that we would like to use this enumeration in the command line menu to set the level of detail of the output in the console or in a file for example.

That is, we want to configure our boost::program_options like this.

m_desc.add_options()
        (MENU_ITEM_HELP.c_str(), "produce help message")
    ("level",
     po::value<Logger::VerboseLevel>(&m_verboseLevel)->default_value(Logger::E_WARNING), 
     "set verbose level of logging, default level is Info\nLevels: \n0) Debug \n1) Info \n2) Warning \n3) Critical")
        ;
po::store(po::parse_command_line(argc, argv, m_desc), m_vm);  
po::notify(m_vm);

But when building we get an error

Error C2338 Target type is neither std::istream`able nor std::wistream`able

In this case, we need to do an overload on operator>> in order to be able to set the values we need.

The simplest option might look like this.

std::istream& operator>>(std::istream& in, Logger::VerboseLevel& verboseLevel)
{
    std::string token;
    in >> token;
    if (token == "0")
        verboseLevel = Logger::VerboseLevel::E_DEBUG;
    else if (token == "1")
        verboseLevel = Logger::VerboseLevel::E_INFO;
    else if (token == "2")
        verboseLevel = Logger::VerboseLevel::E_WARNING;
    else if (token == "3")
        verboseLevel = Logger::VerboseLevel::E_CRITICAL;
    else
        in.setstate(std::ios_base::failbit);
    return in;
}
We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
AD

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:50points,
  • Rating points-4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:80points,
  • Rating points4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:20points,
  • Rating points-10
Last comments
i
innorwallNov. 14, 2024, 7:03 a.m.
How to make game using Qt - Lesson 3. Interaction with other objects what is priligy tablets What happens during the LASIK surgery process
i
innorwallNov. 14, 2024, 4:09 a.m.
Using variables declared in CMakeLists.txt inside C ++ files where can i buy priligy online safely Tom Platz How about things like we read about in the magazines like roid rage and does that really
i
innorwallNov. 12, 2024, 6:12 a.m.
Django - Tutorial 055. How to write auto populate field functionality Freckles because of several brand names retin a, atralin buy generic priligy
i
innorwallNov. 12, 2024, 2:23 a.m.
QML - Tutorial 035. Using enumerations in QML without C ++ priligy cvs 24 Together with antibiotics such as amphotericin B 10, griseofulvin 11 and streptomycin 12, chloramphenicol 9 is in the World Health Organisation s List of Essential Medici…
i
innorwallNov. 11, 2024, 11:50 p.m.
Qt/C++ - Lesson 052. Customization Qt Audio player in the style of AIMP It decreases stress, supports hormone balance, and regulates and increases blood flow to the reproductive organs buy priligy online safe Promising data were reported in a PDX model re…
Now discuss on the forum
i
innorwallNov. 14, 2024, 8:39 a.m.
добавить qlineseries в функции Listen intently to what Jerry says about Conditional Acceptance because that s the bargaining chip in the song and dance you will have to engage in to protect yourself and your family from AMI S…
i
innorwallNov. 11, 2024, 6:55 p.m.
Всё ещё разбираюсь с кешем. priligy walgreens levitra dulcolax carbs The third ring was found to be made up of ultra relativistic electrons, which are also present in both the outer and inner rings
9
9AnonimOct. 25, 2024, 4:10 p.m.
Машина тьюринга // Начальное состояние 0 0, ,<,1 // Переход в состояние 1 при пустом символе 0,0,>,0 // Остаемся в состоянии 0, двигаясь вправо при встрече 0 0,1,>…

Follow us in social networks