38 #include "blocxx/BLOCXX_config.h"
58 longOptIs(
const String& longOpt) :
m_longOpt(longOpt) {}
60 bool operator()(
const CmdLineParser::Option& x)
const
75 shortOptIs(
char shortOpt) :
m_shortOpt(shortOpt) {}
77 bool operator()(
const CmdLineParser::Option& x)
const
88 CmdLineParser::CmdLineParser(
int argc,
char const*
const*
const argv_,
const Option* options, EAllowNonOptionArgsFlag allowNonOptionArgs)
93 char const*
const* argv = argv_;
94 char const*
const* argvEnd = argv + argc;
97 const Option* optionsEnd(options);
98 while (optionsEnd->shortopt !=
'\0' || optionsEnd->longopt != 0)
105 while (argv != argvEnd)
111 if ((arg.length() >= 2) && (arg[0] ==
'-'))
113 const Option* theOpt(0);
114 bool longOpt =
false;
119 arg = arg.substring(2);
121 size_t idx = arg.indexOf(
'=');
131 theOpt = std::find_if (options, optionsEnd, longOptIs(argNoVal));
136 arg = arg.substring(1);
137 theOpt = std::find_if (options, optionsEnd, shortOptIs(arg[0]));
140 if (theOpt == optionsEnd)
153 if ((theOpt->argtype ==
E_OPTIONAL_ARG) && (theOpt->defaultValue != 0))
155 val = theOpt->defaultValue;
158 const char* p = ::strchr(arg.c_str(),
'=');
167 if (longOpt ==
false && arg.length() > 1)
169 val = arg.substring(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;
218 StringBuffer usage(
"Options:\n");
221 for (
const Option* curOption = options; curOption->shortopt !=
'\0' || curOption->longopt != 0; ++curOption)
223 StringBuffer curLine;
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;
268 while (curLine.length() > maxColumns || curLine.toString().indexOf(
'\n') !=
String::npos)
270 String curLineStr(curLine.toString());
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)
306 curLine = spaces.releaseString() + curLineStr.substring(nextLineBeginIdx);
312 return usage.releaseString();
323 return ci->second[ci->second.size()-1];
336 return ci->second[ci->second.size()-1];