Commit aa0692de authored by Brad King's avatar Brad King Committed by Kitware Robot

Merge topic 'argument-parser'

b783e625 cmExecuteProcessCommand: Port to cmArgumentParser
9bddb03f cmParseArgumentsCommand: Port to cmArgumentParser
45edf1ad Retire cmCommandArgumentsHelper
f5acecaa cmExportCommand: Port to cmArgumentParser
e6b6bb06 cmInstallCommand: Port to cmArgumentParser
4336a29e cmFileCommand: Port to cmArgumentParser
4359fe13 Introduce cmArgumentParser
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: Leonid Pospelov's avatarLeonid Pospelov <pospelovlm@yandex.ru>
Merge-request: !3137
parents 6b126c47 b783e625
Pipeline #134268 passed with stage
in 0 seconds
......@@ -143,6 +143,8 @@ set(SRCS
cmAffinity.cxx
cmAffinity.h
cmArchiveWrite.cxx
cmArgumentParser.cxx
cmArgumentParser.h
cmBase32.cxx
cmCacheManager.cxx
cmCacheManager.h
......@@ -443,8 +445,6 @@ set(SRCS
cmCMakeMinimumRequired.h
cmCMakePolicyCommand.cxx
cmCMakePolicyCommand.h
cmCommandArgumentsHelper.cxx
cmCommandArgumentsHelper.h
cmConditionEvaluator.cxx
cmConditionEvaluator.h
cmConfigureFileCommand.cxx
......
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmArgumentParser.h"
#include <algorithm>
#include <type_traits>
namespace ArgumentParser {
auto ActionMap::Emplace(cm::string_view name, Action action)
-> std::pair<iterator, bool>
{
auto const it =
std::lower_bound(this->begin(), this->end(), name,
[](value_type const& elem, cm::string_view const& k) {
return elem.first < k;
});
return (it != this->end() && it->first == name)
? std::make_pair(it, false)
: std::make_pair(this->emplace(it, name, std::move(action)), true);
}
auto ActionMap::Find(cm::string_view name) const -> const_iterator
{
auto const it =
std::lower_bound(this->begin(), this->end(), name,
[](value_type const& elem, cm::string_view const& k) {
return elem.first < k;
});
return (it != this->end() && it->first == name) ? it : this->end();
}
void Instance::Bind(bool& val)
{
val = true;
this->CurrentString = nullptr;
this->CurrentList = nullptr;
this->ExpectValue = false;
}
void Instance::Bind(std::string& val)
{
this->CurrentString = &val;
this->CurrentList = nullptr;
this->ExpectValue = true;
}
void Instance::Bind(StringList& val)
{
this->CurrentString = nullptr;
this->CurrentList = &val;
this->ExpectValue = true;
}
void Instance::Bind(MultiStringList& val)
{
this->CurrentString = nullptr;
this->CurrentList = (val.emplace_back(), &val.back());
this->ExpectValue = false;
}
void Instance::Consume(cm::string_view arg, void* result,
std::vector<std::string>* unparsedArguments,
std::vector<std::string>* keywordsMissingValue)
{
auto const it = this->Bindings.Find(arg);
if (it != this->Bindings.end()) {
it->second(*this, result);
if (this->ExpectValue && keywordsMissingValue != nullptr) {
keywordsMissingValue->emplace_back(arg);
}
return;
}
if (this->CurrentString != nullptr) {
this->CurrentString->assign(std::string(arg));
this->CurrentString = nullptr;
this->CurrentList = nullptr;
} else if (this->CurrentList != nullptr) {
this->CurrentList->emplace_back(arg);
} else if (unparsedArguments != nullptr) {
unparsedArguments->emplace_back(arg);
}
if (this->ExpectValue) {
if (keywordsMissingValue != nullptr) {
keywordsMissingValue->pop_back();
}
this->ExpectValue = false;
}
}
} // namespace ArgumentParser
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#ifndef cmArgumentParser_h
#define cmArgumentParser_h
#include "cmConfigure.h" // IWYU pragma: keep
#include "cm_static_string_view.hxx"
#include "cm_string_view.hxx"
#include <cassert>
#include <functional>
#include <string>
#include <utility>
#include <vector>
namespace ArgumentParser {
using StringList = std::vector<std::string>;
using MultiStringList = std::vector<StringList>;
class Instance;
using Action = std::function<void(Instance&, void*)>;
// using ActionMap = cm::flat_map<cm::string_view, Action>;
class ActionMap : public std::vector<std::pair<cm::string_view, Action>>
{
public:
std::pair<iterator, bool> Emplace(cm::string_view name, Action action);
const_iterator Find(cm::string_view name) const;
};
class Instance
{
public:
Instance(ActionMap const& bindings)
: Bindings(bindings)
{
}
void Bind(bool& val);
void Bind(std::string& val);
void Bind(StringList& val);
void Bind(MultiStringList& val);
void Consume(cm::string_view arg, void* result,
std::vector<std::string>* unparsedArguments,
std::vector<std::string>* keywordsMissingValue);
private:
ActionMap const& Bindings;
std::string* CurrentString = nullptr;
StringList* CurrentList = nullptr;
bool ExpectValue = false;
};
} // namespace ArgumentParser
template <typename Result>
class cmArgumentParser
{
public:
// I *think* this function could be made `constexpr` when the code is
// compiled as C++20. This would allow building a parser at compile time.
template <typename T>
cmArgumentParser& Bind(cm::static_string_view name, T Result::*member)
{
bool const inserted =
this->Bindings
.Emplace(name,
[member](ArgumentParser::Instance& instance, void* result) {
instance.Bind(static_cast<Result*>(result)->*member);
})
.second;
assert(inserted), (void)inserted;
return *this;
}
template <typename Range>
void Parse(Result& result, Range const& args,
std::vector<std::string>* unparsedArguments = nullptr,
std::vector<std::string>* keywordsMissingValue = nullptr) const
{
ArgumentParser::Instance instance(this->Bindings);
for (cm::string_view arg : args) {
instance.Consume(arg, &result, unparsedArguments, keywordsMissingValue);
}
}
template <typename Range>
Result Parse(Range const& args,
std::vector<std::string>* unparsedArguments = nullptr,
std::vector<std::string>* keywordsMissingValue = nullptr) const
{
Result result;
this->Parse(result, args, unparsedArguments, keywordsMissingValue);
return result;
}
private:
ArgumentParser::ActionMap Bindings;
};
template <>
class cmArgumentParser<void>
{
public:
template <typename T>
cmArgumentParser& Bind(cm::static_string_view name, T& ref)
{
bool const inserted = this->Bind(cm::string_view(name), ref);
assert(inserted), (void)inserted;
return *this;
}
template <typename Range>
void Parse(Range const& args,
std::vector<std::string>* unparsedArguments = nullptr,
std::vector<std::string>* keywordsMissingValue = nullptr) const
{
ArgumentParser::Instance instance(this->Bindings);
for (cm::string_view arg : args) {
instance.Consume(arg, nullptr, unparsedArguments, keywordsMissingValue);
}
}
protected:
template <typename T>
bool Bind(cm::string_view name, T& ref)
{
return this->Bindings
.Emplace(name,
[&ref](ArgumentParser::Instance& instance, void*) {
instance.Bind(ref);
})
.second;
}
private:
ArgumentParser::ActionMap Bindings;
};
#endif
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCommandArgumentsHelper.h"
cmCommandArgument::cmCommandArgument(cmCommandArgumentsHelper* args,
const char* key,
cmCommandArgumentGroup* group)
: Key(key)
, Group(group)
, WasActive(false)
, ArgumentsBeforeEmpty(true)
, CurrentIndex(0)
{
if (args != nullptr) {
args->AddArgument(this);
}
if (this->Group != nullptr) {
this->Group->ContainedArguments.push_back(this);
}
}
void cmCommandArgument::Reset()
{
this->WasActive = false;
this->CurrentIndex = 0;
this->DoReset();
}
void cmCommandArgument::Follows(const cmCommandArgument* arg)
{
this->ArgumentsBeforeEmpty = false;
this->ArgumentsBefore.insert(arg);
}
void cmCommandArgument::FollowsGroup(const cmCommandArgumentGroup* group)
{
if (group != nullptr) {
this->ArgumentsBeforeEmpty = false;
this->ArgumentsBefore.insert(group->ContainedArguments.begin(),
group->ContainedArguments.end());
}
}
bool cmCommandArgument::MayFollow(const cmCommandArgument* current) const
{
if (this->ArgumentsBeforeEmpty) {
return true;
}
return this->ArgumentsBefore.find(current) != this->ArgumentsBefore.end();
}
bool cmCommandArgument::KeyMatches(const std::string& key) const
{
if ((this->Key == nullptr) || (this->Key[0] == '\0')) {
return true;
}
return (key == this->Key);
}
void cmCommandArgument::ApplyOwnGroup()
{
if (this->Group != nullptr) {
for (cmCommandArgument* cargs : this->Group->ContainedArguments) {
if (cargs != this) {
this->ArgumentsBefore.insert(cargs);
}
}
}
}
void cmCommandArgument::Activate()
{
this->WasActive = true;
this->CurrentIndex = 0;
}
bool cmCommandArgument::Consume(const std::string& arg)
{
bool res = this->DoConsume(arg, this->CurrentIndex);
this->CurrentIndex++;
return res;
}
cmCAStringVector::cmCAStringVector(cmCommandArgumentsHelper* args,
const char* key,
cmCommandArgumentGroup* group)
: cmCommandArgument(args, key, group)
, Ignore(nullptr)
{
if ((key == nullptr) || (*key == 0)) {
this->DataStart = 0;
} else {
this->DataStart = 1;
}
}
bool cmCAStringVector::DoConsume(const std::string& arg, unsigned int index)
{
if (index >= this->DataStart) {
if ((this->Ignore == nullptr) || (arg != this->Ignore)) {
this->Vector.push_back(arg);
}
}
return false;
}
void cmCAStringVector::DoReset()
{
this->Vector.clear();
}
cmCAString::cmCAString(cmCommandArgumentsHelper* args, const char* key,
cmCommandArgumentGroup* group)
: cmCommandArgument(args, key, group)
{
if ((key == nullptr) || (*key == 0)) {
this->DataStart = 0;
} else {
this->DataStart = 1;
}
}
bool cmCAString::DoConsume(const std::string& arg, unsigned int index)
{
if (index == this->DataStart) {
this->String = arg;
}
return index >= this->DataStart;
}
void cmCAString::DoReset()
{
this->String.clear();
}
cmCAEnabler::cmCAEnabler(cmCommandArgumentsHelper* args, const char* key,
cmCommandArgumentGroup* group)
: cmCommandArgument(args, key, group)
, Enabled(false)
{
}
bool cmCAEnabler::DoConsume(const std::string&, unsigned int index)
{
if (index == 0) {
this->Enabled = true;
}
return true;
}
void cmCAEnabler::DoReset()
{
this->Enabled = false;
}
cmCADisabler::cmCADisabler(cmCommandArgumentsHelper* args, const char* key,
cmCommandArgumentGroup* group)
: cmCommandArgument(args, key, group)
, Enabled(true)
{
}
bool cmCADisabler::DoConsume(const std::string&, unsigned int index)
{
if (index == 0) {
this->Enabled = false;
}
return true;
}
void cmCADisabler::DoReset()
{
this->Enabled = true;
}
void cmCommandArgumentGroup::Follows(const cmCommandArgument* arg)
{
for (cmCommandArgument* ca : this->ContainedArguments) {
ca->Follows(arg);
}
}
void cmCommandArgumentGroup::FollowsGroup(const cmCommandArgumentGroup* group)
{
for (cmCommandArgument* ca : this->ContainedArguments) {
ca->FollowsGroup(group);
}
}
void cmCommandArgumentsHelper::Parse(const std::vector<std::string>* args,
std::vector<std::string>* unconsumedArgs)
{
if (args == nullptr) {
return;
}
for (cmCommandArgument* ca : this->Arguments) {
ca->ApplyOwnGroup();
ca->Reset();
}
cmCommandArgument* activeArgument = nullptr;
const cmCommandArgument* previousArgument = nullptr;
for (std::string const& it : *args) {
for (cmCommandArgument* ca : this->Arguments) {
if (ca->KeyMatches(it) && (ca->MayFollow(previousArgument))) {
activeArgument = ca;
activeArgument->Activate();
break;
}
}
if (activeArgument) {
bool argDone = activeArgument->Consume(it);
previousArgument = activeArgument;
if (argDone) {
activeArgument = nullptr;
}
} else {
if (unconsumedArgs != nullptr) {
unconsumedArgs->push_back(it);
}
}
}
}
void cmCommandArgumentsHelper::AddArgument(cmCommandArgument* arg)
{
this->Arguments.push_back(arg);
}
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#ifndef cmCommandArgumentsHelper_h
#define cmCommandArgumentsHelper_h
#include "cmConfigure.h" // IWYU pragma: keep
#include <set>
#include <string>
#include <vector>
class cmCommandArgumentGroup;
class cmCommandArgumentsHelper;
/* cmCommandArgumentsHelper, cmCommandArgumentGroup and cmCommandArgument (i.e.
its derived classes cmCAXXX can be used to simplify the processing of
arguments to cmake commands. Maybe they can also be used to generate
documentation.
For every argument supported by a command one cmCommandArgument is created
and added to cmCommandArgumentsHelper. cmCommand has a cmCommandArgumentsHelper
as member variable so this should be used.
The order of the arguments is defined using the Follows(arg) method. It says
that this argument follows immediateley the given argument. It can be used
with multiple arguments if the argument can follow after different arguments.
Arguments can be arranged in groups using cmCommandArgumentGroup. Every
member of a group can follow any other member of the group. These groups
can also be used to define the order.
Once all arguments and groups are set up, cmCommandArgumentsHelper::Parse()
is called and afterwards the values of the arguments can be evaluated.
For an example see cmExportCommand.cxx.
*/
class cmCommandArgument
{
public:
cmCommandArgument(cmCommandArgumentsHelper* args, const char* key,
cmCommandArgumentGroup* group = nullptr);
virtual ~cmCommandArgument() = default;
/// this argument may follow after arg. 0 means it comes first.
void Follows(const cmCommandArgument* arg);
/// this argument may follow after any of the arguments in the given group
void FollowsGroup(const cmCommandArgumentGroup* group);
/// Returns true if the argument was found in the argument list
bool WasFound() const { return this->WasActive; }
// The following methods are only called from
// cmCommandArgumentsHelper::Parse(), but making this a friend would
// give it access to everything
/// Make the current argument the currently active argument
void Activate();
/// Consume the current string
bool Consume(const std::string& arg);
/// Return true if this argument may follow after the given argument.
bool MayFollow(const cmCommandArgument* current) const;
/** Returns true if the given key matches the key for this argument.
If this argument has an empty key everything matches. */
bool KeyMatches(const std::string& key) const;
/// Make this argument follow all members of the own group
void ApplyOwnGroup();
/// Reset argument, so it's back to its initial state
void Reset();
private:
const char* Key;
std::set<const cmCommandArgument*> ArgumentsBefore;
cmCommandArgumentGroup* Group;
bool WasActive;
bool ArgumentsBeforeEmpty;
unsigned int CurrentIndex;
virtual bool DoConsume(const std::string& arg, unsigned int index) = 0;
virtual void DoReset() = 0;
};
/** cmCAStringVector is to be used for arguments which can consist of more
than one string, e.g. the FILES argument in INSTALL(FILES f1 f2 f3 ...). */
class cmCAStringVector : public cmCommandArgument
{
public:
cmCAStringVector(cmCommandArgumentsHelper* args, const char* key,
cmCommandArgumentGroup* group = nullptr);
/// Return the vector of strings
const std::vector<std::string>& GetVector() const { return this->Vector; }
/** Is there a keyword which should be skipped in
the arguments (e.g. ARGS for ADD_CUSTOM_COMMAND) ? */
void SetIgnore(const char* ignore) { this->Ignore = ignore; }
private:
std::vector<std::string> Vector;
unsigned int DataStart;
const char* Ignore;
bool DoConsume(const std::string& arg, unsigned int index) override;
void DoReset() override;
};
/** cmCAString is to be used for arguments which consist of one value,
e.g. the executable name in ADD_EXECUTABLE(). */
class cmCAString : public cmCommandArgument
{
public:
cmCAString(cmCommandArgumentsHelper* args, const char* key,
cmCommandArgumentGroup* group = nullptr);
/// Return the string
const std::string& GetString() const { return this->String; }
const char* GetCString() const { return this->String.c_str(); }
private:
std::string String;
unsigned int DataStart;
bool DoConsume(const std::string& arg, unsigned int index) override;
void DoReset() override;
};
/** cmCAEnabler is to be used for options which are off by default and can be
enabled using a special argument, e.g. EXCLUDE_FROM_ALL in ADD_EXECUTABLE(). */
class cmCAEnabler : public cmCommandArgument
{
public:
cmCAEnabler(cmCommandArgumentsHelper* args, const char* key,
cmCommandArgumentGroup* group = nullptr);
/// Has it been enabled ?
bool IsEnabled() const { return this->Enabled; }
private:
bool Enabled;
bool DoConsume(const std::string& arg, unsigned int index) override;
void DoReset() override;
};
/** cmCADisable is to be used for options which are on by default and can be
disabled using a special argument.*/
class cmCADisabler : public cmCommandArgument
{
public:
cmCADisabler(cmCommandArgumentsHelper* args, const char* key,
cmCommandArgumentGroup* group = nullptr);
/// Is it still enabled ?
bool IsEnabled() const { return this->Enabled; }
private:
bool Enabled;
bool DoConsume(const std::string& arg, unsigned int index) override;
void DoReset() override;
};
/** Group of arguments, needed for ordering. E.g. WIN32, EXCLUDE_FROM_ALL and
MACSOX_BUNDLE from ADD_EXECUTABLE() are a group.
*/
class cmCommandArgumentGroup
{
friend class cmCommandArgument;
public:
/// All members of this group may follow the given argument
void Follows(const cmCommandArgument* arg);
/// All members of this group may follow all members of the given group
void FollowsGroup(const cmCommandArgumentGroup* group);
private:
std::vector<cmCommandArgument*> ContainedArguments;
};
class cmCommandArgumentsHelper
{
public:
/// Parse the argument list
void Parse(const std::vector<std::string>* args,
std::vector<std::string>* unconsumedArgs);
/// Add an argument.
void AddArgument(cmCommandArgument* arg);
private:
std::vector<cmCommandArgument*> Arguments;
};
#endif
This diff is collapsed.
......@@ -2,10 +2,13 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmExportCommand.h"
#include "cm_static_string_view.hxx"
#include "cmsys/RegularExpression.hxx"
#include <map>
#include <sstream>
#include <utility>
#include "cmArgumentParser.h"
#include "cmExportBuildAndroidMKGenerator.h"
#include "cmExportBuildFileGenerator.h"
#include "cmExportSetMap.h"
......@@ -18,6 +21,7 @@
#include "cmSystemTools.h"
#include "cmTarget.h"
class cmExportSet;
class cmExecutionStatus;
#if defined(__HAIKU__)
......@@ -25,19 +29,6 @@ class cmExecutionStatus;
# include <StorageDefs.h>
#endif
cmExportCommand::cmExportCommand()
: Targets(&Helper, "TARGETS")
, Append(&Helper, "APPEND", &ArgumentGroup)
, ExportSetName(&Helper, "EXPORT", &ArgumentGroup)
, Namespace(&Helper, "NAMESPACE", &ArgumentGroup)
, Filename(&Helper, "FILE", &ArgumentGroup)
, ExportOld(&Helper, "EXPORT_LINK_INTERFACE_LIBRARIES", &ArgumentGroup)
, AndroidMKFile(&Helper, "ANDROID_MK")
{
this->ExportSet = nullptr;
}