38 #include "blocxx/BLOCXX_config.h" 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)
145 if (theOpt->
argtype == E_NO_ARG)
147 m_parsedOptions[theOpt->
id];
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) )
189 m_parsedOptions[theOpt->
id].push_back(val);
193 if (allowNonOptionArgs == E_NON_OPTION_ARGS_INVALID)
199 m_nonOptionArgs.push_back(arg);
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;
240 if (curOption->argtype == E_REQUIRED_ARG)
244 else if (curOption->argtype == E_OPTIONAL_ARG)
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)
320 if (ci != m_parsedOptions.end() && ci->second.size() > 0)
323 return ci->second[ci->second.size()-1];
333 if (ci != m_parsedOptions.end() && ci->second.size() > 0)
336 return ci->second[ci->second.size()-1];
347 if (ci != m_parsedOptions.end() && ci->second.size() > 0)
359 if (ci != m_parsedOptions.end() && ci->second.size() > 0)
370 return m_parsedOptions.count(
id) > 0;
377 return m_nonOptionArgs.size();
384 return m_nonOptionArgs[n];
391 return m_nonOptionArgs;
char shortopt
short option char. Set to '\0' for none.
const char * longopt
long option string. Set to 0 for none.
StringArray getNonOptionArgs() const
Read out the non-option args.
String substring(size_t beginIndex, size_t length=npos) const
Create another String object that is comprised of a substring of this String object.
int id
unique option id, used to retrieve option values
String mustGetOptionValue(int id, const char *exceptionMessage="") const
Read out a string option.
#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.
This String class is an abstract data type that represents as NULL terminated string of characters...
StringArray getOptionValueList(int id) const
Read out all occurences of a string option.
CmdLineParser(int argc, char const *const *const argv, const Option *options, EAllowNonOptionArgsFlag allowNonOptionArgs)
String getNonOptionArg(size_t n) const
Read out an non-option argument.
#define BLOCXX_DEFINE_EXCEPTION_WITH_ID(NAME)
Define a new exception class named <NAME>Exception that derives from Exception.
const char * c_str() 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.
String getOptionValue(int id, const char *defaultValue="") const
Read out a string option.
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
size_t getNonOptionCount() const
Read the number of arguments that aren't options (but, for example, filenames).
const_iterator find(const T &x, const_iterator first, const_iterator last) const
Find element x in the array range specified by the first and last iterators.
StringArray mustGetOptionValueList(int id, const char *exceptionMessage="") const
Read out all occurences of 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