53 #if ( _MSC_VER < 1600 )
54 #define min(VAL1, VAL2) _cpp_min(VAL1, VAL2)
55 #define max(VAL1, VAL2) _cpp_max(VAL1, VAL2)
62 CommandOptionParser::CommandOptionParser(
63 const std::string& description,
65 : hasRequiredArguments(false),
66 hasOptionalArguments(false),
70 CommandOptionVec::const_iterator optIter = optList.begin();
71 for ( ; optIter != optList.end() ; ++optIter)
82 CommandOptionVec::const_iterator optIter =
optionVec.begin();
83 for ( ; optIter !=
optionVec.end() ; ++optIter)
86 && ((*optIter)->shortOpt == co.
shortOpt) )
88 std::string msg(
"Short option already in use: ");
90 gnsstk::InvalidParameter exc(msg);
94 && ((*optIter)->longOpt.compare(co.
longOpt) == 0) )
96 std::string msg(
"Long option already in use: ");
98 gnsstk::InvalidParameter exc(msg);
104 gnsstk::InvalidParameter exc(
"Multiple trailing options are disallowed");
122 unsigned int order = 0;
127 string::size_type slashPos =
progName.rfind(
'/');
128 if (slashPos != string::npos)
133 string::size_type p =
progName.find(
".exe");
134 if (p != string::npos)
140 string shortOptString;
142 unsigned long optArraySize = 0;
147 CommandOptionVec::size_type index;
148 for(index = 0; index <
optionVec.size(); index++)
157 errorStrings.push_back(
"More than one trailing argument"
158 " object used (programming error");
165 shortOptString +=
optionVec[index]->toGetoptShortOption();
173 optArray[optArraySize - 1] =
193 struct option lastOption = {0,0,0,0};
194 optArray[optArraySize - 1] = lastOption;
197 shortOptString.insert((string::size_type)0, (string::size_type)1,
'+');
210 if ((cha =
getopt_long(argc, argv, shortOptString.c_str(),
211 optArray, &optionIndex)) == -1)
225 if ((cha ==
'?') || (cha ==
':'))
232 errorArg = string(1, (
char)
optopt);
235 errorArg = argv[
optind - 1];
236 errorStrings.push_back(
string(
"Option error: " + errorArg));
245 thisOption = string(1,(
char)cha);
247 thisOption = string(optArray[optionIndex].
name);
250 map<string, CommandOption*>::iterator itr = com.find(thisOption);
252 if (itr != com.end())
262 string(
" has an argument but it shouldn't."));
269 pickedOption->
count++;
270 pickedOption->
order.push_back(order);
281 string(
" has no argument when it should."));
286 pickedOption->
count++;
287 pickedOption->
order.push_back(order);
311 for(i =
optind; i < argc; i++)
315 trailing->
value.push_back(
string(argv[i]));
317 trailing->
order.push_back(order);
323 for(index = 0; index <
optionVec.size(); index++)
325 string retVal =
optionVec[index]->checkArguments();
334 string errstr(
"Option ");
335 errstr +=
optionVec[index]->getOptionString();
336 errstr += string(
" appeared more times than allowed.");
348 vector<string>::size_type index;
355 bool doPretty,
bool firstOnly)
357 std::vector<CommandOptionHelp*>::size_type index;
366 for(index = 0; index <
helpOptions.size(); index++)
377 CommandOptionVec::size_type index;
380 char *colch =
getenv(
"COLUMNS");
385 string colStr(colch);
386 columns =
asInt(colStr);
390 for (index = 0; index <
optionVec.size(); index++)
396 unsigned(
optionVec[index]->getFullOptionString().length()));
401 out <<
" [OPTION] ...";
410 for(
int required = 1; required >= 0; required--)
413 out << endl <<
"Required arguments:" << endl;
415 out << endl <<
"Optional arguments:" << endl;
417 for(index = 0; index <
optionVec.size(); index++)
419 if ((
optionVec[index]->required == (required==1)) &&
422 string optstr(
optionVec[index]->getFullOptionString());
423 string desc(
optionVec[index]->description);
424 string indent(maxlen,
' ');
431 if(!doPretty) out << endl;
442 CommandOptionVec::size_type index;
451 for (index = 0; index <
optionVec.size(); index++)
459 out <<
" * <b>" <<
progName <<
"</b> ";
462 out <<
" <b>-" <<
optionVec[index]->shortOpt;
464 else if (!
optionVec[index]->longOpt.empty())
465 out <<
" <b>\\--" <<
optionVec[index]->longOpt;
469 out <<
" \\argarg{" <<
optionVec[index]->getArgString()
472 out <<
" <br/>" << endl;
475 out <<
" * <b>" <<
progName <<
"</b>";
476 for (
int required = 1; required >= 0; required--)
478 for (index = 0; index <
optionVec.size(); index++)
482 if ((
optionVec[index]->required == (required==1)) &&
490 out <<
"-" <<
optionVec[index]->shortOpt;
492 else if (!
optionVec[index]->longOpt.empty())
493 out <<
"\\--" <<
optionVec[index]->longOpt;
497 out <<
" \\argarg{"
498 <<
optionVec[index]->getArgString() <<
"}";
516 out <<
" <b>[</b>...<b>]</b>";
520 out << endl << endl <<
" * \\dictionary" << endl;
522 for (
int required = 1; required >= 0; required--)
524 for(index = 0; index <
optionVec.size(); index++)
526 if ((
optionVec[index]->required == (required==1)) &&
529 out <<
" * \\dicterm{";
532 out <<
"-" <<
optionVec[index]->shortOpt;
537 out <<
"\\--" <<
optionVec[index]->longOpt;
540 out <<
"=\\argarg{" <<
optionVec[index]->getArgString()
549 out <<
" * \\enddictionary" << endl;
557 unsigned long& oldSize)
559 struct option* newArray =
new struct option[1 + oldSize];
560 std::memcpy(newArray, oldArray, oldSize *
sizeof(
struct option));