# Util::Getopt Class Reference

Parse the command line options of a program. More...

`#include <utils.hpp>`

Inheritance diagram for Util::Getopt:

[legend]
Collaboration diagram for Util::Getopt:

[legend]
List of all members.

## Public Member Functions

| Getopt ()
| Default constructor.
virtual | ~Getopt ()
| Destructor.
int | getopt (int argc, char *const argv[], const std::string &optstring)
| Parse command line arguments.
virtual int | option (int opt, const std::string &optarg, int optopt)=0
| Callback used by getopt() to pass on each option and its argument (if any).
virtual int | nonoption (const std::string &argv)
| Callback used by getopt() to pass on each non-option parameter found on the command line.
const std::string & | progname () const
| Program name (argv[0]).
int | errcnt () const
| Total number of errors returned by calls to option().

## Detailed Description

Parse the command line options of a program.

A wrapper around the POSIX getopt(3) function. Parses the command line options and passes each option to virtual option(). A derived class implements this method to handle options as needed. Similarly, remaining non-option parameters are passed to the virtual nonoption() method.

## Member Function Documentation

int Util::Getopt::getopt | ( | int | argc,
|  | char *const | argv[],
|  | const std::string & | optstring
| ) |

Parse command line arguments.

Parses the command line arguments. Calls option() with the character value of the option and its argument (if any) for each recognized option and with ':' or '?' for unrecognized options. See the manual pages for getopt(3) for details. In addition, nonoption() is invoked for each remaining non-option parameter on the command line.

Parameters:

  | argc | Argument count as passed to main() on program invocation.
  | argv | Argument array as passed to main() on program invocation.
  | optstring | String containing the legitimate option characters.

Returns:
  Number of errors (the sum of the return values from option() and nonoption()).

int Util::Getopt::nonoption | ( | const std::string & | argv | ) | `[virtual]`

Callback used by getopt() to pass on each non-option parameter found on the command line.

Implement this method in a derived class to handle the non-option parameters as needed. The default implementation ignores all non-option parameters.

Parameters:

  | argv | The non-option parameter from the command line.

Returns:
  0 if successful, 1 in case of an error.

Reimplemented in Params, and Params.

virtual int Util::Getopt::option | ( | int | opt,
|  | const std::string & | optarg,
|  | int | optopt
| ) | `[pure virtual]`

Callback used by getopt() to pass on each option and its argument (if any).

Implement this method in a derived class to handle the options as needed. See the manual pages for getopt(3) for further details, in particular, the semantics of optarg and optopt.

Parameters:

  | opt | Value of the option character as returned by getopt(3).
  | optarg | The corresponding option argument.
  | optopt | The actual option character in case of an unrecognized option or a missing option argument (opt is '?' or ':').

Returns:
  0 if successful, 1 in case of an error.

Implemented in Params, and Params.

The documentation for this class was generated from the following files:

- utils.hpp
- utils.cpp