An instance of this class offers operators to retrieve command line options and arguments.
More...
#include <Options.hpp>
|
| Options (int const argc, char const *const *const argv, char const *const usage, Parameter< OptionT > const (&defs)[DefCount]) |
| Construct an options functor. More...
|
|
Options & | operator() () |
| Updates the internal state by parsing the next option. More...
|
|
| operator OptionT () const |
| Implicitly cast to the current option. More...
|
|
const char * | operator[] (int const i) const |
| Retrieve arguments to the current option. More...
|
|
std::string | usage () const |
| Returns a string for usage output, created from the option definitions. More...
|
|
utility::Underlined | show (int const i, int const n=1) const |
| Provide a string containing the entire command line, with the indexed argument highlighted. More...
|
|
std::string | show () const |
| Highlight the last recently accessed argument. More...
|
|
int | offset () const |
| Returns the argument offset of the current parameter/argument. More...
|
|
|
const Parameter< OptionT > & | get (char const ch) |
| Finds the short option matching the given character. More...
|
|
const Parameter< OptionT > & | get (char const *const str) |
| Finds the long option matching the given string. More...
|
|
|
static const char * | removePath (char const *const file) |
| Returns a pointer to the file name portion of the given string. More...
|
|
static bool | match (char const *const lstr, char const *const rstr) |
| Returns true if the given strings match. More...
|
|
static bool | bmatch (char const *const str, char const *const prefix) |
| Returns true if the given string starts with the given prefix. More...
|
|
|
const int | argc |
| The number of command line arguments.
|
|
const char *const *const | argv |
| The command line arguments.
|
|
const char *const | usageStr |
| A string literal for the usage() output.
|
|
const Parameter< OptionT >(& | defs )[DefCount] |
| A reference to the option definitions.
|
|
const Parameter< OptionT > | opt_unknown |
| The option definition to use for unknown options. More...
|
|
const Parameter< OptionT > | opt_noopt |
| The option definition to use for non-options. More...
|
|
const Parameter< OptionT > | opt_dash |
| The option definition to use for a single dash. More...
|
|
const Parameter< OptionT > | opt_ldash |
| The option definition to use for a single double-dash. More...
|
|
int | argi |
| The index of the command line argument containing the current option.
|
|
const char * | argp |
| Points to the current short option character.
|
|
const Parameter< OptionT > * | current |
| Points to the current option definition.
|
|
int | showi |
| The argument index to show if no argument is supplied to show(). More...
|
|
template<class OptionT, size_t DefCount>
class nih::Options< OptionT, DefCount >
An instance of this class offers operators to retrieve command line options and arguments.
Check the operator ()
and operator []
for use.
- Template Parameters
-
OptionT | An enum or enum class matching the requirements set by enum_has_members |
DefCount | The number of option definitions |
◆ Options()
template<class OptionT , size_t DefCount>
nih::Options< OptionT, DefCount >::Options |
( |
int const |
argc, |
|
|
char const *const *const |
argv, |
|
|
char const *const |
usage, |
|
|
Parameter< OptionT > const (&) |
defs[DefCount] |
|
) |
| |
|
inline |
Construct an options functor.
- Parameters
-
argc,argv | The command line arguments |
usage | A usage string following "usage: progname " |
defs | An array of parameter definitions |
◆ bmatch()
template<class OptionT , size_t DefCount>
static bool nih::Options< OptionT, DefCount >::bmatch |
( |
char const *const |
str, |
|
|
char const *const |
prefix |
|
) |
| |
|
inlinestaticprivate |
Returns true if the given string starts with the given prefix.
- Parameters
-
str,prefix | Two 0 terminated strings |
- Return values
-
true | The string starts with the prefix |
false | The string does not start with the prefix |
◆ get() [1/2]
template<class OptionT , size_t DefCount>
Finds the long option matching the given string.
- Parameters
-
str | The long option to find |
- Returns
- An option definition by reference
◆ get() [2/2]
template<class OptionT , size_t DefCount>
Finds the short option matching the given character.
- Parameters
-
ch | The short option to find |
- Returns
- An option definition by reference
◆ match()
template<class OptionT , size_t DefCount>
static bool nih::Options< OptionT, DefCount >::match |
( |
char const *const |
lstr, |
|
|
char const *const |
rstr |
|
) |
| |
|
inlinestaticprivate |
Returns true if the given strings match.
- Parameters
-
lstr,rstr | Two 0 terminated strings |
- Return values
-
true | The given strings match |
false | The strings do not match |
◆ offset()
template<class OptionT , size_t DefCount>
Returns the argument offset of the current parameter/argument.
- Warning
- This may return a value >= argc if the current state is OptionT::OPT_DONE
- Returns
- The current argument index
◆ operator OptionT()
template<class OptionT , size_t DefCount>
nih::Options< OptionT, DefCount >::operator OptionT |
( |
| ) |
const |
|
inline |
Implicitly cast to the current option.
- Returns
- An OptionT member representing the current option
- Return values
-
OPT_UNKNOWN | An option that was not in the list of option definitions was encountered |
OPT_NOOPT | An argument that is not an option was encountered |
OPT_DASH | A lone dash "-" was encountered |
OPT_LDASH | A lone long dash "--" was encountered |
OPT_DONE | All arguments have been processed, or argument processing has not yet started |
◆ operator()()
template<class OptionT , size_t DefCount>
Updates the internal state by parsing the next option.
When reaching the end of the argument list, the internal state is reset, so a successive call will restart the argument parsing.
- Returns
- A self-reference
◆ operator[]()
template<class OptionT , size_t DefCount>
const char* nih::Options< OptionT, DefCount >::operator[] |
( |
int const |
i | ) |
const |
|
inline |
Retrieve arguments to the current option.
The string containing the current option is returned with i = 0, the arguments following the option with greater values of i.
When no more arguments are left the empty string is returned.
- Parameters
-
i | The index of the argument to retrieve |
- Returns
- The option or one of its arguments
◆ removePath()
template<class OptionT , size_t DefCount>
static const char* nih::Options< OptionT, DefCount >::removePath |
( |
char const *const |
file | ) |
|
|
inlinestaticprivate |
Returns a pointer to the file name portion of the given string.
- Parameters
-
file | The string containing the path to the file |
- Returns
- A pointer to the file name portion of the path
◆ show() [1/2]
template<class OptionT , size_t DefCount>
std::string nih::Options< OptionT, DefCount >::show |
( |
| ) |
const |
|
inline |
Highlight the last recently accessed argument.
- Returns
- A string with the last recently accessed argument underlined
- See also
- show(int const, int const = 1)
◆ show() [2/2]
template<class OptionT , size_t DefCount>
Provide a string containing the entire command line, with the indexed argument highlighted.
The current implementation highlights arguments by underlining them with ^~~~
.
- Parameters
-
i | The argument index, like operator [] |
n | The number of arguments to highlight, highlights all remaining arguments if n <= 0 |
- Returns
- A string formatted to highlight the given argument
◆ usage()
template<class OptionT , size_t DefCount>
std::string nih::Options< OptionT, DefCount >::usage |
( |
| ) |
const |
|
inline |
Returns a string for usage output, created from the option definitions.
- Returns
- A usage string for printing on the CLI
◆ opt_dash
template<class OptionT , size_t DefCount>
Initial value:{
OptionT::OPT_DASH, 0, nullptr, nullptr, nullptr
}
The option definition to use for a single dash.
◆ opt_ldash
template<class OptionT , size_t DefCount>
Initial value:{
OptionT::OPT_LDASH, 0, nullptr, nullptr, nullptr
}
The option definition to use for a single double-dash.
◆ opt_noopt
template<class OptionT , size_t DefCount>
Initial value:{
OptionT::OPT_NOOPT, 0, nullptr, nullptr, nullptr
}
The option definition to use for non-options.
◆ opt_unknown
template<class OptionT , size_t DefCount>
Initial value:{
OptionT::OPT_UNKNOWN, 0, nullptr, nullptr, nullptr
}
The option definition to use for unknown options.
◆ showi
template<class OptionT , size_t DefCount>
The argument index to show if no argument is supplied to show().
This is initially 0 for each new argument and updated by use of the subscript operator.
This is for error handling convenience and not considered part of the state.
The documentation for this class was generated from the following file: