[Support][CommandLine] Add cl::Option::setDefault()

Add abstract virtual method setDefault() to class Option and implement it in its inheritors in order to be able to set all the options to its default values in user's code without actually knowing all these options. For instance:

for (auto &OM : cl::getRegisteredOptions(*cl::TopLevelSubCommand)) {
  cl::Option *O = OM.second;
  O->setDefault();
}

Reviewed by: rampitec, Eugene.Zelenko, kasaurov

Differential Revision: http://reviews.llvm.org/D36877

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@311887 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Evgeny Mankov 2017-08-28 13:39:43 +00:00
parent 384007e569
commit dc57c3f759
2 changed files with 57 additions and 0 deletions

View File

@ -349,6 +349,8 @@ public:
virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0;
virtual void setDefault() = 0;
static void printHelpStr(StringRef HelpStr, size_t Indent,
size_t FirstLineIndentedBy);
@ -1318,6 +1320,20 @@ class opt : public Option,
}
}
template <class T, class = typename std::enable_if<
std::is_assignable<T&, T>::value>::type>
void setDefaultImpl() {
const OptionValue<DataType> &V = this->getDefault();
if (V.hasValue())
this->setValue(V.getValue());
}
template <class T, class = typename std::enable_if<
!std::is_assignable<T&, T>::value>::type>
void setDefaultImpl(...) {}
void setDefault() override { setDefaultImpl<DataType>(); }
void done() {
addArgument();
Parser.initialize();
@ -1493,6 +1509,8 @@ class list : public Option, public list_storage<DataType, StorageClass> {
void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
}
void setDefault() override {}
void done() {
addArgument();
Parser.initialize();
@ -1634,6 +1652,8 @@ class bits : public Option, public bits_storage<DataType, Storage> {
void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
}
void setDefault() override {}
void done() {
addArgument();
Parser.initialize();
@ -1684,6 +1704,8 @@ class alias : public Option {
void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
}
void setDefault() override { AliasFor->setDefault(); }
ValueExpected getValueExpectedFlagDefault() const override {
return AliasFor->getValueExpectedFlag();
}

View File

@ -613,4 +613,39 @@ TEST(CommandLineTest, ResponseFiles) {
llvm::sys::fs::remove(TestDir);
}
TEST(CommandLineTest, SetDefautValue) {
cl::ResetCommandLineParser();
StackOption<std::string> Opt1("opt1", cl::init("true"));
StackOption<bool> Opt2("opt2", cl::init(true));
cl::alias Alias("alias", llvm::cl::aliasopt(Opt2));
StackOption<int> Opt3("opt3", cl::init(3));
const char *args[] = {"prog", "-opt1=false", "-opt2", "-opt3"};
EXPECT_TRUE(
cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
EXPECT_TRUE(Opt1 == "false");
EXPECT_TRUE(Opt2);
EXPECT_TRUE(Opt3 == 3);
Opt2 = false;
Opt3 = 1;
cl::ResetAllOptionOccurrences();
for (auto &OM : cl::getRegisteredOptions(*cl::TopLevelSubCommand)) {
cl::Option *O = OM.second;
if (O->ArgStr == "opt2") {
continue;
}
O->setDefault();
}
EXPECT_TRUE(Opt1 == "true");
EXPECT_TRUE(Opt2);
EXPECT_TRUE(Opt3 == 3);
}
} // anonymous namespace