Main   Classes   Namespace members   Examples   Recipes   Rationale   Related pages

Options description

Example of quite a simple usage. Options are registered and the command line is parsed. The user is responsible to interpreting the option values. This also how automatic help message.

#include <boost/program_options.hpp>

using namespace boost;
using namespace boost::program_options;

#include <iostream>
using namespace std;


int main(int ac, const char **av)
{
    try {
        options_description desc("Allowed options");
        desc.add_options()
        // First parameter is name, here, only long name is given
        // Second parameter is option's parameter -- none in this case
        // Third parameter is description
        ("help", "", "produce a help screen")
        // The first parameter specifies both long and short option names
        ("version,v", "", "print the version number")
        // This option has required parameter called "arg"
        ("string,s", "arg", "output the specified string")
        ;
        
    
        options_and_arguments oa = parse_command_line(ac, av, desc);
    
        if (oa.count("help")) {
            cout << "Usage: options_description [options]\n";
            cout << desc;
            return 0;
        }
        if (oa.count("version")) {
            cout << "Version 1.\n";
            return 0;
        }
        if (oa.count("string")) {
            cout << "The string is \"" << oa["string"] << "\"\n";
        }
    }
    catch(exception& e)
    {
        cout << e.what() << "\n";
    }    
}

Generated on 23 May 2003 with
doxygen