BLOCXX_NAMESPACE::CmdLineParser Class Reference

Do command line parsing. More...

#include <CmdLineParser.hpp>

List of all members.

Public Types

enum  EArgumentTypeFlag { E_NO_ARG, E_REQUIRED_ARG, E_OPTIONAL_ARG }
 Flags that can be used to specify how a command line option is processing by the CmdLineParser class. More...
enum  EErrorCodes { E_INVALID_OPTION, E_MISSING_ARGUMENT, E_INVALID_NON_OPTION_ARG, E_MISSING_OPTION }
 Error codes that may be found in a CmdLineParserException. More...
enum  EAllowNonOptionArgsFlag { E_NON_OPTION_ARGS_ALLOWED, E_NON_OPTION_ARGS_INVALID }
 Flags used to control whether or not non option arguments are allowed on the command line. More...

Public Member Functions

 CmdLineParser (int argc, char const *const *const argv, const Option *options, EAllowNonOptionArgsFlag allowNonOptionArgs)
 Construct a new CmdLineParser object.
String getOptionValue (int id, const char *defaultValue="") const
 Retrieve a value that corresponds to a string option id.
String mustGetOptionValue (int id, const char *exceptionMessage="") const
 Retrieve a value that corresponds to an string option id.
StringArray getOptionValueList (int id) const
 Retrieve the values from all occurences of a string option.
StringArray mustGetOptionValueList (int id, const char *exceptionMessage="") const
 Retrieve all occurences of a string option.
bool isSet (int id) const
 Retrieve the value of a boolean option or check for the presence of string option.
size_t getNonOptionCount () const
String getNonOptionArg (size_t n) const
 Retrieve a non-option argument.
StringArray getNonOptionArgs () const
 Retrieve all non-option arguments.

Static Public Member Functions

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

Private Types

typedef SortedVectorMap< int,
StringArray
optionsMap_t

Private Attributes

optionsMap_t m_parsedOptions
StringArray m_nonOptionArgs

Classes

struct  Option
 The Option structure is used to hold information about a single command line option. More...


Detailed Description

Do command line parsing.

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

Definition at line 57 of file CmdLineParser.hpp.


Member Typedef Documentation

typedef SortedVectorMap<int, StringArray> BLOCXX_NAMESPACE::CmdLineParser::optionsMap_t [private]
 

Definition at line 230 of file CmdLineParser.hpp.


Member Enumeration Documentation

enum BLOCXX_NAMESPACE::CmdLineParser::EAllowNonOptionArgsFlag
 

Flags used to control whether or not non option arguments are allowed on the command line.

Enumerator:
E_NON_OPTION_ARGS_ALLOWED  Non-option arguments are allowed.
E_NON_OPTION_ARGS_INVALID  Non-option arguments are NOT allowed.

Definition at line 119 of file CmdLineParser.hpp.

enum BLOCXX_NAMESPACE::CmdLineParser::EArgumentTypeFlag
 

Flags that can be used to specify how a command line option is processing by the CmdLineParser class.

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 65 of file CmdLineParser.hpp.

enum BLOCXX_NAMESPACE::CmdLineParser::EErrorCodes
 

Error codes that may be found in a CmdLineParserException.

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 78 of file CmdLineParser.hpp.


Constructor & Destructor Documentation

BLOCXX_NAMESPACE::CmdLineParser::CmdLineParser int  argc,
char const *const *const   argv,
const Option options,
EAllowNonOptionArgsFlag  allowNonOptionArgs
 

Construct a new CmdLineParser object.

Parameters:
argc Count of pointers in argv. Pass value from main().
argv Arguments. Pass value from main(). Value is not saved.
options An array of Option objects terminated by a final entry that has a '\0' shortopt && 0 longopt. Value is not saved.
allowNonOptionArgs Indicate whether the presense of non-option arguments is an error.
Exceptions:
CmdLineParserException if the given command line is invalid.

Definition at line 87 of file CmdLineParser.cpp.

References BLOCXX_ASSERT, BLOCXX_THROW_ERR, BLOCXX_NAMESPACE::String::c_str(), E_INVALID_OPTION, E_MISSING_ARGUMENT, E_NO_ARG, E_OPTIONAL_ARG, E_REQUIRED_ARG, BLOCXX_NAMESPACE::String::length(), BLOCXX_NAMESPACE::CmdLineParser::Option::longopt, m_parsedOptions, BLOCXX_NAMESPACE::CmdLineParser::Option::shortopt, BLOCXX_NAMESPACE::String::substring(), and val.


Member Function Documentation

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

Retrieve a non-option argument.

Parameters:
n The 0-based index of the argument. Valid values are 0 to count()-1.
Returns:
The value of the non-option argument.

Definition at line 369 of file CmdLineParser.cpp.

References m_nonOptionArgs.

StringArray BLOCXX_NAMESPACE::CmdLineParser::getNonOptionArgs  )  const
 

Retrieve all non-option arguments.

Returns:
All non-option arguments as a String array.

Definition at line 376 of file CmdLineParser.cpp.

References m_nonOptionArgs.

size_t BLOCXX_NAMESPACE::CmdLineParser::getNonOptionCount  )  const
 

Returns:
The number of arguments that aren't options (e.g. filenames).

Definition at line 362 of file CmdLineParser.cpp.

References m_nonOptionArgs, and BLOCXX_NAMESPACE::Array< T >::size().

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

Retrieve a value that corresponds to a string option id.

Parameters:
id The id of the option to retrieve the value for.
defaultValue The 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 304 of file CmdLineParser.cpp.

References BLOCXX_NAMESPACE::SortedVectorMap< Key, T, Compare >::end(), BLOCXX_NAMESPACE::SortedVectorMap< Key, T, Compare >::find(), and m_parsedOptions.

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

Retrieve the values from all occurences of a string option.

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

Definition at line 330 of file CmdLineParser.cpp.

References BLOCXX_NAMESPACE::SortedVectorMap< Key, T, Compare >::end(), BLOCXX_NAMESPACE::SortedVectorMap< Key, T, Compare >::find(), and m_parsedOptions.

String BLOCXX_NAMESPACE::CmdLineParser::getUsage const Option options,
unsigned int  maxColumns = 80
[static]
 

Generate a usage string for the options.

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

The format [arg] is used to represent E_OPTIONAL_ARG options. The format <arg> is used to represent E_REQUIRED_ARG options.

Parameters:
options An array of Option objects terminated by a final entry that has a '\0' shortopt && 0 longopt.
maxColumns Wrap the descriptions so no line of the usage string exceeds the specified number of columns.

Definition at line 196 of file CmdLineParser.cpp.

References BLOCXX_NAMESPACE::CmdLineParser::Option::shortopt.

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

Retrieve the value of a boolean option or check for the presence of string option.

Parameters:
id The id of the option.
Returns:
If id refers to a boolean option, the options value will be returned. If id refers to a string option, true is returned if it was specified, and false is returned if it was not specified.

Definition at line 355 of file CmdLineParser.cpp.

References BLOCXX_NAMESPACE::SortedVectorMap< Key, T, Compare >::count(), and m_parsedOptions.

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

Retrieve a value that corresponds to an string option id.

Throw and exception if the option was not specified.

Parameters:
id The id of the option to retrieve the value for.
exceptionMessage If an exception is thrown this string will be used as the exception message.
Returns:
The value of the option. If the option was specified more than once, the value from the last occurence will be returned.
Exceptions:
CmdLineParserException with code E_MISSING_OPTION if the option wasn't specified.

Definition at line 317 of file CmdLineParser.cpp.

References BLOCXX_NAMESPACE::SortedVectorMap< Key, T, Compare >::end(), BLOCXX_NAMESPACE::SortedVectorMap< Key, T, Compare >::find(), and m_parsedOptions.

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

Retrieve all occurences of a string option.

Parameters:
id The id of the option.
exceptionMessage If an exception is thrown this string will be used as the exception message.
Returns:
The values of the option as a string array.
Exceptions:
CmdLineParserException with code E_MISSING_OPTION if the option wasn't specified.

Definition at line 343 of file CmdLineParser.cpp.

References BLOCXX_NAMESPACE::SortedVectorMap< Key, T, Compare >::end(), BLOCXX_NAMESPACE::SortedVectorMap< Key, T, Compare >::find(), and m_parsedOptions.


Member Data Documentation

StringArray BLOCXX_NAMESPACE::CmdLineParser::m_nonOptionArgs [private]
 

Definition at line 232 of file CmdLineParser.hpp.

Referenced by getNonOptionArg(), getNonOptionArgs(), and getNonOptionCount().

optionsMap_t BLOCXX_NAMESPACE::CmdLineParser::m_parsedOptions [private]
 

Definition at line 231 of file CmdLineParser.hpp.

Referenced by CmdLineParser(), getOptionValue(), getOptionValueList(), isSet(), mustGetOptionValue(), and mustGetOptionValueList().


The documentation for this class was generated from the following files:
Generated on Fri Jun 16 15:39:10 2006 for blocxx by  doxygen 1.4.6