38 #include "blocxx/BLOCXX_config.h"
48 namespace BLOCXX_NAMESPACE
75 shortOptIs(
char shortOpt) :
m_shortOpt(shortOpt) {}
77 bool operator()(
const CmdLineParser::Option& x)
const
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];
String mustGetOptionValue(int id, const char *exceptionMessage="") const
Read out a string option.
Non-option arguments are invalid.
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability...
char shortopt
short option char. Set to '\0' for none.
const char * longopt
long option string. Set to 0 for none.
int id
unique option id, used to retrieve option values
String substring(size_t beginIndex, size_t length=npos) const
Create another String object that is comprised of a substring of this String object.
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(), it throws an AssertionException.
#define BLOCXX_THROW_ERR(exType, msg, err)
Throw an exception using FILE and LINE.
optionsMap_t m_parsedOptions
This String class is an abstract data type that represents as NULL terminated string of characters...
CmdLineParser(int argc, char const *const *const argv, const Option *options, EAllowNonOptionArgsFlag allowNonOptionArgs)
an option for which argtype == E_REQUIRED_ARG did not have an argument
#define BLOCXX_DEFINE_EXCEPTION_WITH_ID(NAME)
Define a new exception class named <NAME>Exception that derives from Exception.
const_iterator end() const
static String getUsage(const Option *options, unsigned int maxColumns=80)
Generate a usage string for the options, for example:
bool isSet(int id) const
Read out a boolean option or check for the presence of string option.
a non-option argument was specified, but they are not allowed
StringArray m_nonOptionArgs
the option might have an argument
StringArray getOptionValueList(int id) const
Read out all occurences of a string option.
size_type count(const key_type &x) const
const char * c_str() const
StringArray getNonOptionArgs() const
Read out the non-option args.
String getNonOptionArg(size_t n) const
Read out an non-option argument.
size_t indexOf(char ch, size_t fromIndex=0) const
Find the first occurence of a given character in this String object.
container_t::const_iterator const_iterator
void push_back(const T &x)
Append an element to the end of the Array.
an unknown option was specified
StringArray mustGetOptionValueList(int id, const char *exceptionMessage="") const
Read out all occurences of a string option.
size_t getNonOptionCount() const
Read the number of arguments that aren't options (but, for example, filenames).
the option wasn't specified
the option does not take an argument
String getOptionValue(int id, const char *defaultValue="") const
Read out a string option.
const char * defaultValue
if argtype == E_OPTIONAL_ARG and no argument is specified, this value will be returned. Set to 0 for none.
EArgumentTypeFlag argtype
specifies constraints for the option's argument
const_iterator find(const key_type &x) const
the option requires an argument