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;
- }