Index: llvm/trunk/include/llvm/Support/CommandLine.h =================================================================== --- llvm/trunk/include/llvm/Support/CommandLine.h +++ llvm/trunk/include/llvm/Support/CommandLine.h @@ -349,6 +349,8 @@ 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 @@ } } + template ::value>::type> + void setDefaultImpl() { + const OptionValue &V = this->getDefault(); + if (V.hasValue()) + this->setValue(V.getValue()); + } + + template ::value>::type> + void setDefaultImpl(...) {} + + void setDefault() override { setDefaultImpl(); } + void done() { addArgument(); Parser.initialize(); @@ -1493,6 +1509,8 @@ void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override { } + void setDefault() override {} + void done() { addArgument(); Parser.initialize(); @@ -1634,6 +1652,8 @@ void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override { } + void setDefault() override {} + void done() { addArgument(); Parser.initialize(); @@ -1684,6 +1704,8 @@ void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override { } + void setDefault() override { AliasFor->setDefault(); } + ValueExpected getValueExpectedFlagDefault() const override { return AliasFor->getValueExpectedFlag(); } Index: llvm/trunk/unittests/Support/CommandLineTest.cpp =================================================================== --- llvm/trunk/unittests/Support/CommandLineTest.cpp +++ llvm/trunk/unittests/Support/CommandLineTest.cpp @@ -613,4 +613,39 @@ llvm::sys::fs::remove(TestDir); } +TEST(CommandLineTest, SetDefautValue) { + cl::ResetCommandLineParser(); + + StackOption Opt1("opt1", cl::init("true")); + StackOption Opt2("opt2", cl::init(true)); + cl::alias Alias("alias", llvm::cl::aliasopt(Opt2)); + StackOption 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