Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

Class template basic_command_line_parser

boost::program_options::basic_command_line_parser

Synopsis

// In header: <boost/program_options/parsers.hpp>

template<typename charT> 
class basic_command_line_parser {
public:

  // public member functions
  basic_command_line_parser(const std::vector< std::basic_string< charT > > &);
  basic_command_line_parser(int, const charT *const);
  basic_command_line_parser & options(const options_description &);
  basic_command_line_parser & 
  positional(const positional_options_description &);
  basic_command_line_parser & style(int);
  basic_command_line_parser & extra_parser(ext_parser);
  basic_parsed_options< charT > run();
  basic_command_line_parser & allow_unregistered();
  basic_command_line_parser & extra_style_parser(style_parser);
};

Description

Command line parser.

The class allows one to specify all the information needed for parsing and to parse the command line. It is primarily needed to emulate named function parameters -- a regular function with 5 parameters will be hard to use and creating overloads with a smaller number of parameters will be confusing.

For the most common case, the function parse_command_line is a better alternative.

There are two typedefs -- command_line_parser and wcommand_line_parser, for charT == char and charT == wchar_t cases.

basic_command_line_parser public member functions

  1. basic_command_line_parser(const std::vector< std::basic_string< charT > > & args);

    Creates a command line parser for the specified arguments list. The 'args' parameter should not include program name.

  2. basic_command_line_parser(int argc, const charT *const argv);

    Creates a command line parser for the specified arguments list. The parameters should be the same as passed to 'main', meaning:

    Parameters:

    argc

    Must be non-negative i.e. >= 0

    argv

    Argv[argc] must be 0 e.g. nullptr and if argc is >0 argv[0] up to argv[argc-1] must point to null terminated strings

  3. basic_command_line_parser & options(const options_description & desc);

    Sets options descriptions to use.

  4. basic_command_line_parser & 
    positional(const positional_options_description & desc);

    Sets positional options description to use.

  5. basic_command_line_parser & style(int);

    Sets the command line style.

  6. basic_command_line_parser & extra_parser(ext_parser);

    Sets the extra parsers.

  7. basic_parsed_options< charT > run();

    Parses the options and returns the result of parsing. Throws on error.

  8. basic_command_line_parser & allow_unregistered();

    Specifies that unregistered options are allowed and should be passed though. For each command like token that looks like an option but does not contain a recognized name, an instance of basic_option<charT> will be added to result, with 'unrecognized' field set to 'true'. It's possible to collect all unrecognized options with the 'collect_unrecognized' funciton.

  9. basic_command_line_parser & extra_style_parser(style_parser s);

PrevUpHomeNext