libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
Argz< String > Class Template Reference

Template command-line argument parser. More...

#include <argz.hpp>

Collaboration diagram for Argz< String >:
[legend]

Public Member Functions

 ~Argz ()=default
 Argz ()=default
 Argz (int argc, char **argv)
 Construct and immediately ingest argv.
 Argz (const Argz< String > &a)
Argz< String > & operator= (const Argz< String > &a)
 Argz (Argz< String > &&a)
Argz< String > & operator= (Argz< String > &&a)
Argz< String > & initArgs (int argc, char **argv)
 Ingest argc/argv, converting to the target String type.
void reset ()
 Reset the internal parse cursor to the beginning of argv.
Argz< String > & addOptionSingle (const int &c, const String &description)
 Register a flag-only short option (e.g.
Argz< String > & addOptionSingleValue (const int &c, const String &description)
 Register a short option that requires a value argument (e.g.
Argz< String > & addOptionDouble (const int &code, const String &value, const String &description)
 Register a flag-only long option (e.g.
Argz< String > & addOptionDoubleValue (const int &code, const String &value, const String &description)
 Register a long option that requires a value argument (e.g.
int lookUpCode (const String &value)
 Look up the integer code registered for a long option name.
int proc (Argument< String > &a)
 Advance the cursor and parse the next argument.
template<typename T>
void help (T &cout)
 Print all registered options to any ostream-like object.
const size_t count () const

Protected Attributes

ArgumentData< String > arg_data
std::unordered_map< int, Argument< String > > arg_info

Private Attributes

int index = 0
int cindex = 1

Detailed Description

template<StringType String>
class Argz< String >

Template command-line argument parser.

Template Parameters
StringString type satisfying StringType (usually std::string or std::wstring).

Options are registered with addOption*() before parsing. Call proc() in a loop until it returns -1 to iterate over all arguments.

Definition at line 143 of file argz.hpp.

Constructor & Destructor Documentation

◆ ~Argz()

template<StringType String>
Argz< String >::~Argz ( )
default

◆ Argz() [1/4]

◆ Argz() [2/4]

template<StringType String>
Argz< String >::Argz ( int argc,
char ** argv )
inline

Construct and immediately ingest argv.

Parameters
argcArgument count from main().
argvArgument vector from main().

Definition at line 153 of file argz.hpp.

References initArgs().

◆ Argz() [3/4]

template<StringType String>
Argz< String >::Argz ( const Argz< String > & a)
inline

Definition at line 154 of file argz.hpp.

References arg_data, arg_info, Argz(), cindex, and index.

◆ Argz() [4/4]

template<StringType String>
Argz< String >::Argz ( Argz< String > && a)
inline

Definition at line 169 of file argz.hpp.

References arg_data, arg_info, Argz(), cindex, and index.

Member Function Documentation

◆ addOptionDouble()

template<StringType String>
Argz< String > & Argz< String >::addOptionDouble ( const int & code,
const String & value,
const String & description )
inline

Register a flag-only long option (e.g.

--verbose).

Parameters
codeUnique integer code associated with this option.
valueLong option name string (without --).
descriptionHelp text.
Returns
Reference to *this for chaining.

Definition at line 254 of file argz.hpp.

References ARG_DOUBLE, arg_info, Argument< String >::arg_letter, Argument< String >::arg_name, Argument< String >::arg_type, Argz(), and Argument< String >::desc.

◆ addOptionDoubleValue()

template<StringType String>
Argz< String > & Argz< String >::addOptionDoubleValue ( const int & code,
const String & value,
const String & description )
inline

Register a long option that requires a value argument (e.g.

--output file).

Parameters
codeUnique integer code.
valueLong option name string.
descriptionHelp text.
Returns
Reference to *this for chaining.

Definition at line 271 of file argz.hpp.

References ARG_DOUBLE_VALUE, arg_info, Argument< String >::arg_letter, Argument< String >::arg_name, Argument< String >::arg_type, Argz(), and Argument< String >::desc.

◆ addOptionSingle()

template<StringType String>
Argz< String > & Argz< String >::addOptionSingle ( const int & c,
const String & description )
inline

Register a flag-only short option (e.g.

-v, -h).

Parameters
cShort option character code.
descriptionHelp text.
Returns
Reference to *this for chaining.

Definition at line 223 of file argz.hpp.

References arg_info, Argument< String >::arg_letter, ARG_SINGLE, Argument< String >::arg_type, Argz(), and Argument< String >::desc.

Referenced by proc_args().

◆ addOptionSingleValue()

template<StringType String>
Argz< String > & Argz< String >::addOptionSingleValue ( const int & c,
const String & description )
inline

Register a short option that requires a value argument (e.g.

-o file).

Parameters
cShort option character code.
descriptionHelp text.
Returns
Reference to *this for chaining.

Definition at line 238 of file argz.hpp.

References arg_info, Argument< String >::arg_letter, ARG_SINGLE_VALUE, Argument< String >::arg_type, Argz(), and Argument< String >::desc.

◆ count()

template<StringType String>
const size_t Argz< String >::count ( ) const
inline
Returns
Number of argv entries consumed so far.

Definition at line 492 of file argz.hpp.

References index.

◆ help()

template<StringType String>
template<typename T>
void Argz< String >::help ( T & cout)
inline

Print all registered options to any ostream-like object.

Template Parameters
TOutput stream type (e.g. std::ostream, std::wostream).
Parameters
coutDestination stream.

Definition at line 443 of file argz.hpp.

References ARG_DOUBLE, ARG_DOUBLE_VALUE, arg_info, ARG_SINGLE, and ARG_SINGLE_VALUE.

Referenced by proc_args().

◆ initArgs()

template<StringType String>
Argz< String > & Argz< String >::initArgs ( int argc,
char ** argv )
inline

Ingest argc/argv, converting to the target String type.

Parameters
argcArgument count.
argvArgument vector.
Returns
Reference to *this for method chaining.

Definition at line 185 of file argz.hpp.

References arg_data, Argz(), and reset().

Referenced by Argz().

◆ lookUpCode()

template<StringType String>
int Argz< String >::lookUpCode ( const String & value)
inline

Look up the integer code registered for a long option name.

Parameters
valueLong option name (without --).
Returns
The registered code, or -1 if not found.

Definition at line 286 of file argz.hpp.

References arg_info.

Referenced by proc().

◆ operator=() [1/2]

template<StringType String>
Argz< String > & Argz< String >::operator= ( Argz< String > && a)
inline

Definition at line 171 of file argz.hpp.

References arg_data, arg_info, Argz(), cindex, and index.

◆ operator=() [2/2]

template<StringType String>
Argz< String > & Argz< String >::operator= ( const Argz< String > & a)
inline

Definition at line 156 of file argz.hpp.

References arg_data, arg_info, Argz(), cindex, and index.

◆ proc()

template<StringType String>
int Argz< String >::proc ( Argument< String > & a)
inline

Advance the cursor and parse the next argument.

Parameters
aOutput: filled with the matched option details.
Returns
The option code on success; '-' for a positional argument; -1 when exhausted.
Exceptions
ArgException<String>On unrecognised options or missing values.

Definition at line 301 of file argz.hpp.

References arg_data, ARG_DOUBLE, arg_info, Argument< String >::arg_name, ARG_NONE, ARG_SINGLE, ARG_SINGLE_VALUE, Argument< String >::arg_type, Argument< String >::arg_value, cindex, index, and lookUpCode().

Referenced by proc_args().

◆ reset()

template<StringType String>
void Argz< String >::reset ( )
inline

Reset the internal parse cursor to the beginning of argv.

Definition at line 212 of file argz.hpp.

References cindex, and index.

Referenced by initArgs().

Member Data Documentation

◆ arg_data

template<StringType String>
ArgumentData<String> Argz< String >::arg_data
protected

Definition at line 494 of file argz.hpp.

Referenced by Argz(), Argz(), initArgs(), operator=(), operator=(), and proc().

◆ arg_info

template<StringType String>
std::unordered_map<int, Argument<String> > Argz< String >::arg_info
protected

◆ cindex

template<StringType String>
int Argz< String >::cindex = 1
private

Definition at line 497 of file argz.hpp.

Referenced by Argz(), Argz(), operator=(), operator=(), proc(), and reset().

◆ index

template<StringType String>
int Argz< String >::index = 0
private

Definition at line 497 of file argz.hpp.

Referenced by Argz(), Argz(), count(), operator=(), operator=(), proc(), and reset().


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