forked from OSchip/llvm-project
Move the handling of CommaSeparated options into ProvideOption.
Makes '--comma-separated val1,val2' mean the same thing as '--comma-separated=val1,val2' (that is, 'val1' and 'val2' are not lumped together as 'val1,val2'). Also declutters the main loop a bit. llvm-svn: 89463
This commit is contained in:
parent
8cde1d978f
commit
5551c207cd
|
@ -177,7 +177,37 @@ static Option *LookupOption(StringRef &Arg, StringRef &Value,
|
||||||
return I->second;
|
return I->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// CommaSeparateAndAddOccurence - A wrapper around Handler->addOccurence() that
|
||||||
|
/// does special handling of cl::CommaSeparated options.
|
||||||
|
static bool CommaSeparateAndAddOccurence(Option *Handler, unsigned pos,
|
||||||
|
StringRef ArgName,
|
||||||
|
StringRef Value, bool MultiArg = false)
|
||||||
|
{
|
||||||
|
// Check to see if this option accepts a comma separated list of values. If
|
||||||
|
// it does, we have to split up the value into multiple values.
|
||||||
|
if (Handler->getMiscFlags() & CommaSeparated) {
|
||||||
|
StringRef Val(Value);
|
||||||
|
StringRef::size_type Pos = Val.find(',');
|
||||||
|
|
||||||
|
while (Pos != StringRef::npos) {
|
||||||
|
// Process the portion before the comma.
|
||||||
|
if (Handler->addOccurrence(pos, ArgName, Val.substr(0, Pos), MultiArg))
|
||||||
|
return true;
|
||||||
|
// Erase the portion before the comma, AND the comma.
|
||||||
|
Val = Val.substr(Pos+1);
|
||||||
|
Value.substr(Pos+1); // Increment the original value pointer as well.
|
||||||
|
// Check for another comma.
|
||||||
|
Pos = Val.find(',');
|
||||||
|
}
|
||||||
|
|
||||||
|
Value = Val;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Handler->addOccurrence(pos, ArgName, Value, MultiArg))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// ProvideOption - For Value, this differentiates between an empty value ("")
|
/// ProvideOption - For Value, this differentiates between an empty value ("")
|
||||||
/// and a null value (StringRef()). The later is accepted for arguments that
|
/// and a null value (StringRef()). The later is accepted for arguments that
|
||||||
|
@ -219,13 +249,13 @@ static inline bool ProvideOption(Option *Handler, StringRef ArgName,
|
||||||
|
|
||||||
// If this isn't a multi-arg option, just run the handler.
|
// If this isn't a multi-arg option, just run the handler.
|
||||||
if (NumAdditionalVals == 0)
|
if (NumAdditionalVals == 0)
|
||||||
return Handler->addOccurrence(i, ArgName, Value);
|
return CommaSeparateAndAddOccurence(Handler, i, ArgName, Value);
|
||||||
|
|
||||||
// If it is, run the handle several times.
|
// If it is, run the handle several times.
|
||||||
bool MultiArg = false;
|
bool MultiArg = false;
|
||||||
|
|
||||||
if (Value.data()) {
|
if (Value.data()) {
|
||||||
if (Handler->addOccurrence(i, ArgName, Value, MultiArg))
|
if (CommaSeparateAndAddOccurence(Handler, i, ArgName, Value, MultiArg))
|
||||||
return true;
|
return true;
|
||||||
--NumAdditionalVals;
|
--NumAdditionalVals;
|
||||||
MultiArg = true;
|
MultiArg = true;
|
||||||
|
@ -236,7 +266,7 @@ static inline bool ProvideOption(Option *Handler, StringRef ArgName,
|
||||||
return Handler->error("not enough values!");
|
return Handler->error("not enough values!");
|
||||||
Value = argv[++i];
|
Value = argv[++i];
|
||||||
|
|
||||||
if (Handler->addOccurrence(i, ArgName, Value, MultiArg))
|
if (CommaSeparateAndAddOccurence(Handler, i, ArgName, Value, MultiArg))
|
||||||
return true;
|
return true;
|
||||||
MultiArg = true;
|
MultiArg = true;
|
||||||
--NumAdditionalVals;
|
--NumAdditionalVals;
|
||||||
|
@ -627,26 +657,6 @@ void cl::ParseCommandLineOptions(int argc, char **argv,
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check to see if this option accepts a comma separated list of values. If
|
|
||||||
// it does, we have to split up the value into multiple values.
|
|
||||||
if (Handler->getMiscFlags() & CommaSeparated) {
|
|
||||||
StringRef Val(Value);
|
|
||||||
StringRef::size_type Pos = Val.find(',');
|
|
||||||
|
|
||||||
while (Pos != StringRef::npos) {
|
|
||||||
// Process the portion before the comma.
|
|
||||||
ErrorParsing |= ProvideOption(Handler, ArgName, Val.substr(0, Pos),
|
|
||||||
argc, argv, i);
|
|
||||||
// Erase the portion before the comma, AND the comma.
|
|
||||||
Val = Val.substr(Pos+1);
|
|
||||||
Value.substr(Pos+1); // Increment the original value pointer as well.
|
|
||||||
|
|
||||||
// Check for another comma.
|
|
||||||
Pos = Val.find(',');
|
|
||||||
}
|
|
||||||
Value = Val;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If this is a named positional argument, just remember that it is the
|
// If this is a named positional argument, just remember that it is the
|
||||||
// active one...
|
// active one...
|
||||||
if (Handler->getFormattingFlag() == cl::Positional)
|
if (Handler->getFormattingFlag() == cl::Positional)
|
||||||
|
|
Loading…
Reference in New Issue