cmCommand refactor: cmParseArgumentsCommand

This commit is contained in:
Gabor Bencze 2019-08-04 19:03:09 +02:00 committed by Brad King
parent 4fa9630b7e
commit d780822da6
3 changed files with 24 additions and 47 deletions

View File

@ -151,8 +151,7 @@ void GetScriptingCommands(cmState* state)
state->AddBuiltinCommand("math", cmMathCommand); state->AddBuiltinCommand("math", cmMathCommand);
state->AddBuiltinCommand("message", cmMessageCommand); state->AddBuiltinCommand("message", cmMessageCommand);
state->AddBuiltinCommand("option", cmOptionCommand); state->AddBuiltinCommand("option", cmOptionCommand);
state->AddBuiltinCommand("cmake_parse_arguments", state->AddBuiltinCommand("cmake_parse_arguments", cmParseArgumentsCommand);
cm::make_unique<cmParseArgumentsCommand>());
state->AddBuiltinCommand("return", cmReturnCommand); state->AddBuiltinCommand("return", cmReturnCommand);
state->AddBuiltinCommand("separate_arguments", state->AddBuiltinCommand("separate_arguments",
cm::make_unique<cmSeparateArgumentsCommand>()); cm::make_unique<cmSeparateArgumentsCommand>());

View File

@ -8,6 +8,7 @@
#include <utility> #include <utility>
#include "cmArgumentParser.h" #include "cmArgumentParser.h"
#include "cmExecutionStatus.h"
#include "cmMakefile.h" #include "cmMakefile.h"
#include "cmMessageType.h" #include "cmMessageType.h"
#include "cmRange.h" #include "cmRange.h"
@ -15,8 +16,6 @@
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cm_string_view.hxx" #include "cm_string_view.hxx"
class cmExecutionStatus;
static std::string EscapeArg(const std::string& arg) static std::string EscapeArg(const std::string& arg)
{ {
// replace ";" with "\;" so output argument lists will split correctly // replace ";" with "\;" so output argument lists will split correctly
@ -105,15 +104,15 @@ static void PassParsedArguments(
} }
} }
bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args, bool cmParseArgumentsCommand(std::vector<std::string> const& args,
cmExecutionStatus&) cmExecutionStatus& status)
{ {
// cmake_parse_arguments(prefix options single multi <ARGN>) // cmake_parse_arguments(prefix options single multi <ARGN>)
// 1 2 3 4 // 1 2 3 4
// or // or
// cmake_parse_arguments(PARSE_ARGV N prefix options single multi) // cmake_parse_arguments(PARSE_ARGV N prefix options single multi)
if (args.size() < 4) { if (args.size() < 4) {
this->SetError("must be called with at least 4 arguments."); status.SetError("must be called with at least 4 arguments.");
return false; return false;
} }
@ -123,7 +122,7 @@ bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args,
unsigned long argvStart = 0; unsigned long argvStart = 0;
if (*argIter == "PARSE_ARGV") { if (*argIter == "PARSE_ARGV") {
if (args.size() != 6) { if (args.size() != 6) {
this->Makefile->IssueMessage( status.GetMakefile().IssueMessage(
MessageType::FATAL_ERROR, MessageType::FATAL_ERROR,
"PARSE_ARGV must be called with exactly 6 arguments."); "PARSE_ARGV must be called with exactly 6 arguments.");
cmSystemTools::SetFatalErrorOccured(); cmSystemTools::SetFatalErrorOccured();
@ -132,9 +131,9 @@ bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args,
parseFromArgV = true; parseFromArgV = true;
argIter++; // move past PARSE_ARGV argIter++; // move past PARSE_ARGV
if (!cmStrToULong(*argIter, &argvStart)) { if (!cmStrToULong(*argIter, &argvStart)) {
this->Makefile->IssueMessage(MessageType::FATAL_ERROR, status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR,
"PARSE_ARGV index '" + *argIter + "PARSE_ARGV index '" + *argIter +
"' is not an unsigned integer"); "' is not an unsigned integer");
cmSystemTools::SetFatalErrorOccured(); cmSystemTools::SetFatalErrorOccured();
return true; return true;
} }
@ -154,8 +153,8 @@ bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args,
// anything else is put into a vector of unparsed strings // anything else is put into a vector of unparsed strings
std::vector<std::string> unparsed; std::vector<std::string> unparsed;
auto const duplicateKey = [this](std::string const& key) { auto const duplicateKey = [&status](std::string const& key) {
this->GetMakefile()->IssueMessage( status.GetMakefile().IssueMessage(
MessageType::WARNING, "keyword defined more than once: " + key); MessageType::WARNING, "keyword defined more than once: " + key);
}; };
@ -183,23 +182,24 @@ bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args,
} }
} else { } else {
// in the PARSE_ARGV move read the arguments from ARGC and ARGV# // in the PARSE_ARGV move read the arguments from ARGC and ARGV#
std::string argc = this->Makefile->GetSafeDefinition("ARGC"); std::string argc = status.GetMakefile().GetSafeDefinition("ARGC");
unsigned long count; unsigned long count;
if (!cmStrToULong(argc, &count)) { if (!cmStrToULong(argc, &count)) {
this->Makefile->IssueMessage(MessageType::FATAL_ERROR, status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR,
"PARSE_ARGV called with ARGC='" + argc + "PARSE_ARGV called with ARGC='" +
"' that is not an unsigned integer"); argc +
"' that is not an unsigned integer");
cmSystemTools::SetFatalErrorOccured(); cmSystemTools::SetFatalErrorOccured();
return true; return true;
} }
for (unsigned long i = argvStart; i < count; ++i) { for (unsigned long i = argvStart; i < count; ++i) {
std::ostringstream argName; std::ostringstream argName;
argName << "ARGV" << i; argName << "ARGV" << i;
const char* arg = this->Makefile->GetDefinition(argName.str()); const char* arg = status.GetMakefile().GetDefinition(argName.str());
if (!arg) { if (!arg) {
this->Makefile->IssueMessage(MessageType::FATAL_ERROR, status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR,
"PARSE_ARGV called with " + "PARSE_ARGV called with " +
argName.str() + " not set"); argName.str() + " not set");
cmSystemTools::SetFatalErrorOccured(); cmSystemTools::SetFatalErrorOccured();
return true; return true;
} }
@ -212,7 +212,8 @@ bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args,
parser.Parse(list, &unparsed, &keywordsMissingValues); parser.Parse(list, &unparsed, &keywordsMissingValues);
PassParsedArguments( PassParsedArguments(
prefix, *this->Makefile, options, singleValArgs, multiValArgs, unparsed, prefix, status.GetMakefile(), options, singleValArgs, multiValArgs,
unparsed,
options_set(keywordsMissingValues.begin(), keywordsMissingValues.end()), options_set(keywordsMissingValues.begin(), keywordsMissingValues.end()),
parseFromArgV); parseFromArgV);

View File

@ -8,32 +8,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "cm_memory.hxx"
#include "cmCommand.h"
class cmExecutionStatus; class cmExecutionStatus;
/** \class cmParseArgumentsCommand bool cmParseArgumentsCommand(std::vector<std::string> const& args,
* cmExecutionStatus& status);
*/
class cmParseArgumentsCommand : public cmCommand
{
public:
/**
* This is a virtual constructor for the command.
*/
std::unique_ptr<cmCommand> Clone() override
{
return cm::make_unique<cmParseArgumentsCommand>();
}
/**
* This is called when the command is first encountered in
* the CMakeLists.txt file.
*/
bool InitialPass(std::vector<std::string> const& args,
cmExecutionStatus& status) override;
};
#endif #endif