19 : mNames(), mDesc(), mDescExtra(), mValues(), mDefaultValue()
23 : mNames(), mDesc(), mDescExtra(), mValues(), mDefaultValue()
29 const std::string &desc)
30 : mNames(), mDesc(desc), mDescExtra(), mValues(), mDefaultValue()
36 const std::string &desc,
37 const std::string &defaultValue)
38 : mNames(), mDesc(desc), mDescExtra(), mValues(), mDefaultValue(defaultValue)
44 : mNames(), mDesc(), mDescExtra(), mValues(), mDefaultValue()
46 for (
size_t i = 0; i < names.size(); i++) {
52 const std::string &desc)
53 : mNames(), mDesc(desc), mDescExtra(), mValues(), mDefaultValue()
55 for (
size_t i = 0; i < names.size(); i++) {
61 const std::string &desc,
62 const std::string &defaultValue)
63 : mNames(), mDesc(desc), mDescExtra(), mValues(), mDefaultValue()
65 for (
size_t i = 0; i < names.size(); i++) {
68 mDefaultValue = defaultValue;
77 bool haveName =
false;
78 for (
size_t i = 0; i < mNames.size(); i++) {
79 if (mNames.at(i) == name) {
85 mNames.push_back(name);
119 mDefaultValue = value;
124 return mDefaultValue;
129 mValues.push_back(value);
134 if (index > mValues.size() || mValues.empty())
136 return mValues.at(index);
145 : mFlags(flags), mName(), mCommandName(), mDesc(), mDescExtra(), mUsageText(), mHelpText(),
146 mOptions(), mKnownOptions(), mUnknownOptions(), mSubParsers()
151 : mFlags(flags), mName(name), mCommandName(), mDesc(), mDescExtra(), mUsageText(), mHelpText(),
152 mOptions(), mKnownOptions(), mUnknownOptions(), mSubParsers()
167 mFlags = other.mFlags;
169 mCommandName = other.mCommandName;
171 mDescExtra = other.mDescExtra;
172 mUsageText = other.mUsageText;
173 mHelpText = other.mHelpText;
174 mOptions = other.mOptions;
175 mKnownOptions.clear();
176 mKnownOptions = other.mKnownOptions;
177 mUnknownOptions.clear();
178 mUnknownOptions = other.mUnknownOptions;
180 for (
SubParserMapConstIter iter = other.mSubParsers.begin(); iter != other.mSubParsers.end(); iter++) {
187 mKnownOptions.clear();
188 mUnknownOptions.clear();
197 if (!mCommandName.empty()) {
204 iter != mOptions.end(); iter++) {
207 std::ostringstream oss;
214 if (++count < names.size())
217 oss <<
"] " <<
"set? " << (
IsSet(name) ?
"true" :
"false") <<
" value = " << o.
GetValue();
218 std::cout << oss.str() << std::endl;
226 iter != mOptions.end(); iter++) {
230 if (name == *nameIter) {
242 const std::string &name = p->
GetName();
243 if (mSubParsers.find(name) == mSubParsers.end()) {
255 if (
static_cast<int>(arg.length()) > prefixSize) {
257 for (
size_t c = arg.length();
static_cast<int>(c) > prefixSize; c--) {
258 subStr = arg.substr(prefixSize, c-prefixSize);
260 parseOptionValue(subStr, arg, iter, args.end());
261 mKnownOptions.push_back(subStr);
264 mUnknownOptions.push_back(subStr);
274 if (args.size() <= 1)
277 AJAStringList::const_iterator iter = args.begin();
280 for (SubParserMap::iterator spIter = mSubParsers.begin(); spIter != mSubParsers.end(); spIter++) {
282 if (*iter == spIter->first)
283 mCommandName = *iter;
285 if (spIter->second !=
NULL) {
286 spIter->second->ParseArgs(args);
293 if ((*iter != mName) && !mName.empty())
297 for (; iter != args.end(); iter++) {
298 const std::string &arg = *iter;
300 std::string optValue;
302 reverseOptionSearch(&iter, args, arg, 2, opt);
319 if (arg.length() > 1) {
322 for (
size_t c = 1; c < arg.length(); c++) {
323 subStr = arg.substr(c, 1);
325 mKnownOptions.push_back(subStr);
327 mUnknownOptions.push_back(subStr);
331 reverseOptionSearch(&iter, args, arg, 1, opt);
352 if (argc == 0 || argc == 1 || argv ==
NULL)
356 for (
int i = 0; i < argc; i++)
357 argList.push_back(std::string(argv[i]));
364 if (argc == 0 || argc == 1 || argv ==
NULL)
368 for (
int i = 0; i < argc; i++)
369 argList.push_back(std::string(argv[i]));
376 if (!mCommandName.empty()) {
379 return sp->
IsSet(name);
404 if (!values.empty()) {
417 if (!mCommandName.empty()) {
437 if (!mCommandName.empty()) {
459 if (*wantIter == *nameIter) {
471 mOptions.push_back(option);
478 uint32_t okCount = 0;
479 for (
size_t i = 0; i < options.size(); i++) {
483 return options.size() > 0 ? (okCount == (uint32_t)options.size() ?
true :
false) :
false;
492 helpOpt.
SetDesc(
"Print the help text");
494 std::ostringstream oss;
497 oss <<
"usage: " << exePath;
500 oss <<
" [OPTION...]" << std::endl;
503 size_t longestSize = 0;
505 it != mOptions.end(); it++) {
507 size_t namesLength = 0;
509 const std::string &name = *sIter;
510 namesLength += name.length();
512 if (name.length() == 1) {
519 namesLength += ((names.size()*2)-2);
520 if (namesLength > longestSize)
521 longestSize = namesLength;
528 it != mOptions.end(); it++) {
529 oss << std::setw(2) << std::right;
531 size_t nameCount = 0;
532 size_t namesLength = 0;
534 const std::string &name = *sIter;
535 namesLength += name.length();
536 if (name.length() == 1) {
543 if (++nameCount < names.size()) {
547 namesLength += ((names.size()*2)-2);
548 oss << std::setw((longestSize-namesLength) + it->GetDesc().length() + 8);
549 oss << it->GetDesc() << std::endl;
552 mHelpText = oss.str();
562 if (!mCommandName.empty()) {
574 if (!mCommandName.empty()) {
580 mUsageText = usageText;
586 if (!mCommandName.empty()) {
598 if (!mCommandName.empty()) {
604 mHelpText = helpText;
610 if (!mCommandName.empty()) {
622 if (!mCommandName.empty()) {
629 return mKnownOptions;
639 if (!mCommandName.empty()) {
646 return mUnknownOptions;
649 bool AJACommandLineParser::hasOptionPrefix(
const std::string &name)
654 bool AJACommandLineParser::parseOptionValue(
const std::string &name,
655 const std::string &arg,
656 AJAStringList::const_iterator *iter,
657 AJAStringList::const_iterator
end)
660 std::string optValue;
664 if (assignPos != std::string::npos) {
666 optValue = arg.substr(assignPos+1, arg.length()-assignPos);
669 optValue = arg.substr(prefixSize+name.length(), arg.length());
670 if (optValue.empty()) {
671 if (iter !=
NULL && *iter !=
end) {
672 AJAStringList::const_iterator tmp = *iter;
704 if (optValue.empty())
708 return setOptionValue(name, optValue);
711 bool AJACommandLineParser::setOptionValue(
const std::string &name,
const std::string &value)
713 if (!
value.empty()) {
714 for (
size_t i = 0; i < mOptions.size(); i++) {
719 mOptions[i].AddValue(value);
729 std::string AJACommandLineParser::removePrefix(
const std::string &name,
int &count)
732 prefixes.push_back(
"--");
733 prefixes.push_back(
"-");
735 const std::string &prefix = *iter;
737 size_t prefixSize = prefix.length();
738 count = (int)prefixSize;
739 return name.substr(prefixSize, name.length()-prefixSize);