Public Member Functions | List of all members
CommandLine< KeyT > Class Template Reference

A compact command line argument parser. More...

#include <CommandLine.hpp>

Public Member Functions

 CommandLine (CommandLineStyle style_=CommandLineStyle::Detect)
 Create a command line parser with the specified style. More...
 
 CommandLine (const CommandLine &)=default
 Create a command line parser by copying the supplied parser. More...
 
 CommandLine (CommandLine &&) noexcept=default
 Create a command line parser by moving the supplied parser. More...
 
std::string getFinalValue (size_t index=0) const
 Get the final value with the specified index as a string or throw if no final value is available. More...
 
double getFinalValueAsDouble (size_t index=0) const
 Get the final value as a double or throw if no final value is available. More...
 
double getFinalValueAsDoubleOrDefault (double defaultValue, size_t index=0) const
 Get the final value as a double or return the supplied default if no final value is available. More...
 
float getFinalValueAsFloat (size_t index=0) const
 Get the final value as a float or throw if no final value is available. More...
 
float getFinalValueAsFloatOrDefault (float defaultValue, size_t index=0) const
 Get the final value as a float or return the supplied default if no final value is available. More...
 
size_t getFinalValueCount () const
 Get the number of final values available. More...
 
std::string getFinalValueOrDefault (const std::string &defaultValue, size_t index=0) const
 Get the final value as a string or return the supplied default if no final value is available. More...
 
std::string getHelpText (size_t indent, size_t totalWidth, CommandLineStyle styleOverride=CommandLineStyle::Detect) const
 Get the help text. More...
 
std::string getOption (KeyT key) const
 Get the specified option value as a UTF-8 string. More...
 
double getOptionAsDouble (KeyT key) const
 Get the specified option value as a double. More...
 
double getOptionAsDoubleOrDefault (KeyT key, double defaultValue) const
 Get the specified option value as a double or return the supplied value if there is no such option. More...
 
float getOptionAsFloat (KeyT key) const
 Get the specified option value as a float. More...
 
float getOptionAsFloatOrDefault (KeyT key, float defaultValue) const
 Get the specified option value as a float or return the supplied value if there is no such option. More...
 
int getOptionAsInt (KeyT key) const
 Get the specified option value as a signed integer value. More...
 
int getOptionAsIntOrDefault (KeyT key, int defaultValue) const
 Get the specified option value as a signed int, or the specified default if there is no such option. More...
 
long long getOptionAsLong (KeyT key) const
 Get the specified option value as a signed long long value. More...
 
long long getOptionAsLongOrDefault (KeyT key, long long defaultValue) const
 Get the specified option value as a signed long long, or the specified default if there is no such option. More...
 
short getOptionAsShort (KeyT key) const
 Get the specified option value as a signed short value. More...
 
short getOptionAsShortOrDefault (KeyT key, short defaultValue) const
 Get the specified option value as a signed short, or the specified default if there is no such option. More...
 
unsigned int getOptionAsUnsignedInt (KeyT key) const
 Get the specified option value as an unsigned integer value. More...
 
unsigned int getOptionAsUnsignedIntOrDefault (KeyT key, unsigned int defaultValue) const
 Get the specified option value as an unsigned int, or the specified default if there is no such option. More...
 
unsigned long long getOptionAsUnsignedLong (KeyT key) const
 Get the specified option value as an unsigned long long value. More...
 
unsigned long long getOptionAsUnsignedLongOrDefault (KeyT key, unsigned long long defaultValue) const
 Get the specified option value as an unsigned long long, or the specified default if there is no such option. More...
 
unsigned short getOptionAsUnsignedShort (KeyT key) const
 Get the specified option value as an unsigned short value. More...
 
unsigned short getOptionAsUnsignedShortOrDefault (KeyT key, unsigned short defaultValue) const
 Get the specified option value as an unsigned short, or the specified default if there is no such option. More...
 
std::string getOptionOrDefault (KeyT key, std::string defaultValue) const
 Get the specified option value as a string, or the specified default if there is no such option. More...
 
bool hasOption (KeyT key) const
 Does the command line option set have the specified option? More...
 
CommandLineoperator= (const CommandLine &)=default
 Assign the current command line parser to a copy of the supplied parser. More...
 
CommandLineoperator= (CommandLine &&) noexcept=default
 Assign the current command line parser by moving the contents of the supplied parser. More...
 
void parse (int argc, char *argv[], bool ignoreFirst)
 Parse the input arguments. More...
 
void parse (int argc, const char *argv[], bool ignoreFirst)
 Parse the input arguments. More...
 
void parse (const std::vector< std::string > &args, bool ignoreFirst)
 Parse the input arguments, which are supplied as a vector. More...
 
CommandLinewithFinalValue ()
 Specify that the command line arguments include one (SEV style) or one or more (SSV style) final stand alone values. More...
 
CommandLinewithHelpOption (KeyT helpKey, const std::string &shortSwitch, const std::string &longSwitch, const std::string &helpDocumentation)
 Specify the help command line short and long switches. More...
 
CommandLinewithOption (KeyT key, const std::string &shortSwitch, const std::string &longSwitch, bool hasValue, const std::string &documentation)
 Specify an option that has a short style switch and a long style switch. More...
 
CommandLinewithOption (KeyT key, const std::string &shortSwitch, bool hasValue, const std::string &documentation)
 Specify an option that has a short style switch. More...
 
CommandLinewithOptionalFinalValue ()
 Specify that the command line arguments include one (SEV style) or one or more (SSV style) final stand alone values. More...
 

Detailed Description

template<typename KeyT>
class Balau::CommandLine< KeyT >

A compact command line argument parser.

Constructor & Destructor Documentation

◆ CommandLine() [1/3]

Create a command line parser with the specified style.

Parameters
style_the command line style to use

◆ CommandLine() [2/3]

CommandLine ( const CommandLine< KeyT > &  )
default

Create a command line parser by copying the supplied parser.

◆ CommandLine() [3/3]

CommandLine ( CommandLine< KeyT > &&  )
defaultnoexcept

Create a command line parser by moving the supplied parser.

Member Function Documentation

◆ getFinalValue()

std::string getFinalValue ( size_t  index = 0) const
inline

Get the final value with the specified index as a string or throw if no final value is available.

Exceptions
OptionNotFoundExceptionif this parser does not have final values or the index is out of range

◆ getFinalValueAsDouble()

double getFinalValueAsDouble ( size_t  index = 0) const
inline

Get the final value as a double or throw if no final value is available.

Exceptions
OptionNotFoundExceptionif no final value exists or the parser does not have final values
OptionValueExceptionif the value supplied on the command line was invalid

◆ getFinalValueAsDoubleOrDefault()

double getFinalValueAsDoubleOrDefault ( double  defaultValue,
size_t  index = 0 
) const
inline

Get the final value as a double or return the supplied default if no final value is available.

Exceptions
OptionNotFoundExceptionif no final value exists or the parser does not have final values
OptionValueExceptionif the value supplied on the command line was invalid

◆ getFinalValueAsFloat()

float getFinalValueAsFloat ( size_t  index = 0) const
inline

Get the final value as a float or throw if no final value is available.

Exceptions
OptionNotFoundExceptionif no final value exists or the parser does not have final values
OptionValueExceptionif the value supplied on the command line was invalid

◆ getFinalValueAsFloatOrDefault()

float getFinalValueAsFloatOrDefault ( float  defaultValue,
size_t  index = 0 
) const
inline

Get the final value as a float or return the supplied default if no final value is available.

Exceptions
OptionNotFoundExceptionif no final value exists or the parser does not have final values
OptionValueExceptionif the value supplied on the command line was invalid

◆ getFinalValueCount()

size_t getFinalValueCount ( ) const
inline

Get the number of final values available.

This will be zero or 1 for SEV style command lines, or zero or more for SSV style command lines.

◆ getFinalValueOrDefault()

std::string getFinalValueOrDefault ( const std::string &  defaultValue,
size_t  index = 0 
) const
inline

Get the final value as a string or return the supplied default if no final value is available.

◆ getHelpText()

std::string getHelpText ( size_t  indent,
size_t  totalWidth,
CommandLineStyle  styleOverride = CommandLineStyle::Detect 
) const
inline

Get the help text.

For parsers that are configured with CommandLineStyle::Detect, the help text will default to switch-space-value unless this is overridden here.

If both styles need to be communicated, this method can be called twice with different overrides.

Note that the implementation of this method does not current do word break analysis, thus it is currently up to the end developer to ensure words are not cut across lines (patches to implement word break analysis are welcome).

◆ getOption()

std::string getOption ( KeyT  key) const
inline

Get the specified option value as a UTF-8 string.

Exceptions
OptionNotFoundExceptionif no such option was provided in the command line

◆ getOptionAsDouble()

double getOptionAsDouble ( KeyT  key) const
inline

Get the specified option value as a double.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsDoubleOrDefault()

double getOptionAsDoubleOrDefault ( KeyT  key,
double  defaultValue 
) const
inline

Get the specified option value as a double or return the supplied value if there is no such option.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsFloat()

float getOptionAsFloat ( KeyT  key) const
inline

Get the specified option value as a float.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsFloatOrDefault()

float getOptionAsFloatOrDefault ( KeyT  key,
float  defaultValue 
) const
inline

Get the specified option value as a float or return the supplied value if there is no such option.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsInt()

int getOptionAsInt ( KeyT  key) const
inline

Get the specified option value as a signed integer value.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsIntOrDefault()

int getOptionAsIntOrDefault ( KeyT  key,
int  defaultValue 
) const
inline

Get the specified option value as a signed int, or the specified default if there is no such option.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsLong()

long long getOptionAsLong ( KeyT  key) const
inline

Get the specified option value as a signed long long value.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsLongOrDefault()

long long getOptionAsLongOrDefault ( KeyT  key,
long long  defaultValue 
) const
inline

Get the specified option value as a signed long long, or the specified default if there is no such option.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsShort()

short getOptionAsShort ( KeyT  key) const
inline

Get the specified option value as a signed short value.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsShortOrDefault()

short getOptionAsShortOrDefault ( KeyT  key,
short  defaultValue 
) const
inline

Get the specified option value as a signed short, or the specified default if there is no such option.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsUnsignedInt()

unsigned int getOptionAsUnsignedInt ( KeyT  key) const
inline

Get the specified option value as an unsigned integer value.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsUnsignedIntOrDefault()

unsigned int getOptionAsUnsignedIntOrDefault ( KeyT  key,
unsigned int  defaultValue 
) const
inline

Get the specified option value as an unsigned int, or the specified default if there is no such option.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsUnsignedLong()

unsigned long long getOptionAsUnsignedLong ( KeyT  key) const
inline

Get the specified option value as an unsigned long long value.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsUnsignedLongOrDefault()

unsigned long long getOptionAsUnsignedLongOrDefault ( KeyT  key,
unsigned long long  defaultValue 
) const
inline

Get the specified option value as an unsigned long long, or the specified default if there is no such option.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsUnsignedShort()

unsigned short getOptionAsUnsignedShort ( KeyT  key) const
inline

Get the specified option value as an unsigned short value.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionAsUnsignedShortOrDefault()

unsigned short getOptionAsUnsignedShortOrDefault ( KeyT  key,
unsigned short  defaultValue 
) const
inline

Get the specified option value as an unsigned short, or the specified default if there is no such option.

Exceptions
OptionNotFoundExceptionif the specified option was not found in the parsed command line
OptionValueExceptionif the value supplied on the command line was invalid

◆ getOptionOrDefault()

std::string getOptionOrDefault ( KeyT  key,
std::string  defaultValue 
) const
inline

Get the specified option value as a string, or the specified default if there is no such option.

◆ hasOption()

bool hasOption ( KeyT  key) const
inline

Does the command line option set have the specified option?

◆ operator=() [1/2]

CommandLine& operator= ( const CommandLine< KeyT > &  )
default

Assign the current command line parser to a copy of the supplied parser.

◆ operator=() [2/2]

CommandLine& operator= ( CommandLine< KeyT > &&  )
defaultnoexcept

Assign the current command line parser by moving the contents of the supplied parser.

◆ parse() [1/3]

void parse ( int  argc,
char *  argv[],
bool  ignoreFirst 
)
inline

Parse the input arguments.

Pass true for ignoreFirst if the first argument is the program name (normally the case).

If the parser is configured to detect the switch style, the first parsed program argument will be examined. If it starts with a leading '-', the switch style used will be switch-space-value, otherwise the switch style used will be switch-equals-value

◆ parse() [2/3]

void parse ( int  argc,
const char *  argv[],
bool  ignoreFirst 
)
inline

Parse the input arguments.

Pass true for ignoreFirst if the first argument is the program name (normally the case).

If the parser is configured to detect the switch style, the first parsed program argument will be examined. If it starts with a leading '-', the switch style used will be switch-space-value, otherwise the switch style used will be switch-equals-value

◆ parse() [3/3]

void parse ( const std::vector< std::string > &  args,
bool  ignoreFirst 
)
inline

Parse the input arguments, which are supplied as a vector.

Pass true for ignoreFirst if the first argument is the program name.

If the parser is configured to detect the switch style, the first parsed program argument will be examined. If it starts with a leading '-', the switch style used will be switch-space-value, otherwise the switch style used will be switch-equals-value

Exceptions
MissingFinalValueExceptionif a final value is required and none was supplied
OptionNotFoundExceptionif an option was not found
MissingOptionValueExceptionif an option with a required value was specified and no value was provided

◆ withFinalValue()

CommandLine& withFinalValue ( )
inline

Specify that the command line arguments include one (SEV style) or one or more (SSV style) final stand alone values.

◆ withHelpOption()

CommandLine& withHelpOption ( KeyT  helpKey,
const std::string &  shortSwitch,
const std::string &  longSwitch,
const std::string &  helpDocumentation 
)
inline

Specify the help command line short and long switches.

Do not specify a leading "-" or "--" for the short and long switches, as the parser will detect these itself according to the switch style specified in the constructor.

◆ withOption() [1/2]

CommandLine& withOption ( KeyT  key,
const std::string &  shortSwitch,
const std::string &  longSwitch,
bool  hasValue,
const std::string &  documentation 
)
inline

Specify an option that has a short style switch and a long style switch.

Also specify whether the option has a value.

Do not specify a leading "-" or "--" for the short and long switches, as the parser will detect these itself according to the switch style specified in the constructor.

◆ withOption() [2/2]

CommandLine& withOption ( KeyT  key,
const std::string &  shortSwitch,
bool  hasValue,
const std::string &  documentation 
)
inline

Specify an option that has a short style switch.

Also specify whether the option has a value.

Do not specify a leading "-" for the short switch, as the parser will detect these itself according to the switch style specified in the constructor.

◆ withOptionalFinalValue()

CommandLine& withOptionalFinalValue ( )
inline

Specify that the command line arguments include one (SEV style) or one or more (SSV style) final stand alone values.


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