Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is complete program as per rcollyer and Tim, whom the credits go to:</p> <pre><code>#include &lt;boost/program_options.hpp&gt; #include &lt;iostream&gt; #include &lt;sstream&gt; namespace po = boost::program_options; bool process_command_line(int argc, char** argv, std::string&amp; host, std::string&amp; port, std::string&amp; configDir) { int iport; try { po::options_description desc("Program Usage", 1024, 512); desc.add_options() ("help", "produce help message") ("host,h", po::value&lt;std::string&gt;(&amp;host)-&gt;required(), "set the host server") ("port,p", po::value&lt;int&gt;(&amp;iport)-&gt;required(), "set the server port") ("config,c", po::value&lt;std::string&gt;(&amp;configDir)-&gt;required(), "set the config path") ; po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); if (vm.count("help")) { std::cout &lt;&lt; desc &lt;&lt; "\n"; return false; } // There must be an easy way to handle the relationship between the // option "help" and "host"-"port"-"config" // Yes, the magic is putting the po::notify after "help" option check po::notify(vm); } catch(std::exception&amp; e) { std::cerr &lt;&lt; "Error: " &lt;&lt; e.what() &lt;&lt; "\n"; return false; } catch(...) { std::cerr &lt;&lt; "Unknown error!" &lt;&lt; "\n"; return false; } std::stringstream ss; ss &lt;&lt; iport; port = ss.str(); return true; } int main(int argc, char** argv) { std::string host; std::string port; std::string configDir; bool result = process_command_line(argc, argv, host, port, configDir); if (!result) return 1; // else std::cout &lt;&lt; "host:\t" &lt;&lt; host &lt;&lt; "\n"; std::cout &lt;&lt; "port:\t" &lt;&lt; port &lt;&lt; "\n"; std::cout &lt;&lt; "config:\t" &lt;&lt; configDir &lt;&lt; "\n"; // Do the main routine here } /* Sample output: C:\Documents and Settings\plee\My Documents\Visual Studio 2010\Projects\VCLearning\Debug&gt;boost.exe --help Program Usage: --help produce help message -h [ --host ] arg set the host server -p [ --port ] arg set the server port -c [ --config ] arg set the config path C:\Documents and Settings\plee\My Documents\Visual Studio 2010\Projects\VCLearning\Debug&gt;boost.exe Error: missing required option config C:\Documents and Settings\plee\My Documents\Visual Studio 2010\Projects\VCLearning\Debug&gt;boost.exe --host localhost Error: missing required option config C:\Documents and Settings\plee\My Documents\Visual Studio 2010\Projects\VCLearning\Debug&gt;boost.exe --config . Error: missing required option host C:\Documents and Settings\plee\My Documents\Visual Studio 2010\Projects\VCLearning\Debug&gt;boost.exe --config . --help Program Usage: --help produce help message -h [ --host ] arg set the host server -p [ --port ] arg set the server port -c [ --config ] arg set the config path C:\Documents and Settings\plee\My Documents\Visual Studio 2010\Projects\VCLearning\Debug&gt;boost.exe --host 127.0.0.1 --port 31528 --config . host: 127.0.0.1 port: 31528 config: . C:\Documents and Settings\plee\My Documents\Visual Studio 2010\Projects\VCLearning\Debug&gt;boost.exe -h 127.0.0.1 -p 31528 -c . host: 127.0.0.1 port: 31528 config: . */ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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