[llvm] [CommandLine] Add subcommand groups (PR #75678)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 18 01:07:19 PST 2023


================
@@ -2274,4 +2274,67 @@ TEST(CommandLineTest, UnknownCommands) {
   EXPECT_EQ(Errs, "prog: Unknown subcommand 'faz'.  Try: 'prog --help'\n");
 }
 
+TEST(CommandLineTest, SubCommandGroups) {
+  cl::ResetCommandLineParser();
+
+  StackSubCommand SC1("sc1", "SC1 subcommand");
+  StackSubCommand SC2("sc2", "SC2 subcommand");
+  StackSubCommand SC3("sc3", "SC3 subcommand");
+  cl::SubCommandGroup Group12 = {&SC1, &SC2};
+  cl::SubCommandGroup Group23 = {&SC2, &SC3};
+
+  StackOption<bool> Opt1("opt1", cl::sub(SC1), cl::init(false));
+  StackOption<bool> Opt2("opt2", cl::sub(SC2), cl::init(false));
+  StackOption<bool> Opt3("opt3", cl::sub(SC3), cl::init(false));
+  StackOption<bool> Opt12("opt12", cl::sub(Group12), cl::init(false));
+  StackOption<bool> Opt23("opt23", cl::sub(Group23), cl::init(false));
+
+  EXPECT_EQ(2, SC1.OptionsMap.size());
+  EXPECT_TRUE(SC1.OptionsMap.contains("opt1"));
+  EXPECT_TRUE(SC1.OptionsMap.contains("opt12"));
+
+  EXPECT_EQ(3, SC2.OptionsMap.size());
+  EXPECT_TRUE(SC2.OptionsMap.contains("opt2"));
+  EXPECT_TRUE(SC2.OptionsMap.contains("opt12"));
+  EXPECT_TRUE(SC2.OptionsMap.contains("opt23"));
+
+  EXPECT_EQ(2, SC3.OptionsMap.size());
+  EXPECT_TRUE(SC3.OptionsMap.contains("opt3"));
+  EXPECT_TRUE(SC3.OptionsMap.contains("opt23"));
+
+  const char *Args1[] = {"prog", "--opt1"};
+  EXPECT_FALSE(cl::ParseCommandLineOptions(std::size(Args1), Args1, StringRef(),
+                                           &llvm::nulls()));
+
+  cl::ResetAllOptionOccurrences();
+
+  const char *Args2[] = {"prog", "sc2", "--opt2"};
+  EXPECT_TRUE(cl::ParseCommandLineOptions(std::size(Args2), Args2, StringRef(),
----------------
jh7370 wrote:

I think it might be slightly easier to follow the test by dividing it up into three individual tests, using a shared test fixture to do the set up per test. This then means you can name the inidivdual tests distinctly to describe the behaviour that is intended to be tested.

https://github.com/llvm/llvm-project/pull/75678


More information about the llvm-commits mailing list