blocxx
BLOCXX_NAMESPACE::CmdLineParser Class Reference

Do command line parsing. More...

#include <CmdLineParser.hpp>

Classes

struct  Option
 

Public Types

enum  EArgumentTypeFlag { E_NO_ARG , E_REQUIRED_ARG , E_OPTIONAL_ARG }
 
enum  EErrorCodes { E_INVALID_OPTION , E_MISSING_ARGUMENT , E_INVALID_NON_OPTION_ARG , E_MISSING_OPTION }
 
enum  EAllowNonOptionArgsFlag { E_NON_OPTION_ARGS_ALLOWED , E_NON_OPTION_ARGS_INVALID }
 

Public Member Functions

 CmdLineParser (int argc, char const *const *const argv, const Option *options, EAllowNonOptionArgsFlag allowNonOptionArgs)
 
String getOptionValue (int id, const char *defaultValue="") const
 Read out a string option.
 
String mustGetOptionValue (int id, const char *exceptionMessage="") const
 Read out a string option.
 
StringArray getOptionValueList (int id) const
 Read out all occurences of a string option.
 
StringArray mustGetOptionValueList (int id, const char *exceptionMessage="") const
 Read out all occurences of a string option.
 
bool isSet (int id) const
 Read out a boolean option or check for the presence of string option.
 
size_t getNonOptionCount () const
 Read the number of arguments that aren't options (but, for example, filenames).
 
String getNonOptionArg (size_t n) const
 Read out an non-option argument.
 
StringArray getNonOptionArgs () const
 Read out the non-option args.
 

Static Public Member Functions

static String getUsage (const Option *options, unsigned int maxColumns=80)
 Generate a usage string for the options, for example:
 

Private Types

typedef SortedVectorMap< int, StringArrayoptionsMap_t
 

Private Attributes

optionsMap_t m_parsedOptions
 
StringArray m_nonOptionArgs
 

Detailed Description

Do command line parsing.

Thread safety: read/write Copy semantics: Value Exception safety: Strong

Definition at line 58 of file CmdLineParser.hpp.

Member Typedef Documentation

◆ optionsMap_t

Member Enumeration Documentation

◆ EAllowNonOptionArgsFlag

Enumerator
E_NON_OPTION_ARGS_ALLOWED 

Non-option arguments are allowed.

E_NON_OPTION_ARGS_INVALID 

Non-option arguments are invalid.

Definition at line 87 of file CmdLineParser.hpp.

◆ EArgumentTypeFlag

Enumerator
E_NO_ARG 

the option does not take an argument

E_REQUIRED_ARG 

the option requires an argument

E_OPTIONAL_ARG 

the option might have an argument

Definition at line 61 of file CmdLineParser.hpp.

◆ EErrorCodes

Enumerator
E_INVALID_OPTION 

an unknown option was specified

E_MISSING_ARGUMENT 

an option for which argtype == E_REQUIRED_ARG did not have an argument

E_INVALID_NON_OPTION_ARG 

a non-option argument was specified, but they are not allowed

E_MISSING_OPTION 

the option wasn't specified

Definition at line 69 of file CmdLineParser.hpp.

Constructor & Destructor Documentation

◆ CmdLineParser()

BLOCXX_NAMESPACE::CmdLineParser::CmdLineParser ( int argc,
char const *const *const argv,
const Option * options,
EAllowNonOptionArgsFlag allowNonOptionArgs )
Parameters
argcCount of pointers in argv. Pass value from main().
argvArguments. Pass value from main(). Value is not saved.
optionsAn array of Option terminated by a final entry that has a '\0' shortopt && 0 longopt. Value is not saved.
allowNonOptionArgsIndicate whether the presense of non-option arguments is an error.
Exceptions
CmdLineParserExceptionif the given command line is invalid.

Definition at line 88 of file CmdLineParser.cpp.

References BLOCXX_NAMESPACE::CmdLineParser::Option::argtype, BLOCXX_ASSERT, BLOCXX_THROW_ERR, BLOCXX_NAMESPACE::String::c_str(), BLOCXX_NAMESPACE::CmdLineParser::Option::defaultValue, E_INVALID_NON_OPTION_ARG, E_INVALID_OPTION, E_MISSING_ARGUMENT, E_NO_ARG, E_NON_OPTION_ARGS_INVALID, E_OPTIONAL_ARG, E_REQUIRED_ARG, BLOCXX_NAMESPACE::String::empty(), BLOCXX_NAMESPACE::CmdLineParser::Option::id, BLOCXX_NAMESPACE::String::indexOf(), BLOCXX_NAMESPACE::String::length(), BLOCXX_NAMESPACE::CmdLineParser::Option::longopt, m_nonOptionArgs, m_parsedOptions, BLOCXX_NAMESPACE::String::npos, BLOCXX_NAMESPACE::CmdLineParser::Option::shortopt, and BLOCXX_NAMESPACE::String::substring().

Member Function Documentation

◆ getNonOptionArg()

String BLOCXX_NAMESPACE::CmdLineParser::getNonOptionArg ( size_t n) const

Read out an non-option argument.

Parameters
nThe 0-based index of the argument. Valid values are 0 to count()-1.

Definition at line 382 of file CmdLineParser.cpp.

References m_nonOptionArgs.

◆ getNonOptionArgs()

StringArray BLOCXX_NAMESPACE::CmdLineParser::getNonOptionArgs ( ) const

Read out the non-option args.

Definition at line 389 of file CmdLineParser.cpp.

References m_nonOptionArgs.

◆ getNonOptionCount()

size_t BLOCXX_NAMESPACE::CmdLineParser::getNonOptionCount ( ) const

Read the number of arguments that aren't options (but, for example, filenames).

Definition at line 375 of file CmdLineParser.cpp.

References m_nonOptionArgs.

◆ getOptionValue()

String BLOCXX_NAMESPACE::CmdLineParser::getOptionValue ( int id,
const char * defaultValue = "" ) const

Read out a string option.

Parameters
idThe id of the option.
defaultValueThe return value if the option wasn't set.
Returns
The value of the option, if given, otherwise defaultValue. If the option was specified more than once, the value from the last occurence will be returned.

Definition at line 317 of file CmdLineParser.cpp.

References m_parsedOptions.

◆ getOptionValueList()

StringArray BLOCXX_NAMESPACE::CmdLineParser::getOptionValueList ( int id) const

Read out all occurences of a string option.

Parameters
idThe id of the option.
Returns
The value of the option, if given, otherwise an empty StringArray.

Definition at line 343 of file CmdLineParser.cpp.

References m_parsedOptions.

◆ getUsage()

String BLOCXX_NAMESPACE::CmdLineParser::getUsage ( const Option * options,
unsigned int maxColumns = 80 )
static

Generate a usage string for the options, for example:

"Options:\n"
" -1, --one first description\n"
" -2, --two [arg] second description (default is optional)\n"
" -3, --three <arg> third description\n"

The E_OPTIONAL_ARG option arguments are indicated by squared brackets

"[arg]"

and E_REQUIRED_ARG option arguments by angle brackets

"<arg>"

.

Parameters
optionsAn array of Option terminated by a final entry that has a '\0' shortopt && 0 longopt.
maxColumnsWrap the descriptions so no line of the usage string exceeds the specified number of columns.

Definition at line 209 of file CmdLineParser.cpp.

References E_OPTIONAL_ARG, E_REQUIRED_ARG, BLOCXX_NAMESPACE::String::indexOf(), BLOCXX_NAMESPACE::String::lastIndexOf(), BLOCXX_NAMESPACE::StringBuffer::length(), BLOCXX_NAMESPACE::String::npos, BLOCXX_NAMESPACE::StringBuffer::releaseString(), BLOCXX_NAMESPACE::CmdLineParser::Option::shortopt, BLOCXX_NAMESPACE::String::substring(), and BLOCXX_NAMESPACE::StringBuffer::toString().

◆ isSet()

bool BLOCXX_NAMESPACE::CmdLineParser::isSet ( int id) const

Read out a boolean option or check for the presence of string option.

Definition at line 368 of file CmdLineParser.cpp.

References m_parsedOptions.

◆ mustGetOptionValue()

String BLOCXX_NAMESPACE::CmdLineParser::mustGetOptionValue ( int id,
const char * exceptionMessage = "" ) const

Read out a string option.

Parameters
idThe id of the option.
Returns
The value of the option. If the option was specified more than once, the value from the last occurence will be returned.
Parameters
exceptionMessageIf an exception is thrown this string will be used as the exception message.
Exceptions
CmdLineParserExceptionwith code E_MISSING_OPTION if the option wasn't specified.

Definition at line 330 of file CmdLineParser.cpp.

References BLOCXX_THROW_ERR, E_MISSING_OPTION, and m_parsedOptions.

◆ mustGetOptionValueList()

StringArray BLOCXX_NAMESPACE::CmdLineParser::mustGetOptionValueList ( int id,
const char * exceptionMessage = "" ) const

Read out all occurences of a string option.

Parameters
idThe id of the option.
Returns
The value of the option.
Parameters
exceptionMessageIf an exception is thrown this string will be used as the exception message.
Exceptions
CmdLineParserExceptionwith code E_MISSING_OPTION if the option wasn't specified.

Definition at line 356 of file CmdLineParser.cpp.

References BLOCXX_THROW_ERR, E_MISSING_OPTION, and m_parsedOptions.

Member Data Documentation

◆ m_nonOptionArgs

StringArray BLOCXX_NAMESPACE::CmdLineParser::m_nonOptionArgs
private

◆ m_parsedOptions

optionsMap_t BLOCXX_NAMESPACE::CmdLineParser::m_parsedOptions
private

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