po::options_description desc("This are the options that are available");
desc.add_options()("help", "print help")(
"deer", po::value<uint32_t>(), "set how many deer you want")(
"rating", po::value<uint32_t>(), "how good ?")(
"name", po::value<std::string>(), "and your name is ... ?");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
in a following section of the code I tried to iterate over vm
for (const auto& it : vm) {
std::cout << it.first.c_str() << " "
<< it.second.as<it.pair::second_type>() << "\n";
}
The main point here is that vm
contains keys
of the same type, but values with different types, in this example I have uint32_t
mixed with a std::string
.
How I can iterate over this kind of containers ? I would like to avoid a verbose approach so I'm trying to just iterate over this data structure.
EDIT:
I forgot to write this down, but obviously
namespace po = boost::program_options;
boost variable_map
use boost::any
as the value so you can try to use boost::any_cast<T>
to find out the type.
perhaps something like this
for (const auto& it : vm) {
std::cout << it.first.c_str() << " ";
auto& value = it.second.value();
if (auto v = boost::any_cast<uint32_t>(&value))
std::cout << *v;
else if (auto v = boost::any_cast<std::string>(&value))
std::cout << *v;
else
std::cout << "error";
}