Minimac4
|
A wrapper for parsing command-line options using getopt and getopt_long. More...
#include <getopt_wrapper.hpp>
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_desc > | opts_ |
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. | |
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.long_options_
) and original option definitions (opts_
).usage_str_
) and the generated short option string (short_opt_string_
).max_long_opt_length_
) for nicely aligned help output.
|
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.
usage_str | A string describing the program usage, typically displayed when the user requests help. |
long_opts | A vector of option_with_desc structures describing long options, their short equivalents, argument requirements, and descriptions. |
The constructor performs the following steps:
long_options_
array to accommodate all options plus a terminating zeroed element (required by getopt_long
).opts_
into long_options_
.max_long_opt_length_
to track the length of the longest long option name (used for formatting help output).short_opt_string_
:val
is set.:
if the option requires an argument (required_argument
).
|
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.
os | The output stream to which the usage information is written (e.g., std::cout or std::cerr ). |
The method performs the following steps:
usage_str_
) followed by a blank line.opts_
.-
.--
.max_long_opt_length_
) for a clean, columnar format.Example output:
|
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.
|
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.
|
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.
|
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:"
).
|
protected |
Program usage/help string.
Contains a short description of the program usage, typically displayed at the top of the --help
output.