powerd++  0.4.4
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
nih::Options< OptionT, DefCount > Class Template Reference

An instance of this class offers operators to retrieve command line options and arguments. More...

#include <Options.hpp>

Public Member Functions

 Options (int const argc, char const *const *const argv, char const *const usage, Parameter< OptionT > const (&defs)[DefCount])
 Construct an options functor. More...
 
Optionsoperator() ()
 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...
 

Private Member Functions

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 Private Member Functions

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...
 

Private Attributes

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...
 

Detailed Description

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
OptionTAn enum or enum class matching the requirements set by enum_has_members
DefCountThe number of option definitions

Constructor & Destructor Documentation

◆ 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,argvThe command line arguments
usageA usage string following "usage: progname "
defsAn array of parameter definitions

Member Function Documentation

◆ 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,prefixTwo 0 terminated strings
Return values
trueThe string starts with the prefix
falseThe string does not start with the prefix

◆ get() [1/2]

template<class OptionT , size_t DefCount>
const Parameter<OptionT>& nih::Options< OptionT, DefCount >::get ( char const *const  str)
inlineprivate

Finds the long option matching the given string.

Parameters
strThe long option to find
Returns
An option definition by reference

◆ get() [2/2]

template<class OptionT , size_t DefCount>
const Parameter<OptionT>& nih::Options< OptionT, DefCount >::get ( char const  ch)
inlineprivate

Finds the short option matching the given character.

Parameters
chThe 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,rstrTwo 0 terminated strings
Return values
trueThe given strings match
falseThe strings do not match

◆ offset()

template<class OptionT , size_t DefCount>
int nih::Options< OptionT, DefCount >::offset ( ) const
inline

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_UNKNOWNAn option that was not in the list of option definitions was encountered
OPT_NOOPTAn argument that is not an option was encountered
OPT_DASHA lone dash "-" was encountered
OPT_LDASHA lone long dash "--" was encountered
OPT_DONEAll arguments have been processed, or argument processing has not yet started

◆ operator()()

template<class OptionT , size_t DefCount>
Options& nih::Options< OptionT, DefCount >::operator() ( )
inline

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
iThe 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
fileThe 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>
utility::Underlined nih::Options< OptionT, DefCount >::show ( int const  i,
int const  n = 1 
) const
inline

Provide a string containing the entire command line, with the indexed argument highlighted.

The current implementation highlights arguments by underlining them with ^~~~.

Parameters
iThe argument index, like operator []
nThe 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

Member Data Documentation

◆ opt_dash

template<class OptionT , size_t DefCount>
const Parameter<OptionT> nih::Options< OptionT, DefCount >::opt_dash
private
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>
const Parameter<OptionT> nih::Options< OptionT, DefCount >::opt_ldash
private
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>
const Parameter<OptionT> nih::Options< OptionT, DefCount >::opt_noopt
private
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>
const Parameter<OptionT> nih::Options< OptionT, DefCount >::opt_unknown
private
Initial value:
{
OptionT::OPT_UNKNOWN, 0, nullptr, nullptr, nullptr
}

The option definition to use for unknown options.

◆ showi

template<class OptionT , size_t DefCount>
int nih::Options< OptionT, DefCount >::showi
mutableprivate

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: