Main   Classes   Namespace members   Examples   Recipes   Rationale   Related pages

The cmdline class

When validation or automatic help message are not needed, it's possible to use low-level boost::program_options::cmdline class, like shown in this example.

#include <boost/program_options/cmdline.hpp>
#include <boost/program_options/parsers.hpp>

using namespace boost;
using namespace boost::program_options;

#include <iostream>
using namespace std;


int main(int ac, const char **av)
{
    try {
        cmdline cmd(ac, av, cmdline::unix_style);

        // add long option 'help' with no short equvivalent, and no parameter
        cmd.add_option("help", 0);
        // add long option with short equivivalent.
        cmd.add_option("version", 'v');    
        // add an option with required parameter.
        cmd.add_option("string", 'c', ':');
    
        options_and_arguments oa = parse_command_line(cmd);
    
        if (oa.count("help"))
            cout << "Have --help option\n";
        if (oa.count("version"))
            cout << "Have --version option\n";
        if (oa.count("string")) {
            cout << "Have --string option with value " << oa["string"] << "\n";         
        }
    }
    catch(exception& e)
    {
        cout << e.what() << "\n";
    }    
}

Generated on 23 May 2003 with
doxygen