title: c++ boost::program_options: convert program_options/variables_map back to command line
body: <p>I need to pass some specific arguments from one programm to another using boost::program_options and boost::process. Here is a simple example. In this example I need to pass all args stored in vm_slave to child process, but in common case I wanna pass one or more specific args from vm_slave.</p>
  <pre><code>#include &lt;iostream&gt;
  
  #include &lt;boost/process.hpp&gt;
  #include &lt;boost/program_options.hpp&gt;
  
  using namespace std;
  namespace po = boost::program_options;
  
  
  int main(int argc, char* argv[]) {
      po::options_description tester_options(&quot;Tester options&quot;);
      po::options_description slave_options(&quot;Slave options&quot;);
  
      tester_options.add_options()
          (&quot;help,h&quot;, &quot;Show help&quot;)
          (&quot;iter,i&quot;, po::value&lt;short&gt;()-&gt;default_value(1), &quot;TODO&quot;)
          (&quot;modules,m&quot;, po::value&lt;std::vector&lt;string&gt;&gt;()-&gt;multitoken(), &quot;TODO&quot;);
  
      slave_options.add_options()
          (&quot;size,s&quot;, po::value&lt;size_t&gt;()-&gt;required(), &quot;TODO&quot;)
          (&quot;threads,t&quot;, po::value&lt;short&gt;()-&gt;default_value(1), &quot;TODO&quot;);
  
      po::variables_map vm;
      po::variables_map vm_slave;
  
      auto p0 = po::command_line_parser(argc, argv).options(tester_options).allow_unregistered().run();
      auto p1 = po::command_line_parser(argc, argv).options(slave_options).allow_unregistered().run();
  
      po::store(p0, vm);
      po::store(p1, vm);
      po::store(p1, vm_slave);
  
      // Do some stuff such as write help if needed
      // ...
  
      // I need call child process with all (or specific) args from vm_slave
      boost::process::ipstream pipe;
      boost::process::child cp(
          &quot;slave&quot; /* + vm_slave args */,
          boost::process::std_err &gt; pipe,
          boost::process::std_out &gt; pipe
      );
  
      cp.wait();
  
      return 0;
  }
  </code></pre>
  <p>Of course I can do something like this:</p>
  <pre><code>ostringstream args;
  for (const auto&amp; arg : p1.options) {
      if (vm_slave.count(arg.string_key) == 0)
          continue;
  
      args &lt;&lt; arg.string_key &lt;&lt; &quot; &quot;;
      for (const auto&amp; val : arg.value)
          args &lt;&lt; val &lt;&lt; &quot; &quot;;
  }
  string cmd_args = args.str();
  </code></pre>
  <p>But in this case some args stored in vm_slave by default is lost.</p>
  <p>Or I can do this:</p>
  <pre><code>ostringstream args;
  for (const auto&amp; arg : vm_slave) {
      args &lt;&lt; arg.first &lt;&lt; &quot; &quot;;
  
      const auto&amp; any_val = arg.second.value();
      if (boost::any_cast&lt;size_t&gt;(any_val))
          args &lt;&lt; to_string(boost::any_cast&lt;size_t&gt;(any_val));
      else if (boost::any_cast&lt;short&gt;(any_val))
          args &lt;&lt; to_string(boost::any_cast&lt;size_t&gt;(any_val));
      // And more and more casts...
  
      args &lt;&lt; &quot; &quot;;
  }
  </code></pre>
  <p>But now we have many any_cast...</p>
  <p>If I just pass <code>argv</code> to child process, the child might fail because extra args presented (args like <code>iter</code> not intended for this application).</p>
  <p>All this attempts seem bad to me.</p>
  <p><strong>What is the proper way to convert parsed arguments back to command line?</strong></p>