38#include "blocxx/BLOCXX_config.h"
58 longOptIs(
const String& longOpt) : m_longOpt(longOpt) {}
75 shortOptIs(
char shortOpt) : m_shortOpt(shortOpt) {}
93 char const*
const* argv = argv_;
94 char const*
const* argvEnd = argv + argc;
97 const Option* optionsEnd(options);
105 while (argv != argvEnd)
111 if ((arg.
length() >= 2) && (arg[0] ==
'-'))
114 bool longOpt =
false;
131 theOpt = std::find_if (options, optionsEnd, longOptIs(argNoVal));
137 theOpt = std::find_if (options, optionsEnd, shortOptIs(arg[0]));
140 if (theOpt == optionsEnd)
158 const char* p = ::strchr(arg.
c_str(),
'=');
167 if (longOpt ==
false && arg.
length() > 1)
172 else if (argv+1 != argvEnd)
175 if ( **(argv+1) !=
'-' || (**(argv+1) ==
'-' &&
String(*(argv+1)).length() == 1) )
217 const unsigned int NUM_OPTION_COLUMNS = 28;
221 for (
const Option* curOption = options; curOption->
shortopt !=
'\0' || curOption->longopt != 0; ++curOption)
225 if (curOption->shortopt !=
'\0')
228 curLine += curOption->shortopt;
229 if (curOption->longopt != 0)
234 if (curOption->longopt != 0)
237 curLine += curOption->longopt;
249 size_t bufferlen = (curLine.
length() >= NUM_OPTION_COLUMNS-1) ? 1 : (NUM_OPTION_COLUMNS - curLine.
length());
250 for (
size_t i = 0; i < bufferlen; ++i)
255 if (curOption->description != 0)
257 curLine += curOption->description;
260 if (curOption->defaultValue != 0)
262 curLine +=
" (default is ";
263 curLine += curOption->defaultValue;
272 size_t newlineIdx = curLineStr.
indexOf(
'\n');
275 size_t lastSpaceIdx = curLineStr.
lastIndexOf(
' ', maxColumns);
278 size_t nextLineBeginIdx = 0;
279 if (newlineIdx <= maxColumns)
282 nextLineBeginIdx = newlineIdx + 1;
284 else if (lastSpaceIdx > NUM_OPTION_COLUMNS)
286 cutIdx = lastSpaceIdx;
287 nextLineBeginIdx = lastSpaceIdx + 1;
293 nextLineBeginIdx = maxColumns;
297 usage += curLineStr.
substring(0, cutIdx);
302 for (
size_t i = 0; i < NUM_OPTION_COLUMNS; ++i)
323 return ci->second[ci->second.size()-1];
336 return ci->second[ci->second.size()-1];
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(),...
#define BLOCXX_DEFINE_EXCEPTION_WITH_ID(NAME)
Define a new exception class named <NAME>Exception that derives from Exception.
#define BLOCXX_THROW_ERR(exType, msg, err)
Throw an exception using FILE and LINE.
StringArray getOptionValueList(int id) const
Read out all occurences of a string option.
String getOptionValue(int id, const char *defaultValue="") const
Read out a string option.
CmdLineParser(int argc, char const *const *const argv, const Option *options, EAllowNonOptionArgsFlag allowNonOptionArgs)
bool isSet(int id) const
Read out a boolean option or check for the presence of string option.
String getNonOptionArg(size_t n) const
Read out an non-option argument.
StringArray m_nonOptionArgs
StringArray mustGetOptionValueList(int id, const char *exceptionMessage="") const
Read out all occurences of a string option.
@ E_REQUIRED_ARG
the option requires an argument
@ E_NO_ARG
the option does not take an argument
@ E_OPTIONAL_ARG
the option might have an argument
size_t getNonOptionCount() const
Read the number of arguments that aren't options (but, for example, filenames).
@ E_INVALID_OPTION
an unknown option was specified
@ E_INVALID_NON_OPTION_ARG
a non-option argument was specified, but they are not allowed
@ E_MISSING_ARGUMENT
an option for which argtype == E_REQUIRED_ARG did not have an argument
@ E_MISSING_OPTION
the option wasn't specified
@ E_NON_OPTION_ARGS_INVALID
Non-option arguments are invalid.
String mustGetOptionValue(int id, const char *exceptionMessage="") const
Read out a string option.
optionsMap_t m_parsedOptions
static String getUsage(const Option *options, unsigned int maxColumns=80)
Generate a usage string for the options, for example:
StringArray getNonOptionArgs() const
Read out the non-option args.
container_t::const_iterator const_iterator
This String class is an abstract data type that represents as NULL terminated string of characters.
const char * c_str() const
String substring(size_t beginIndex, size_t length=npos) const
Create another String object that is comprised of a substring of this String object.
size_t indexOf(char ch, size_t fromIndex=0) const
Find the first occurence of a given character in this String object.
size_t lastIndexOf(char ch, size_t fromIndex=npos) const
Find the last occurence of a character in this String object.
Array< String > StringArray
char shortopt
short option char. Set to '\0' for none.
EArgumentTypeFlag argtype
specifies constraints for the option's argument
const char * longopt
long option string. Set to 0 for none.
const char * defaultValue
if argtype == E_OPTIONAL_ARG and no argument is specified, this value will be returned....
int id
unique option id, used to retrieve option values