Skip to content

Instantly share code, notes, and snippets.

@gesquive
Last active June 12, 2022 11:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save gesquive/8673796 to your computer and use it in GitHub Desktop.
Save gesquive/8673796 to your computer and use it in GitHub Desktop.
Methods to print out boost::program_options objects
#include <string.h>
#include <iostream>
#include "boost/program_options.hpp"
#include "boost/filesystem.hpp"
#include "boost/any.hpp"
namespace po = boost::program_options;
inline void PrintUsage(const boost::program_options::options_description desc) {
std::cout << "Usage: " << app_name << " [options]" << std::endl;
std::cout << " App description" << std::endl;
std::cout << desc << std::endl;
std::cout << std::endl << "v" << VERSION << std::endl;
}
inline void PrintVariableMap(const boost::program_options::variables_map vm) {
for (po::variables_map::iterator it = vm.begin(); it != vm.end(); it++) {
std::cout << "> " << it->first;
if (((boost::any)it->second.value()).empty()) {
std::cout << "(empty)";
}
if (vm[it->first].defaulted() || it->second.defaulted()) {
std::cout << "(default)";
}
std::cout << "=";
bool is_char;
try {
boost::any_cast<const char *>(it->second.value());
is_char = true;
} catch (const boost::bad_any_cast &) {
is_char = false;
}
bool is_str;
try {
boost::any_cast<std::string>(it->second.value());
is_str = true;
} catch (const boost::bad_any_cast &) {
is_str = false;
}
if (((boost::any)it->second.value()).type() == typeid(int)) {
std::cout << vm[it->first].as<int>() << std::endl;
} else if (((boost::any)it->second.value()).type() == typeid(bool)) {
std::cout << vm[it->first].as<bool>() << std::endl;
} else if (((boost::any)it->second.value()).type() == typeid(double)) {
std::cout << vm[it->first].as<double>() << std::endl;
} else if (is_char) {
std::cout << vm[it->first].as<const char * >() << std::endl;
} else if (is_str) {
std::string temp = vm[it->first].as<std::string>();
if (temp.size()) {
std::cout << temp << std::endl;
} else {
std::cout << "true" << std::endl;
}
} else { // Assumes that the only remainder is vector<string>
try {
std::vector<std::string> vect = vm[it->first].as<std::vector<std::string> >();
uint i = 0;
for (std::vector<std::string>::iterator oit=vect.begin();
oit != vect.end(); oit++, ++i) {
std::cout << "\r> " << it->first << "[" << i << "]=" << (*oit) << std::endl;
}
} catch (const boost::bad_any_cast &) {
std::cout << "UnknownType(" << ((boost::any)it->second.value()).type().name() << ")" << std::endl;
}
}
}
}
@cgrandits
Copy link

I just tested this recently. I found the need to make one change (declaring a const_iterator) on line 18:

for (po::variables_map::const_iterator it = vm.begin(); it != vm.end(); it++) {

@munsingh
Copy link

munsingh commented Mar 7, 2022

This will not work if you have custom validators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment