本文整理汇总了C++中Options::GetLongOptions方法的典型用法代码示例。如果您正苦于以下问题:C++ Options::GetLongOptions方法的具体用法?C++ Options::GetLongOptions怎么用?C++ Options::GetLongOptions使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Options
的用法示例。
在下文中一共展示了Options::GetLongOptions方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: switch
void
Args::ParseAliasOptions
(
Options &options,
CommandReturnObject &result,
OptionArgVector *option_arg_vector
)
{
StreamString sstr;
int i;
struct option *long_options = options.GetLongOptions();
if (long_options == NULL)
{
result.AppendError ("invalid long options");
result.SetStatus (eReturnStatusFailed);
return;
}
for (i = 0; long_options[i].name != NULL; ++i)
{
if (long_options[i].flag == NULL)
{
sstr << (char) long_options[i].val;
switch (long_options[i].has_arg)
{
default:
case no_argument:
break;
case required_argument:
sstr << ":";
break;
case optional_argument:
sstr << "::";
break;
}
}
}
#ifdef __GLIBC__
optind = 0;
#else
optreset = 1;
optind = 1;
#endif
int val;
while (1)
{
int long_options_index = -1;
val = ::getopt_long (GetArgumentCount(), GetArgumentVector(), sstr.GetData(), long_options,
&long_options_index);
if (val == -1)
break;
if (val == '?')
{
result.AppendError ("unknown or ambiguous option");
result.SetStatus (eReturnStatusFailed);
break;
}
if (val == 0)
continue;
((Options *) &options)->OptionSeen (val);
// Look up the long option index
if (long_options_index == -1)
{
for (int j = 0;
long_options[j].name || long_options[j].has_arg || long_options[j].flag || long_options[j].val;
++j)
{
if (long_options[j].val == val)
{
long_options_index = j;
break;
}
}
}
// See if the option takes an argument, and see if one was supplied.
if (long_options_index >= 0)
{
StreamString option_str;
option_str.Printf ("-%c", (char) val);
switch (long_options[long_options_index].has_arg)
{
case no_argument:
option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()), "<no-argument>"));
break;
case required_argument:
if (optarg != NULL)
{
option_arg_vector->push_back (OptionArgPair (std::string (option_str.GetData()),
std::string (optarg)));
result.SetStatus (eReturnStatusSuccessFinishNoResult);
}
//.........这里部分代码省略.........
示例2: if
void
Args::ParseArgsForCompletion
(
Options &options,
OptionElementVector &option_element_vector,
uint32_t cursor_index
)
{
StreamString sstr;
struct option *long_options = options.GetLongOptions();
option_element_vector.clear();
if (long_options == NULL)
{
return;
}
// Leading : tells getopt to return a : for a missing option argument AND
// to suppress error messages.
sstr << ":";
for (int i = 0; long_options[i].name != NULL; ++i)
{
if (long_options[i].flag == NULL)
{
sstr << (char) long_options[i].val;
switch (long_options[i].has_arg)
{
default:
case no_argument:
break;
case required_argument:
sstr << ":";
break;
case optional_argument:
sstr << "::";
break;
}
}
}
#ifdef __GLIBC__
optind = 0;
#else
optreset = 1;
optind = 1;
#endif
opterr = 0;
int val;
const OptionDefinition *opt_defs = options.GetDefinitions();
// Fooey... getopt_long permutes the GetArgumentVector to move the options to the front.
// So we have to build another Arg and pass that to getopt_long so it doesn't
// change the one we have.
std::vector<const char *> dummy_vec (GetArgumentVector(), GetArgumentVector() + GetArgumentCount() + 1);
bool failed_once = false;
uint32_t dash_dash_pos = -1;
while (1)
{
bool missing_argument = false;
int parse_start = optind;
int long_options_index = -1;
val = ::getopt_long (dummy_vec.size() - 1,
(char *const *) dummy_vec.data(),
sstr.GetData(),
long_options,
&long_options_index);
if (val == -1)
{
// When we're completing a "--" which is the last option on line,
if (failed_once)
break;
failed_once = true;
// If this is a bare "--" we mark it as such so we can complete it successfully later.
// Handling the "--" is a little tricky, since that may mean end of options or arguments, or the
// user might want to complete options by long name. I make this work by checking whether the
// cursor is in the "--" argument, and if so I assume we're completing the long option, otherwise
// I let it pass to getopt_long which will terminate the option parsing.
// Note, in either case we continue parsing the line so we can figure out what other options
// were passed. This will be useful when we come to restricting completions based on what other
// options we've seen on the line.
if (optind < dummy_vec.size() - 1
&& (strcmp (dummy_vec[optind-1], "--") == 0))
{
dash_dash_pos = optind - 1;
if (optind - 1 == cursor_index)
{
option_element_vector.push_back (OptionArgElement (OptionArgElement::eBareDoubleDash, optind - 1,
OptionArgElement::eBareDoubleDash));
continue;
}
//.........这里部分代码省略.........