llvm.org GIT mirror llvm / dc57c3f
[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 Evgeny Mankov 2 years ago
2 changed file(s) with 57 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
348348
349349 virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0;
350350
351 virtual void setDefault() = 0;
352
351353 static void printHelpStr(StringRef HelpStr, size_t Indent,
352354 size_t FirstLineIndentedBy);
353355
13171319 }
13181320 }
13191321
1322 template
1323 std::is_assignable::value>::type>
1324 void setDefaultImpl() {
1325 const OptionValue &V = this->getDefault();
1326 if (V.hasValue())
1327 this->setValue(V.getValue());
1328 }
1329
1330 template
1331 !std::is_assignable::value>::type>
1332 void setDefaultImpl(...) {}
1333
1334 void setDefault() override { setDefaultImpl(); }
1335
13201336 void done() {
13211337 addArgument();
13221338 Parser.initialize();
14921508 void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
14931509 }
14941510
1511 void setDefault() override {}
1512
14951513 void done() {
14961514 addArgument();
14971515 Parser.initialize();
16331651 void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
16341652 }
16351653
1654 void setDefault() override {}
1655
16361656 void done() {
16371657 addArgument();
16381658 Parser.initialize();
16821702 // Aliases do not need to print their values.
16831703 void printOptionValue(size_t /*GlobalWidth*/, bool /*Force*/) const override {
16841704 }
1705
1706 void setDefault() override { AliasFor->setDefault(); }
16851707
16861708 ValueExpected getValueExpectedFlagDefault() const override {
16871709 return AliasFor->getValueExpectedFlag();
612612 llvm::sys::fs::remove(TestDir);
613613 }
614614
615 TEST(CommandLineTest, SetDefautValue) {
616 cl::ResetCommandLineParser();
617
618 StackOption Opt1("opt1", cl::init("true"));
619 StackOption Opt2("opt2", cl::init(true));
620 cl::alias Alias("alias", llvm::cl::aliasopt(Opt2));
621 StackOption Opt3("opt3", cl::init(3));
622
623 const char *args[] = {"prog", "-opt1=false", "-opt2", "-opt3"};
624
625 EXPECT_TRUE(
626 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
627
628 EXPECT_TRUE(Opt1 == "false");
629 EXPECT_TRUE(Opt2);
630 EXPECT_TRUE(Opt3 == 3);
631
632 Opt2 = false;
633 Opt3 = 1;
634
635 cl::ResetAllOptionOccurrences();
636
637 for (auto &OM : cl::getRegisteredOptions(*cl::TopLevelSubCommand)) {
638 cl::Option *O = OM.second;
639 if (O->ArgStr == "opt2") {
640 continue;
641 }
642 O->setDefault();
643 }
644
645 EXPECT_TRUE(Opt1 == "true");
646 EXPECT_TRUE(Opt2);
647 EXPECT_TRUE(Opt3 == 3);
648 }
649
615650 } // anonymous namespace