Minimac4
Loading...
Searching...
No Matches
getopt_wrapper Class Reference

A wrapper for parsing command-line options using getopt and getopt_long. More...

#include <getopt_wrapper.hpp>

Inheritance diagram for getopt_wrapper:
[legend]

Classes

struct  option_with_desc
 Extension of struct option that includes a description string. More...
 

Public Member Functions

 getopt_wrapper (std::string usage_str, std::vector< option_with_desc > long_opts)
 Constructs a getopt_wrapper object for parsing command-line options.
 
void print_usage (std::ostream &os)
 Prints the program usage and all available options to a stream.
 

Protected Attributes

std::vector< option > long_options_
 Storage of long options for getopt parsing.
 
std::vector< option_with_descopts_
 Original option definitions with descriptions.
 
std::string usage_str_
 Program usage/help string.
 
std::string short_opt_string_
 Concatenated short option string.
 
std::size_t max_long_opt_length_ = 0
 Maximum long option length.
 

Detailed Description

A wrapper for parsing command-line options using getopt and getopt_long.

This class simplifies handling both short and long command-line options, automatically generating the short option string and formatting help output.

The class provides:

  • option_with_desc: A structure extending struct option to include a description string for help messages.
  • Storage for long options (long_options_) and original option definitions (opts_).
  • The program usage string (usage_str_) and the generated short option string (short_opt_string_).
  • Tracking of the maximum long option length (max_long_opt_length_) for nicely aligned help output.

Constructor & Destructor Documentation

◆ getopt_wrapper()

getopt_wrapper::getopt_wrapper ( std::string usage_str,
std::vector< option_with_desc > long_opts )
inline

Constructs a getopt_wrapper object for parsing command-line options.

This constructor initializes both short and long options for use with getopt and getopt_long. It sets up internal data structures for option parsing and generates the short option string automatically.

Parameters
usage_strA string describing the program usage, typically displayed when the user requests help.
long_optsA vector of option_with_desc structures describing long options, their short equivalents, argument requirements, and descriptions.

The constructor performs the following steps:

  1. Moves the usage string and option vector into member variables.
  2. Resizes the internal long_options_ array to accommodate all options plus a terminating zeroed element (required by getopt_long).
  3. Copies each option from opts_ into long_options_.
  4. Updates max_long_opt_length_ to track the length of the longest long option name (used for formatting help output).
  5. Builds the short option string short_opt_string_:
    • Adds the short option character if val is set.
    • Appends : if the option requires an argument (required_argument).
Here is the caller graph for this function:

Member Function Documentation

◆ print_usage()

void getopt_wrapper::print_usage ( std::ostream & os)
inline

Prints the program usage and all available options to a stream.

This function formats and displays the usage string followed by a nicely aligned list of command-line options and their descriptions. Both short and long options are displayed if available.

Parameters
osThe output stream to which the usage information is written (e.g., std::cout or std::cerr).

The method performs the following steps:

  1. Prints the main usage string (usage_str_) followed by a blank line.
  2. Iterates over all options in opts_.
  3. For each option with a description:
    • Prints the short option (if printable) prefixed with -.
    • Prints the long option name (if present) prefixed with --.
    • Aligns descriptions based on the longest long option (max_long_opt_length_) for a clean, columnar format.
  4. Flushes the output stream to ensure all text is written.

Example output:

Usage: myprogram [options]
-h, --help Display this help message
-v, --version Show version information
Here is the caller graph for this function:

Member Data Documentation

◆ long_options_

std::vector<option> getopt_wrapper::long_options_
protected

Storage of long options for getopt parsing.

This vector holds the struct option objects used by getopt_long. It is automatically populated based on the provided option_with_desc definitions.

◆ max_long_opt_length_

std::size_t getopt_wrapper::max_long_opt_length_ = 0
protected

Maximum long option length.

Used when formatting help output so that all descriptions are aligned to the same column, regardless of option name length.

◆ opts_

std::vector<option_with_desc> getopt_wrapper::opts_
protected

Original option definitions with descriptions.

This vector stores the extended option structures (option_with_desc), which include descriptions for generating nicely formatted help/usage messages.

◆ short_opt_string_

std::string getopt_wrapper::short_opt_string_
protected

Concatenated short option string.

Generated automatically from the provided options. This string is passed to getopt() for parsing short options (e.g., "hv:o:").

◆ usage_str_

std::string getopt_wrapper::usage_str_
protected

Program usage/help string.

Contains a short description of the program usage, typically displayed at the top of the --help output.


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