How to reassemble command line string using Boost Program Options?

Viewed 50
  • I parsed command line using Boost Program Options as usual. po::command_line_parser populated po::variables_map vm.

  • I changed some values in po::variables_map like this:

    vm.at(option).value() = val;

  • Now I need to reassemble modified command line string from po::variables_map. How to do this?

1 Answers

Program Options po:variables_map vm has type: std::map<std::string, variable_value>

variable_value stores value in boost::any v; Usage of boost::any influences the solution:

string ProgramOptions::getCommandLine()
{
    using boost::any_cast;
    ostringstream cmdLine;
    for (auto i : _vm)
    {
        string dashForm = i.first.length() == 1 ? "-" : "--";
        cmdLine << dashForm << i.first << " ";
        boost::any value(i.second.value());
        if (auto *const v = any_cast<int>(&value))
            cmdLine << *v;
        else if(auto* const v = any_cast<string>(&value))
            cmdLine << *v;
        cmdLine << " ";
    }
    return cmdLine.str();
}
Related