本文整理汇总了C++中Option::hasArgument方法的典型用法代码示例。如果您正苦于以下问题:C++ Option::hasArgument方法的具体用法?C++ Option::hasArgument怎么用?C++ Option::hasArgument使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Option
的用法示例。
在下文中一共展示了Option::hasArgument方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: usage
void OptionsList::usage()
{
size_t len;
char buffer[1024], *cp;
cp = buffer;
len = sprintf(buffer, "Usage: %s ", program_);
cp += len;
bool hasSimpleFlags = false;
for (list<Option>::iterator iter = begin(); iter != end(); iter++)
{
Option *opt = &(*iter);
if (opt->getName() != NULL &&
!opt->hasArgument() &&
(len = strlen(opt->getName())) == 1)
{
hasSimpleFlags = true;
break;
}
}
if (hasSimpleFlags)
{
*cp++ = '[';
*cp++ = '-';
for (list<Option>::iterator iter = begin(); iter != end(); iter++)
{
Option *opt = &(*iter);
if (opt->getName() != NULL &&
!opt->hasArgument() &&
(len = strlen(opt->getName())) == 1)
{
strcpy(cp, opt->getName());
cp += len;
}
}
*cp++ = ']';
}
char staging[128], *cp2;
for (list<Option>::iterator iter = begin(); iter != end(); iter++)
{
Option *opt = &(*iter);
if (opt->getName() != NULL && !opt->hasArgument() && (len = strlen(opt->getName())) == 1)
continue;
*cp++ = ' ';
cp2 = staging;
if (!opt->isRequired())
{
*cp2++ = '[';
}
if (opt->getType() == Option::eList)
{
*cp2++ = '{';
}
if (opt->getName() != NULL && !opt->hasArgument() && strlen(opt->getName()) > 1)
{
len = sprintf(cp2, "-%s", opt->getName());
cp2 += len;
}
else if (opt->getName() != NULL && opt->hasArgument())
{
len = sprintf(cp2, "-%s <%s>", opt->getName(), opt->getArgDesc());
cp2 += len;
}
else if (opt->getName() == NULL)
{
len = sprintf(cp2, "<%s>", opt->getArgDesc());
cp2 += len;
}
if (opt->getType() == Option::eList)
{
*cp2++ = '}';
*cp2++ = '.';
*cp2++ = '.';
*cp2++ = '.';
}
if (!opt->isRequired())
{
*cp2++ = ']';
}
*cp2 = '\0';
if (((cp2 - staging) + (cp - buffer)) > 79)
{
*cp++ = '\n';
*cp = '\0';
//.........这里部分代码省略.........
示例2: parse
int OptionsList::parse(int &aArgc, const char **aArgv)
{
sort();
// This assumes that the first option (app name) has already been removed.
int order = 0, count = 0;
program_ = "agent";
Option *opt;
const char **argp = aArgv;
const char *cp;
while (aArgc > 0)
{
if (**argp == '-')
{
cp = (*argp) + 1;
bool next = false;
while (*cp != 0 && !next)
{
if (find(cp, opt))
{
count++;
if (opt->hasArgument())
{
getArg(argp, aArgc, opt, cp);
next = true;
}
else
{
if (opt->type_ != Option::eBoolean)
{
cerr << "Bad argument definition: " << opt->getName() << endl;
}
else if (opt->isSet_)
{
cerr << "Option " << opt->getName() << " is already specified" << endl;
usage();
}
else
{
*(opt->boolPtr_) = true;
}
cp += strlen(opt->getName());
}
opt->isSet_ = true;
}
else
{
cerr << "Bad argument:" << *argp << endl;
usage();
}
}
}
else
{
if (find(order, opt))
{
if (!opt->setValue(*argp))
usage();
count++;
}
else if (find(-1, opt))
{
if (!opt->setValue(*argp))
usage();
count++;
}
order++;
}
argp++;
aArgc--;
}
for (list<Option>::iterator iter = begin(); iter != end(); iter++)
{
opt = &(*iter);
if (opt->isRequired() && !opt->isSet())
{
if (opt->getName() != NULL)
cerr << "Required option -" << opt->getName() << " is not specified" << endl;
else
cerr << "Required option <" << opt->getArgDesc() << "> is not specified" << endl;
usage();
}
}
return count;
}