Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One approach is to tokenize <code>std::string</code> into a <code>std::vector&lt;std::string&gt;</code>, then pass the result to Boost.ProgramOption's <a href="http://www.boost.org/doc/libs/1_54_0/doc/html/boost/program_options/basic_command_line_parser.html" rel="nofollow noreferrer"><code>command_line_parser</code></a>. The Boost.ProgramOption's <a href="http://www.boost.org/doc/libs/1_54_0/doc/html/program_options/howto.html#idp123410640" rel="nofollow noreferrer">documentation</a> briefly covers this approach. Additionally, I use a similar approach in part of <a href="https://stackoverflow.com/a/12513321/1053968">this</a> answer.</p> <p>Here is a minimal complete example:</p> <pre><code>#include &lt;algorithm&gt; #include &lt;iostream&gt; #include &lt;iterator&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;boost/bind.hpp&gt; #include &lt;boost/program_options.hpp&gt; #include &lt;boost/tokenizer.hpp&gt; // copy_if was left out of the C++03 standard, so mimic the C++11 // behavior to support all predicate types. The alternative is to // use remove_copy_if, but it only works for adaptable functors. template &lt;typename InputIterator, typename OutputIterator, typename Predicate&gt; OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred) { while(first != last) { if(pred(*first)) *result++ = *first; ++first; } return result; } /// @brief Tokenize a string. The tokens will be separated by each non-quoted /// space or equal character. Empty tokens are removed. /// /// @param input The string to tokenize. /// /// @return Vector of tokens. std::vector&lt;std::string&gt; tokenize(const std::string&amp; input) { typedef boost::escaped_list_separator&lt;char&gt; separator_type; separator_type separator("\\", // The escape characters. "= ", // The separator characters. "\"\'"); // The quote characters. // Tokenize the intput. boost::tokenizer&lt;separator_type&gt; tokens(input, separator); // Copy non-empty tokens from the tokenizer into the result. std::vector&lt;std::string&gt; result; copy_if(tokens.begin(), tokens.end(), std::back_inserter(result), !boost::bind(&amp;std::string::empty, _1)); return result; } int main() { // Variables that will store parsed values. std::string address; unsigned int port; // Setup options. namespace po = boost::program_options; po::options_description desc("Options"); desc.add_options() ("address", po::value&lt;std::string&gt;(&amp;address)) ("port", po::value&lt;unsigned int&gt;(&amp;port)) ; // Mock up input. std::string input = "--address 127.0.0.1 --port 12345"; // Parse mocked up input. po::variables_map vm; po::store(po::command_line_parser(tokenize(input)) .options(desc).run(), vm); po::notify(vm); // Output. std::cout &lt;&lt; "address = " &lt;&lt; address &lt;&lt; "\n" "port = " &lt;&lt; port &lt;&lt; std::endl; } </code></pre> <p>Which produces the following output:</p> <pre class="lang-none prettyprint-override"><code>address = 127.0.0.1 port = 12345 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload